-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
41 lines (36 loc) · 874 Bytes
/
helpers.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
// @flow
const FETCH_SUCCESSFUL = "__FETCHER__FETCH_SUCCESSFUL";
// function is binded
export function fetchData(endpoint: string) {
fetch(endpoint)
.then(response => {
let contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
return response.json();
} else {
throw new Error("REDUX FETCH HOC: Response is not a JSON!");
}
})
.then(data => {
this.props.dispatch({
type: FETCH_SUCCESSFUL,
endpoint,
payload: data
});
this.setState(successState());
})
.catch(() => this.setState(errorState()));
}
function successState() {
return {
isLoading: false,
isSuccess: true
};
}
function errorState() {
return {
isSuccess: false,
isLoading: false
};
}
export let voidFunction = () => ({});