actions.tpl 5.91 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
{% for obj in data %}
11
    {{obj.NAME}}_FETCH_HAS_ERROR,
12
13
14
    {{obj.NAME}}_IS_LOADING,
    {{obj.NAME}}_FETCH_DATA_SUCCESS,
    {{obj.NAME}}_INVALIDATED,
Florent Chehab's avatar
Florent Chehab committed
15
16

    {{obj.NAME}}_EL_INVALIDATED,
17
    {{obj.NAME}}_EL_FETCH_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
    {{obj.NAME}}_EL_IS_SAVING,
23
    {{obj.NAME}}_EL_SAVING_HAS_ERROR,
Florent Chehab's avatar
Florent Chehab committed
24
    {{obj.NAME}}_EL_SAVING_DATA_SUCCESS,
Florent Chehab's avatar
Florent Chehab committed
25
    {% endif %}
Florent Chehab's avatar
Florent Chehab committed
26

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

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

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



65
function _ElSaveData(data, api_end_point, _ElIsSaving, _ElFetchDataSuccess, _ElInvalidated, _ElHasError) {
Florent Chehab's avatar
Florent Chehab committed
66
67
68
69
70
    return (dispatch) => {
        let method = "POST";
        let pk = "";
        if ('id' in data){
            method = "PUT";
71
            pk = data.id + '/';
Florent Chehab's avatar
Florent Chehab committed
72
        }
73
        let errorStatusText = '';
Florent Chehab's avatar
Florent Chehab committed
74
75
76

        dispatch(_ElIsSaving(true));
        let token = Cookies.get('csrftoken');
77
        let f = fetch(api_end_point+pk, {
Florent Chehab's avatar
Florent Chehab committed
78
79
80
81
82
83
84
85
86
87
88
            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) {
89
90
                errorStatusText = response.statusText;
                throw response;
Florent Chehab's avatar
Florent Chehab committed
91
            }
92
            return response.json();
Florent Chehab's avatar
Florent Chehab committed
93
94
95
        })
        .then((_El) => {
            dispatch(_ElInvalidated(false));
96
            dispatch(_ElFetchDataSuccess(_El)); // we use the same here
97
            dispatch(_ElIsSaving(false));
Florent Chehab's avatar
Florent Chehab committed
98
        })
99
        .catch((e) => {
100
101
102
103
104
105
106
107
108
109
110
111
            if (typeof e.json == 'function'){
                return e.json()
            } else {
                return new Promise(function(resolve, reject) {resolve(e)});
            }
        })
        .then( (errorContent) => {
            if (typeof errorContent != 'undefined'){
                dispatch(_ElHasError(true, {message: errorStatusText, content: errorContent}));
                dispatch(_ElIsSaving(false));
            }
        })
Florent Chehab's avatar
Florent Chehab committed
112
113
114
115
116
    };
}

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

117
{% for obj in data %}
118
export function {{obj.name}}FetchHasError(bool, error=null) {
119
    return {
120
121
122
        type: {{obj.NAME}}_FETCH_HAS_ERROR,
        hasError: bool,
        error
123
124
125
    };
}

126
export function {{obj.name}}IsLoading(bool) {
127
    return {
128
        type: {{obj.NAME}}_IS_LOADING,
129
130
131
132
        isLoading: bool
    };
}

133
export function {{obj.name}}Invalidated(bool) {
134
    return {
135
        type: {{obj.NAME}}_INVALIDATED,
136
137
138
139
        invalidated: bool
    };
}

140
141
export function {{obj.name}}FetchDataSuccess({{obj.name}}) {
    {{obj.name}}Invalidated(false)
142
143

    return {
144
145
146
        type: {{obj.NAME}}_FETCH_DATA_SUCCESS,
        {{obj.name}},
        {{obj.name}}FetchedAt: Date.now()
147
148
149
150
    };
}


