Skip to content

Commit

Permalink
Merge pull request #9 from zoopi-palette/feat/redux
Browse files Browse the repository at this point in the history
Feat/redux
  • Loading branch information
hotbreakb authored May 1, 2022
2 parents 6309c35 + 5ce1314 commit d9eafc6
Show file tree
Hide file tree
Showing 8 changed files with 16,464 additions and 103 deletions.
33 changes: 33 additions & 0 deletions modules/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {configureStore, Store} from "@reduxjs/toolkit";
import {createWrapper} from "next-redux-wrapper";
import {logger} from "redux-logger";
import createSagaMiddleware, {Task} from "redux-saga";
import userReducer from "./user/user";

// ref: https://github.com/vercel/next.js/tree/canary/examples/with-redux-saga
// ref: https://github.com/kirill-konshin/next-redux-wrapper#usage-with-redux-saga
// ref: https://github.com/Tinkerbell-Green/need-compliments/pull/6/files
export interface SagaStore extends Store {
sagaTask?: Task;
}

function* rootSaga() {
// yield all([사가()]);
}

const makeStore = () => {
const sagaMiddleware = createSagaMiddleware();
const store: SagaStore = configureStore({
reducer: {
user: userReducer,
},
devTools: process.env.NODE_ENV !== "production",
middleware: [sagaMiddleware, logger],
});
store.sagaTask = sagaMiddleware.run(rootSaga);
return store
}

export type RootStore = ReturnType<typeof makeStore>;
export type RootState = ReturnType<RootStore["getState"]>;
export const wrapper = createWrapper<RootStore>(makeStore, {debug: true});
34 changes: 34 additions & 0 deletions modules/user/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {createSlice, PayloadAction} from "@reduxjs/toolkit";
import {HYDRATE} from "next-redux-wrapper";

export type State = {
loggedInUserId: string | null,
initialized: boolean,
}

export const initialState: State = {
loggedInUserId: null,
initialized: false,
}

export const userSlice = createSlice({
name: "user",
initialState,
reducers: {
SET_USER_ID: (state: State, {payload}: PayloadAction<{ id: string }>) => {
state.loggedInUserId = payload.id;
},
SET_INITIALIZED: (state: State, {payload}: PayloadAction<{ isInitialized: boolean }>) => {
state.initialized = payload.isInitialized;
},
},
extraReducers: {
[HYDRATE]: (state: State, {payload}: PayloadAction) => payload,
}
})

export const {
SET_USER_ID, SET_INITIALIZED
} = userSlice.actions;

export default userSlice.reducer;
Loading

0 comments on commit d9eafc6

Please sign in to comment.