Last active
July 22, 2021 16:43
-
-
Save mahpah/5f9bd2785177fb005d5c38fdea498b9c to your computer and use it in GitHub Desktop.
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
import users from './users'; | |
const rootReducer = { | |
users | |
}; | |
export const store = configureStore({ | |
reducer: rootReducer, | |
middleware: getDefaultMiddleware({ | |
thunk: true, | |
}), | |
}); | |
export type AppState = ReturnType<typeof store.getState>; | |
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
import { combineReducers, createAsyncThunk, createEntityAdapter, createSlice } from '@reduxjs/toolkit'; | |
import axios from 'axios'; | |
type User = { | |
uid: string; | |
displayName: string; | |
email: string; | |
}; | |
const userAdapter = createEntityAdapter<User>(); | |
const loadUser = createAsyncThunk('users/load', async (params: { nextToken: string }) => { | |
return (await axios.get('/api/users', { | |
params: { | |
nextToken: params.nextToken, | |
}, | |
})) as { items: User[] }; | |
}); | |
const userSlice = createSlice({ | |
name: 'users', | |
initialState: userAdapter.getInitialState(), | |
reducers: {}, | |
extraReducers: (builder) => { | |
builder.addCase(loadUser.fulfilled, (state, { payload }) => { | |
userAdapter.setAll(state, payload.items); | |
}); | |
}, | |
}); | |
type UserListPageState = { | |
loading: boolean; | |
}; | |
const userListPage = createSlice({ | |
name: 'userListPage', | |
initialState: {} as UserListPageState, | |
reducers: {}, | |
extraReducers: (builder) => { | |
builder.addCase(loadUser.pending, (state) => {state.loading = true}); | |
builder.addCase(loadUser.fulfilled, (state) => {state.loading = false}); | |
builder.addCase(loadUser.rejected, (state) => {state.loading = false}); | |
}, | |
}); | |
export default combineReducers({ | |
users: userSlice.reducer, | |
userListPage: userListPage.reducer | |
}) | |
export const userSelectors = userAdapter.getSelectors((s: AppState) => s.users.userList) |
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
import { userSelectors } from './users' | |
import { AppState } from './rootReducer' | |
const Users: React.FC = () => { | |
const dispatch = useDispatch(); | |
useEffect(() => { | |
dispatch(loadUser({})); | |
}, [dispatch]); | |
return <UserList />; | |
}; | |
const UserList: React.FC = () => { | |
const users = useSelector(userSelectors.selectAll); | |
return ( | |
<> | |
{users.map((t) => ( | |
<span>{t.displayName}</span> | |
))} | |
</> | |
); | |
}; | |
export default Users; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment