-
Notifications
You must be signed in to change notification settings - Fork 33
/
thunks.ts
25 lines (24 loc) · 953 Bytes
/
thunks.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
import { ThunkAction } from 'redux-thunk';
import { RootState } from '..';
import { GithubAction } from './types';
import { getUserProfile } from '../../api/github';
import { getUserProfileAsync } from './actions';
// ThunkAction 의 Generics 에는 다음 값들을 순서대로 넣어줍니다.
/*
1. thunk 함수에서 반환하는 값의 타입
2. 리덕스 스토어의 상태 타입
3. Extra Argument (https://github.com/reduxjs/redux-thunk#injecting-a-custom-argument)
4. thunk 함수 내부에서 디스패치 할 수 있는 액션들의 타입
*/
export function getUserProfileThunk(username: string): ThunkAction<void, RootState, null, GithubAction> {
return async dispatch => {
const { request, success, failure } = getUserProfileAsync;
dispatch(request());
try {
const userProfile = await getUserProfile(username);
dispatch(success(userProfile));
} catch (e) {
dispatch(failure(e));
}
};
}