Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redux-toolkit alternative #110

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@fontsource/roboto": "^5.0.13",
"@mui/icons-material": "^5.15.17",
"@mui/material": "^5.15.17",
"@reduxjs/toolkit": "^2.2.5",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
Expand Down
10 changes: 10 additions & 0 deletions src/store/root-reducer.toolkit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { combineReducers } from "@reduxjs/toolkit";

import { userReducer } from "./shared/user/user.reducer.toolkit";
import { nutritionTrackerReducer } from "./signed-out/nutrition-tracker/nutrition-tracker.reducer.toolkit";

export const rootReducer = combineReducers({
user: userReducer,

nutritionTracker: nutritionTrackerReducer
})
19 changes: 19 additions & 0 deletions src/store/shared/user/user.reducer.toolkit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createSlice } from "@reduxjs/toolkit";

const INITIAL_STATE = {
currentUser: null
}

export const userSlice = createSlice({
name: "user",
initialState: INITIAL_STATE,
reducers: {
setCurrentUser(state, action) {
state.currentUser = action.payload
}
}
})

export const { setCurrentUser } = userSlice.actions

export const userReducer = userSlice.reducer
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { createSlice } from "@reduxjs/toolkit";

export const NUTRITION_TRACKER_INITIAL_STATE = {
nutritionTrackedDays: [],
formInputMicronutrients: [],
filterConditions: null,
nutritionTrackedDaysView: [],
dayTrackedSearchResult: undefined,
nutritionTrackedDaysSummary: {}
}

export const nutritionTrackerSlice = createSlice({
name: "nutritionTracker",
initialState: NUTRITION_TRACKER_INITIAL_STATE,
reducers: {
setNutritionTrackedDays(state, action) {
state.nutritionTrackedDays = action.payload
},
setFormInputMicronutrients(state, action) {
state.formInputMicronutrients = action.payload
},
setFilterConditions(state, action) {
state.filterConditions = action.payload
},
setNutritionTrackedDaysView(state, action) {
state.nutritionTrackedDaysView = action.payload
},
setDayTrackedSearchResult(state, action) {
state.dayTrackedSearchResult = action.payload
},
setNutritionTrackedDaysSummary(state, action) {
state.nutritionTrackedDaysSummary = action.payload
}
}
})

export const { setNutritionTrackedDays, setFormInputMicronutrients, setFilterConditions,
setNutritionTrackedDaysView, setDayTrackedSearchResult, setNutritionTrackedDaysSummary
} = nutritionTrackerSlice.actions

export const nutritionTrackerReducer = nutritionTrackerSlice.reducer
14 changes: 14 additions & 0 deletions src/store/store.toolkit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { configureStore } from "@reduxjs/toolkit";
import logger from "redux-logger";
import rootReducer from "./root-reducer.toolkit"

const middleWares = [
process.env.NODE_ENV === "development" && logger
].filter(Boolean)

export const store = configureStore({
reducer: rootReducer,
middleware: (getDefaultMiddleware) => getDefaultMiddleware({
serializableCheck: false
}).concat(middleWares)
})
Loading