profile.js
1.35 KB
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
52
53
54
55
56
57
58
import { createAction, handleActions } from 'redux-actions';
import createRequestSaga, {
createRequestActionTypes,
} from '../lib/createRequestSaga';
import produce from 'immer';
import * as profileAPI from '../lib/api/profile';
import { takeLatest } from 'redux-saga/effects';
const INITIALIZE = 'profile/INITIALIZE';
const CHANGE_FIELD = 'profile/CHANGE_FIELD';
const [SET_BJID, SET_BJID_SUCCESS, SET_BJID_FAILURE] = createRequestActionTypes(
'profile/SET_BJID',
);
export const setBJID = createAction(SET_BJID, ({ username, BJID }) => ({
username,
BJID,
}));
export const changeField = createAction(CHANGE_FIELD, ({ key, value }) => ({
key,
value,
}));
const initialState = {
username: '',
userBJID: '',
solvedBJ: '',
friendList: [],
BJIDError: '',
};
const setBJIDSaga = createRequestSaga(SET_BJID, profileAPI.setBJID);
export function* profileSaga() {
yield takeLatest(SET_BJID, setBJIDSaga);
}
export default handleActions(
{
[INITIALIZE]: (state) => initialState,
[CHANGE_FIELD]: (state, { payload: { key, value } }) =>
produce(state, (draft) => {
draft[key] = value;
}),
[SET_BJID_SUCCESS]: (state, { payload: BJID }) => ({
...state,
BJID,
BJIDError: null,
}),
[SET_BJID_FAILURE]: (state, { payload: error }) => ({
...state,
BJIDError: error,
}),
},
initialState,
);