actions.tpl 1.71 KB
Newer Older
1
2
3
4
5
6
// WARNING
// THIS FILE HAS BEEN AUTOMATICALLY GENERATED
// WITH /frontend/generate/generate_frontend_files.js
// MODIFY THE FILE ABOVE IF YOUR NOT SATISFIED 
// THIS WARNING DOESN'T APPLY TO .tpl FILES...

7
import {
8
9
10
11
12
13
{% for obj in data %}
    {{obj.NAME}}_HAS_ERROR,
    {{obj.NAME}}_IS_LOADING,
    {{obj.NAME}}_FETCH_DATA_SUCCESS,
    {{obj.NAME}}_INVALIDATED,
{% endfor %}
14
15
} from "./action-types";

16
17
{% for obj in data %}
export function {{obj.name}}HasError(bool) {
18
    return {
19
        type: {{obj.NAME}}_HAS_ERROR,
20
21
22
23
        hasError: bool
    };
}

24
export function {{obj.name}}IsLoading(bool) {
25
    return {
26
        type: {{obj.NAME}}_IS_LOADING,
27
28
29
30
        isLoading: bool
    };
}

31
export function {{obj.name}}Invalidated(bool) {
32
    return {
33
        type: {{obj.NAME}}_INVALIDATED,
34
35
36
37
        invalidated: bool
    };
}

38
39
export function {{obj.name}}FetchDataSuccess({{obj.name}}) {
    {{obj.name}}Invalidated(false)
40
41

    return {
42
43
44
        type: {{obj.NAME}}_FETCH_DATA_SUCCESS,
        {{obj.name}},
        {{obj.name}}FetchedAt: Date.now()
45
46
47
48
    };
}


49
export function {{obj.name}}FetchData(url) {
50
    return (dispatch) => {
51
        dispatch({{obj.name}}IsLoading(true));
52
53
54
55
56
57
58

        fetch(url)
            .then((response) => {
                if (!response.ok) {
                    throw Error(response.statusText);
                }

59
                dispatch({{obj.name}}IsLoading(false));
60
61
62
63

                return response;
            })
            .then((response) => response.json())
64
65
66
            .then(({{obj.name}}) => {
                dispatch({{obj.name}}Invalidated(false));
                dispatch({{obj.name}}FetchDataSuccess({{obj.name}}));
67
            })
68
            .catch(() => dispatch({{obj.name}}HasError(true)));
69
    };
70
71
}
{% endfor %}