This repository was archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseRestApiRunner.tsx
113 lines (101 loc) · 2.98 KB
/
useRestApiRunner.tsx
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
import { useState, useCallback } from 'react';
import {
REQUEST_POLLING_CANCELLED, NotificationMapper, ErrorType, AbstractRequestApi,
} from '@fpsak-frontend/rest-api-new';
import useRestApiErrorDispatcher from '../error/useRestApiErrorDispatcher';
import RestApiState from '../RestApiState';
interface RestApiData<T> {
startRequest: (params?: any, keepData?: boolean) => Promise<T>;
resetRequestData: () => void;
state: RestApiState;
error?: ErrorType;
data?: T;
cancelRequest: () => void;
}
/**
* For mocking i unit-test
*/
export const getUseRestApiRunnerMock = (requestApi: AbstractRequestApi) => function useRestApiRunner<T>(key: string):RestApiData<T> {
const [data, setData] = useState({
state: RestApiState.NOT_STARTED,
data: undefined,
error: undefined,
});
const startRequest = (params: any = {}):Promise<T> => {
setData({
state: RestApiState.SUCCESS,
data: requestApi.startRequest(key, params),
error: undefined,
});
return Promise.resolve(data, params);
};
return {
startRequest,
resetRequestData: () => undefined,
cancelRequest: () => undefined,
...data,
};
};
/**
* Hook som gir deg ein funksjon til å starte restkall, i tillegg til kallets status/resultat/feil
*/
const getUseRestApiRunner = (requestApi: AbstractRequestApi) => function useRestApiRunner<T>(key: string):RestApiData<T> {
const [data, setData] = useState({
state: RestApiState.NOT_STARTED,
data: undefined,
error: undefined,
});
const { addErrorMessage } = useRestApiErrorDispatcher();
const notif = new NotificationMapper();
notif.addRequestErrorEventHandlers((errorData, type) => {
addErrorMessage({ ...errorData, type });
});
const startRequest = useCallback((params: any = {}, keepData = false):Promise<T> => {
if (requestApi.hasPath(key)) {
setData((oldState) => ({
state: RestApiState.LOADING,
data: keepData ? oldState.data : undefined,
error: undefined,
}));
return requestApi.startRequest(key, params, notif)
.then((dataRes) => {
if (dataRes.payload !== REQUEST_POLLING_CANCELLED) {
setData({
state: RestApiState.SUCCESS,
data: dataRes.payload,
error: undefined,
});
}
return Promise.resolve(dataRes.payload);
})
.catch((error) => {
setData({
state: RestApiState.ERROR,
data: undefined,
error,
});
throw error;
});
}
setData({
state: RestApiState.NOT_STARTED,
error: undefined,
data: undefined,
});
return undefined;
}, []);
const resetRequestData = useCallback(() => {
setData({
state: RestApiState.NOT_STARTED,
data: undefined,
error: undefined,
});
}, []);
return {
startRequest,
resetRequestData,
cancelRequest: () => requestApi.cancelRequest(key),
...data,
};
};
export default getUseRestApiRunner;