151
export function {{obj.name}}FetchData(pk="") {
Florent Chehab's avatar
Florent Chehab committed
152
    return _FetchData(
153
        pk, 
154
        "{{obj.api_end_point}}", 
Florent Chehab's avatar
Florent Chehab committed
155
156
157
        {{obj.name}}IsLoading, 
        {{obj.name}}FetchDataSuccess, 
        {{obj.name}}Invalidated, 
158
        {{obj.name}}FetchHasError
Florent Chehab's avatar
Florent Chehab committed
159
    )
160
}
Florent Chehab's avatar
Florent Chehab committed
161
162


163
export function {{obj.name}}ElFetchHasError(bool, error) {
Florent Chehab's avatar
Florent Chehab committed
164
    return {
165
166
167
        type: {{obj.NAME}}_EL_FETCH_HAS_ERROR,
        hasError: bool,
        error
Florent Chehab's avatar
Florent Chehab committed
168
169
170
171
172
173
174
175
176
177
    };
}

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

Florent Chehab's avatar
Florent Chehab committed
178
export function {{obj.name}}ElIsLoading(bool) {
Florent Chehab's avatar
Florent Chehab committed
179
    return {
Florent Chehab's avatar
Florent Chehab committed
180
181
        type: {{obj.NAME}}_EL_IS_LOADING,
        isLoading: bool
Florent Chehab's avatar
Florent Chehab committed
182
183
184
185
    };
}


Florent Chehab's avatar
Florent Chehab committed
186
export function {{obj.name}}ElFetchDataSuccess({{obj.name}}El) {
Florent Chehab's avatar
Florent Chehab committed
187
188
189
    {{obj.name}}ElInvalidated(false)

    return {
Florent Chehab's avatar
Florent Chehab committed
190
        type: {{obj.NAME}}_EL_FETCH_DATA_SUCCESS,
Florent Chehab's avatar
Florent Chehab committed
191
        {{obj.name}}El,
Florent Chehab's avatar
Florent Chehab committed
192
        {{obj.name}}ElFetchedAt: Date.now()
Florent Chehab's avatar
Florent Chehab committed
193
194
195
196
    };
}


Florent Chehab's avatar
Florent Chehab committed
197
198
199
export function {{obj.name}}ElFetchData(pk) {
    return _FetchData(
        pk, 
200
        "{{obj.api_end_point}}", 
Florent Chehab's avatar
Florent Chehab committed
201
202
203
        {{obj.name}}ElIsLoading, 
        {{obj.name}}ElFetchDataSuccess, 
        {{obj.name}}ElInvalidated, 
204
        {{obj.name}}ElFetchHasError,
Florent Chehab's avatar
Florent Chehab committed
205
206
207
        true
    )
}
Florent Chehab's avatar
Florent Chehab committed
208
209


Florent Chehab's avatar
Florent Chehab committed
210
{% if not obj.read_only %}
Florent Chehab's avatar
Florent Chehab committed
211

Florent Chehab's avatar
Florent Chehab committed
212
213
214
215
export function {{obj.name}}ElIsSaving(bool) {
    return {
        type: {{obj.NAME}}_EL_IS_SAVING,
        isSaving: bool
Florent Chehab's avatar
Florent Chehab committed
216
217
218
    };
}

Florent Chehab's avatar
Florent Chehab committed
219
export function {{obj.name}}ElSavingHasError(b, error=null) {
Florent Chehab's avatar
Florent Chehab committed
220
    return {
221
        type: {{obj.NAME}}_EL_SAVING_HAS_ERROR,
Florent Chehab's avatar
Florent Chehab committed
222
        hasError: b,
223
        error
Florent Chehab's avatar
Florent Chehab committed
224
225
226
    };
}

227

Florent Chehab's avatar
Florent Chehab committed
228
export function {{obj.name}}ElSaveData(data) {
229
    return  _ElSaveData(data, "{{obj.api_end_point}}", {{obj.name}}ElIsSaving, {{obj.name}}ElFetchDataSuccess, {{obj.name}}ElInvalidated, {{obj.name}}ElSavingHasError) 
Florent Chehab's avatar
Florent Chehab committed
230
231
232
}
{% endif %}

233
{% endfor %}