-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathstore.ts
131 lines (118 loc) · 3.08 KB
/
store.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import type { Reducer } from 'react';
import {
call,
put,
delay,
takeLatest,
takeEvery,
all,
} from 'redux-saga/effects';
import useSagaReducer from 'use-saga-reducer';
import { createContainer } from 'react-tracked';
type State = {
firstName: string;
loadingState: 'idle' | 'connecting' | 'fetching';
count: number;
};
const initialState: State = {
firstName: '',
loadingState: 'idle',
count: 0,
};
type InnerAction =
| { type: 'START_FETCH_USER' }
| { type: 'CONTINUE_FETCH_USER' }
| { type: 'FINISH_FETCH_USER'; firstName: string }
| { type: 'ERROR_FETCH_USER' }
| { type: 'DECREMENT' };
type OuterAction = { type: 'CLEAR_USER_NAME' } | { type: 'INCREMENT' };
const reducer: Reducer<State, InnerAction | OuterAction> = (state, action) => {
switch (action.type) {
case 'START_FETCH_USER':
return {
...state,
loadingState: 'connecting',
};
case 'CONTINUE_FETCH_USER':
return {
...state,
loadingState: 'fetching',
};
case 'FINISH_FETCH_USER':
return {
...state,
loadingState: 'idle',
firstName: action.firstName,
};
case 'ERROR_FETCH_USER':
return {
...state,
loadingState: 'idle',
};
case 'CLEAR_USER_NAME':
return {
...state,
firstName: '',
};
case 'INCREMENT':
return {
...state,
count: state.count + 1,
};
case 'DECREMENT':
return {
...state,
count: state.count - 1,
};
default:
return state; // needs this for AsyncAction
}
};
type AsyncActionFetch = { type: 'FETCH_USER'; id: number };
type AsyncActionDecrement = { type: 'DELAYED_DECREMENT' };
type AsyncAction = AsyncActionFetch | AsyncActionDecrement;
function* userFetcher(action: AsyncActionFetch) {
try {
yield put<InnerAction>({ type: 'START_FETCH_USER' });
const response: Response = yield call(() =>
fetch(`https://reqres.in/api/users/${action.id}?delay=1`),
);
yield put<InnerAction>({ type: 'CONTINUE_FETCH_USER' });
const data: { data: Record<string, unknown> } = yield call(() =>
response.json(),
);
yield delay(500);
const firstName = data.data.first_name;
if (typeof firstName !== 'string') throw new Error();
yield put<InnerAction>({ type: 'FINISH_FETCH_USER', firstName });
} catch {
yield put<InnerAction>({ type: 'ERROR_FETCH_USER' });
}
}
function* delayedDecrementer() {
yield delay(500);
yield put<InnerAction>({ type: 'DECREMENT' });
}
function* userFetchingSaga() {
yield takeLatest<AsyncActionFetch>('FETCH_USER', userFetcher);
}
function* delayedDecrementingSaga() {
yield takeEvery<AsyncActionDecrement>(
'DELAYED_DECREMENT',
delayedDecrementer,
);
}
function* rootSaga() {
yield all([userFetchingSaga(), delayedDecrementingSaga()]);
}
const useValue = () =>
useSagaReducer(
rootSaga,
reducer as Reducer<State, AsyncAction | OuterAction>,
initialState,
);
export const {
Provider,
useTrackedState,
useUpdate: useDispatch,
} = createContainer(useValue, { concurrentMode: true });