actions.tpl 1.55 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
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
import {
    {{NAME}}_HAS_ERROR,
    {{NAME}}_IS_LOADING,
    {{NAME}}_FETCH_DATA_SUCCESS,
    {{NAME}}_INVALIDATED
} from "./action-types";


export function {{name}}HasError(bool) {
    return {
        type: {{NAME}}_HAS_ERROR,
        hasError: bool
    };
}

export function {{name}}IsLoading(bool) {
    return {
        type: {{NAME}}_IS_LOADING,
        isLoading: bool
    };
}

export function {{name}}Invalidated(bool) {
    return {
        type: {{NAME}}_INVALIDATED,
        invalidated: bool
    };
}

export function {{name}}FetchDataSuccess({{name}}) {
    {{name}}Invalidated(false)

    return {
        type: {{NAME}}_FETCH_DATA_SUCCESS,
        {{name}},
        {{name}}FetchedAt: Date.now()
    };
}


export function {{name}}FetchData(url) {
    return (dispatch) => {
        dispatch({{name}}IsLoading(true));

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

                dispatch({{name}}IsLoading(false));

                return response;
            })
            .then((response) => response.json())
            .then(({{name}}) => {
                dispatch({{name}}Invalidated(false));
                dispatch({{name}}FetchDataSuccess({{name}}));
            })
            .catch(() => dispatch({{name}}HasError(true)));
    };
}