actions.tpl 5.2 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...

Florent Chehab's avatar
Florent Chehab committed
7
8
import Cookies from 'js-cookie';

9
import {
10
11
12
13
14
{% for obj in data %}
    {{obj.NAME}}_HAS_ERROR,
    {{obj.NAME}}_IS_LOADING,
    {{obj.NAME}}_FETCH_DATA_SUCCESS,
    {{obj.NAME}}_INVALIDATED,
Florent Chehab's avatar
Florent Chehab committed
15
16
17

    {{obj.NAME}}_EL_INVALIDATED,
    {{obj.NAME}}_EL_HAS_ERROR,
Florent Chehab's avatar
Florent Chehab committed
18
19
20
    {{obj.NAME}}_EL_IS_LOADING,
    {{obj.NAME}}_EL_FETCH_DATA_SUCCESS,

Florent Chehab's avatar
Florent Chehab committed
21
    {% if not obj.read_only %}
Florent Chehab's avatar
Florent Chehab committed
22
23
    {{obj.NAME}}_EL_IS_SAVING,
    {{obj.NAME}}_EL_SAVING_DATA_SUCCESS,
Florent Chehab's avatar
Florent Chehab committed
24
    {% endif %}
Florent Chehab's avatar
Florent Chehab committed
25

26
{% endfor %}
27
28
} from "./action-types";

Florent Chehab's avatar
Florent Chehab committed
29
30
31
//////////////////////////////////
// generic function definitions

32
function _FetchData(pk, api_end_point, _IsLoading, _FetchDataSuccess, _Invalidated, _HasError, pk_required=false) {
Florent Chehab's avatar
Florent Chehab committed
33
34
35
36
37
    if (pk_required && pk == ""){
        throw "pk shouldn't be empty when requesting a specific element";
    }
    return (dispatch) => {
        dispatch(_IsLoading(true));
38
39
        let token = Cookies.get('csrftoken');
        fetch(api_end_point, {credentials: 'same-origin',headers: {'X-CSRFToken': token}})
Florent Chehab's avatar
Florent Chehab committed
40
41
42
43
44
45
46
47
48
49
            .then((response) => {
                if (!response.ok) {
                    throw Error(response.statusText);
                }
                return response;
            })
            .then((response) => response.json())
            .then((obj) => {
                dispatch(_Invalidated(false));
                dispatch(_FetchDataSuccess(obj));
50
                dispatch(_IsLoading(false));
Florent Chehab's avatar
Florent Chehab committed
51
52
53
54
55
56
57
            })
            .catch(() => dispatch(_HasError(true)));
    };
}



58
function _ElSaveData(data, api_end_point, _IsLoading, _FetchDataSuccess, _Invalidated, _HasError) {
Florent Chehab's avatar
Florent Chehab committed
59
60
61
62
63
64
65
66
67
68
    return (dispatch) => {
        let method = "POST";
        let pk = "";
        if ('id' in data){
            method = "PUT";
            pk = data.id;
        }

        dispatch(_ElIsSaving(true));
        let token = Cookies.get('csrftoken');
69
        fetch(api_end_point+pk, {
Florent Chehab's avatar
Florent Chehab committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
            method: method,
            credentials: 'same-origin',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'X-CSRFToken': token
            },
            body: JSON.stringify(data)
        })
        .then((response) => {
            if (!response.ok) {
                throw Error(response.statusText);
            }

            return response;
        })
        .then((response) => response.json())
        .then((_El) => {
            dispatch(_ElInvalidated(false));
            dispatch(_ElSaveDataSuccess(_El));
90
            dispatch(_ElIsSaving(false));
Florent Chehab's avatar
Florent Chehab committed
91
92
93
94
95
96
97
        })
        .catch(() => dispatch(_ElHasError(true)));
    };
}

/////////////////////////////////

98
99
{% for obj in data %}
export function {{obj.name}}HasError(bool) {
100
    return {
101
        type: {{obj.NAME}}_HAS_ERROR,
102
103
104
105
        hasError: bool
    };
}

106
export function {{obj.name}}IsLoading(bool) {
107
    return {
108
        type: {{obj.NAME}}_IS_LOADING,
109
110
111
112
        isLoading: bool
    };
}

113
export function {{obj.name}}Invalidated(bool) {
114
    return {
115
        type: {{obj.NAME}}_INVALIDATED,
116
117
118
119
        invalidated: bool
    };
}

120
121
export function {{obj.name}}FetchDataSuccess({{obj.name}}) {
    {{obj.name}}Invalidated(false)
122
123

    return {
124
125
126
        type: {{obj.NAME}}_FETCH_DATA_SUCCESS,
        {{obj.name}},
        {{obj.name}}FetchedAt: Date.now()
127
128
129
130
    };
}


Florent Chehab's avatar
Florent Chehab committed
131
132
133
export function {{obj.name}}FetchData() {
    return _FetchData(
        "", 
134
        "{{obj.api_end_point}}", 
Florent Chehab's avatar
Florent Chehab committed
135
136
137
138
139
        {{obj.name}}IsLoading, 
        {{obj.name}}FetchDataSuccess, 
        {{obj.name}}Invalidated, 
        {{obj.name}}HasError
    )
140
}
Florent Chehab's avatar
Florent Chehab committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156


export function {{obj.name}}ElHasError(bool) {
    return {
        type: {{obj.NAME}}_EL_HAS_ERROR,
        hasError: bool
    };
}

export function {{obj.name}}ElInvalidated(bool) {
    return {
        type: {{obj.NAME}}_EL_INVALIDATED,
        invalidated: bool
    };
}

Florent Chehab's avatar
Florent Chehab committed
157
export function {{obj.name}}ElIsLoading(bool) {
Florent Chehab's avatar
Florent Chehab committed
158
    return {
Florent Chehab's avatar
Florent Chehab committed
159
160
        type: {{obj.NAME}}_EL_IS_LOADING,
        isLoading: bool
Florent Chehab's avatar
Florent Chehab committed
161
162
163
164
    };
}


Florent Chehab's avatar
Florent Chehab committed
165
export function {{obj.name}}ElFetchDataSuccess({{obj.name}}El) {
Florent Chehab's avatar
Florent Chehab committed
166
167
168
    {{obj.name}}ElInvalidated(false)

    return {
Florent Chehab's avatar
Florent Chehab committed
169
        type: {{obj.NAME}}_EL_FETCH_DATA_SUCCESS,
Florent Chehab's avatar
Florent Chehab committed
170
        {{obj.name}}El,
Florent Chehab's avatar
Florent Chehab committed
171
        {{obj.name}}ElFetchedAt: Date.now()
Florent Chehab's avatar
Florent Chehab committed
172
173
174
175
    };
}


Florent Chehab's avatar
Florent Chehab committed
176
177
178
export function {{obj.name}}ElFetchData(pk) {
    return _FetchData(
        pk, 
179
        "{{obj.api_end_point}}", 
Florent Chehab's avatar
Florent Chehab committed
180
181
182
183
184
185
186
        {{obj.name}}ElIsLoading, 
        {{obj.name}}ElFetchDataSuccess, 
        {{obj.name}}ElInvalidated, 
        {{obj.name}}ElHasError,
        true
    )
}
Florent Chehab's avatar
Florent Chehab committed
187
188


Florent Chehab's avatar
Florent Chehab committed
189
{% if not obj.read_only %}
Florent Chehab's avatar
Florent Chehab committed
190

Florent Chehab's avatar
Florent Chehab committed
191
192
193
194
export function {{obj.name}}ElIsSaving(bool) {
    return {
        type: {{obj.NAME}}_EL_IS_SAVING,
        isSaving: bool
Florent Chehab's avatar
Florent Chehab committed
195
196
197
198
    };
}


Florent Chehab's avatar
Florent Chehab committed
199
200
export function {{obj.name}}ElSaveDataSuccess({{obj.name}}El) {
    {{obj.name}}ElInvalidated(false)
Florent Chehab's avatar
Florent Chehab committed
201

Florent Chehab's avatar
Florent Chehab committed
202
203
204
205
    return {
        type: {{obj.NAME}}_EL_SAVING_DATA_SUCCESS,
        {{obj.name}}El,
        {{obj.name}}ElSavedAt: Date.now()
Florent Chehab's avatar
Florent Chehab committed
206
207
208
    };
}

Florent Chehab's avatar
Florent Chehab committed
209
export function {{obj.name}}ElSaveData(data) {
210
    return  _ElSaveData(data, "{{obj.api_end_point}}", {{obj.name}}ElIsLoading, {{obj.name}}ElFetchDataSuccess, {{obj.name}}ElInvalidated, {{obj.name}}ElHasError) 
Florent Chehab's avatar
Florent Chehab committed
211
212
213
}
{% endif %}

214
{% endfor %}