-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of issue #10 . Adds redux (@reduxjs/toolkit) and react-redux in order to manage state. Added a basic demo with initializing a user in state based on the server's socket.id
- Loading branch information
Showing
9 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { configureStore } from '@reduxjs/toolkit'; | ||
import { userReducer, userSlice } from './user'; | ||
|
||
export const store = configureStore({ | ||
reducer: { | ||
user: userReducer, | ||
}, | ||
devTools: true, | ||
preloadedState: { | ||
user: userSlice.getInitialState(), | ||
}, | ||
}); | ||
|
||
// Infer the `RootState` and `AppDispatch` types from the store itself | ||
export type RootState = ReturnType<typeof store.getState>; | ||
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState} | ||
export type AppDispatch = typeof store.dispatch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './user'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createSlice, PayloadAction, Reducer } from '@reduxjs/toolkit'; | ||
|
||
import { User } from '@/types/user'; | ||
|
||
export const userSlice = createSlice({ | ||
name: 'user', | ||
initialState: {} as User, | ||
reducers: { | ||
initializeUser(state, action: PayloadAction<{ id: string }>) { | ||
state.id = action.payload.id; | ||
state.isDisconnected = false; | ||
}, | ||
disconnectUser(state) { | ||
state.id = undefined; | ||
state.name = undefined; | ||
state.isDisconnected = true; | ||
}, | ||
chooseName(state, action: PayloadAction<{ name: string }>) { | ||
state.name = action.payload.name; | ||
}, | ||
}, | ||
}); | ||
|
||
export const userReducer: Reducer<User> = userSlice.reducer; | ||
|
||
export const { disconnectUser, initializeUser, chooseName } = userSlice.actions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type User = { | ||
id?: string; | ||
isDisconnected?: boolean; // if true, user has lost connection with server | ||
name?: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters