From 6db9cd436b16abe528436270700c9758d0addd15 Mon Sep 17 00:00:00 2001 From: Danny Rorabaugh Date: Mon, 6 Nov 2023 13:59:46 -0500 Subject: [PATCH 1/2] Port analytics to use redux-toolkit --- src/rootReducer.ts | 2 +- src/types/Redux/analytics.ts | 53 ++++++++++++-------------- src/types/Redux/analyticsReduxTypes.ts | 9 ----- 3 files changed, 25 insertions(+), 39 deletions(-) diff --git a/src/rootReducer.ts b/src/rootReducer.ts index a617d965fc..f3065316e8 100644 --- a/src/rootReducer.ts +++ b/src/rootReducer.ts @@ -10,7 +10,7 @@ import { characterInventoryReducer } from "goals/CharacterInventory/Redux/Charac import { mergeDupStepReducer } from "goals/MergeDuplicates/Redux/MergeDupsReducer"; import { reviewEntriesReducer } from "goals/ReviewEntries/ReviewEntriesComponent/Redux/ReviewEntriesReducer"; import { StoreState } from "types"; -import { analyticsReducer } from "types/Redux/analytics"; +import analyticsReducer from "types/Redux/analytics"; export const rootReducer: Reducer = combineReducers({ //login diff --git a/src/types/Redux/analytics.ts b/src/types/Redux/analytics.ts index 290bf04723..7c2ad48e43 100644 --- a/src/types/Redux/analytics.ts +++ b/src/types/Redux/analytics.ts @@ -1,36 +1,31 @@ -import { - AnalyticsActionTypes, - AnalyticsChangePageAction, - AnalyticsState, - defaultState, -} from "types/Redux/analyticsReduxTypes"; +import { PayloadAction, createSlice } from "@reduxjs/toolkit"; -export function changePage(newPage: string): AnalyticsChangePageAction { - return { - type: AnalyticsActionTypes.ChangePage, - newPage, - }; -} +import { StoreActionTypes } from "rootActions"; +import { defaultState } from "types/Redux/analyticsReduxTypes"; -export const analyticsReducer = ( - //createStore() calls each reducer with undefined state - state: AnalyticsState = defaultState, - action: AnalyticsChangePageAction -): AnalyticsState => { - switch (action.type) { - case AnalyticsActionTypes.ChangePage: - if (action.newPage !== state.currentPage) { +const analyticsSlice = createSlice({ + name: "analyticsState", + initialState: defaultState, + reducers: { + changePageAction: (state, action) => { + if (action.payload !== state.currentPage) { analytics.track("navigate", { + destination: action.payload, source: state.currentPage, - destination: action.newPage, }); } - return { - ...state, - currentPage: action.newPage, - }; + state.currentPage = action.payload; + }, + resetAction: () => defaultState, + }, + extraReducers: (builder) => + builder.addCase(StoreActionTypes.RESET, () => defaultState), +}); + +const { changePageAction } = analyticsSlice.actions; - default: - return state; - } -}; +export default analyticsSlice.reducer; + +export function changePage(newPage: string): PayloadAction { + return changePageAction(newPage); +} diff --git a/src/types/Redux/analyticsReduxTypes.ts b/src/types/Redux/analyticsReduxTypes.ts index a7210e11ac..036cd9b620 100644 --- a/src/types/Redux/analyticsReduxTypes.ts +++ b/src/types/Redux/analyticsReduxTypes.ts @@ -5,12 +5,3 @@ export interface AnalyticsState { export const defaultState: AnalyticsState = { currentPage: "", }; - -export enum AnalyticsActionTypes { - ChangePage = "CHANGE_CURRENT_PAGE", -} - -export interface AnalyticsChangePageAction { - type: AnalyticsActionTypes.ChangePage; - newPage: string; -} From 014a2d354eb45a80454b70905f0b4d53057aaef1 Mon Sep 17 00:00:00 2001 From: Danny Rorabaugh Date: Wed, 8 Nov 2023 08:54:31 -0500 Subject: [PATCH 2/2] Remove unused reset --- src/types/Redux/analytics.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/types/Redux/analytics.ts b/src/types/Redux/analytics.ts index 7c2ad48e43..930d6af320 100644 --- a/src/types/Redux/analytics.ts +++ b/src/types/Redux/analytics.ts @@ -16,7 +16,6 @@ const analyticsSlice = createSlice({ } state.currentPage = action.payload; }, - resetAction: () => defaultState, }, extraReducers: (builder) => builder.addCase(StoreActionTypes.RESET, () => defaultState),