-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathaccountSlice.js
51 lines (45 loc) · 1.34 KB
/
accountSlice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import axios from 'axios'
export const getUser = createAsyncThunk(
'account/getUser',
async (userId, thunkAPI) => {
await new Promise((resolve, rejected) => {
setTimeout(() => {
resolve()
}, 5000);
})
const { data } = await axios(`http://localhost:8080/accounts/${userId}`);
return data;
}
)
const initialState = {
amount: 1,
pending: false
}
export const accountSlice = createSlice({
name: 'account',
initialState,
reducers: {
increment: (state) => {
state.amount += 1
},
decrement: (state) => {
state.amount -= 1
},
incrementByAmount: (state, action) => {
state.amount += action.payload
}
},
extraReducers: (builder) => {
builder.addCase(getUser.pending, (state, action) => {
state.pending = true;
}).addCase(getUser.fulfilled, (state, action) => {
state.pending = false;
}).addCase(getUser.rejected, (state, action) => {
state.pending = false;
})
}
})
// Action creators are generated for each case reducer function
export const { increment, decrement, incrementByAmount } = accountSlice.actions
export default accountSlice.reducer