Redux toolkit (#115)
* add @redux/toolkit * bump react version to allow for hooks * add remote-redux devtools to help track extension state * add remote-redux server to allow for remote state viewing * setup react-redux for current user * setup watch mode for running redux remote dev watch to options * move screenshots
This commit is contained in:
37
src/reducers/currentUser.ts
Normal file
37
src/reducers/currentUser.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
|
||||
import axios, { AxiosResponse } from 'axios';
|
||||
import { User, UserPayload } from '../types/user';
|
||||
import config from '../config/config';
|
||||
|
||||
type NameType = 'currentUser';
|
||||
export const name: NameType = 'currentUser';
|
||||
|
||||
export const fetchCurrentUser = createAsyncThunk<User, undefined>(`[${name}]`, async () => {
|
||||
const userPayload: AxiosResponse<UserPayload> = await axios.get(config.currentUserApiUrl);
|
||||
return userPayload.data.data;
|
||||
});
|
||||
|
||||
export interface CurrentUser {
|
||||
error?: unknown;
|
||||
pending?: boolean;
|
||||
user?: User;
|
||||
}
|
||||
export const initialState: CurrentUser = {};
|
||||
|
||||
const currentUser = createSlice({
|
||||
extraReducers: (builder) => {
|
||||
builder.addCase(fetchCurrentUser.fulfilled, (state, { payload }) => {
|
||||
state.user = payload;
|
||||
});
|
||||
builder.addCase(fetchCurrentUser.rejected, (state, { error }) => {
|
||||
state.user = undefined;
|
||||
state.error = error;
|
||||
});
|
||||
},
|
||||
initialState,
|
||||
name,
|
||||
reducers: {},
|
||||
});
|
||||
|
||||
export const actions = currentUser.actions;
|
||||
export default currentUser.reducer;
|
||||
Reference in New Issue
Block a user