actions.tpl 5.53 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
74
75
        }

        dispatch(_ElIsSaving(true));
        let token = Cookies.get('csrftoken');
76
        fetch(api_end_point+pk, {
Florent Chehab's avatar
Florent Chehab committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
            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));
96
            dispatch(_ElFetchDataSuccess(_El)); // we use the same here
97
            dispatch(_ElIsSaving(false));
Florent Chehab's avatar
Florent Chehab committed
98
        })
99
100
101
102
        .catch((e) => {
            dispatch(_ElHasError(true, e))
            dispatch(_ElIsSaving(false));
        });
Florent Chehab's avatar
Florent Chehab committed
103
104
105
106
107
    };
}

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

108
{% for obj in data %}
109
export function {{obj.name}}FetchHasError(bool, error=null) {
110
    return {
111
112
113
        type: {{obj.NAME}}_FETCH_HAS_ERROR,
        hasError: bool,
        error
114
115
116
    };
}

117
export function {{obj.name}}IsLoading(bool) {
118
    return {
119
        type: {{obj.NAME}}_IS_LOADING,
120
121
122
123
        isLoading: bool
    };
}

124
export function {{obj.name}}Invalidated(bool) {
125
    return {
126
        type: {{obj.NAME}}_INVALIDATED,
127
128
129
130
        invalidated: bool
    };
}

131
132
export function {{obj.name}}FetchDataSuccess({{obj.name}}) {
    {{obj.name}}Invalidated(false)
133
134

    return {
135
136
137
        type: {{obj.NAME}}_FETCH_DATA_SUCCESS,
        {{obj.name}},
        {{obj.name}}FetchedAt: Date.now()
138
139
140
141
    };
}


142
export function {{obj.name}}FetchData(pk="") {
Florent Chehab's avatar
Florent Chehab committed
143
    return _FetchData(
144
        pk, 
145
        "{{obj.api_end_point}}", 
Florent Chehab's avatar
Florent Chehab committed
146
147
148
        {{obj.name}}IsLoading, 
        {{obj.name}}FetchDataSuccess, 
        {{obj.name}}Invalidated, 
149
        {{obj.name}}FetchHasError
Florent Chehab's avatar
Florent Chehab committed
150
    )
151
}
Florent Chehab's avatar
Florent Chehab committed
152
153


154
export function {{obj.name}}ElFetchHasError(bool, error) {
Florent Chehab's avatar
Florent Chehab committed
155
    return {
156
157
158
        type: {{obj.NAME}}_EL_FETCH_HAS_ERROR,
        hasError: bool,
        error
Florent Chehab's avatar
Florent Chehab committed
159
160
161
162
163
164
165
166
167
168
    };
}

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

Florent Chehab's avatar
Florent Chehab committed
169
export function {{obj.name}}ElIsLoading(bool) {
Florent Chehab's avatar
Florent Chehab committed
170
    return {
Florent Chehab's avatar
Florent Chehab committed
171
172
        type: {{obj.NAME}}_EL_IS_LOADING,
        isLoading: bool
Florent Chehab's avatar
Florent Chehab committed
173
174
175
176
    };
}


Florent Chehab's avatar
Florent Chehab committed
177
export function {{obj.name}}ElFetchDataSuccess({{obj.name}}El) {
Florent Chehab's avatar
Florent Chehab committed
178
179
180
    {{obj.name}}ElInvalidated(false)

    return {
Florent Chehab's avatar
Florent Chehab committed
181
        type: {{obj.NAME}}_EL_FETCH_DATA_SUCCESS,
Florent Chehab's avatar
Florent Chehab committed
182
        {{obj.name}}El,
Florent Chehab's avatar
Florent Chehab committed
183
        {{obj.name}}ElFetchedAt: Date.now()
Florent Chehab's avatar
Florent Chehab committed
184
185
186
187
    };
}


Florent Chehab's avatar
Florent Chehab committed
188
189
190
export function {{obj.name}}ElFetchData(pk) {
    return _FetchData(
        pk, 
191
        "{{obj.api_end_point}}", 
Florent Chehab's avatar
Florent Chehab committed
192
193
194
        {{obj.name}}ElIsLoading, 
        {{obj.name}}ElFetchDataSuccess, 
        {{obj.name}}ElInvalidated, 
195
        {{obj.name}}ElFetchHasError,
Florent Chehab's avatar
Florent Chehab committed
196
197
198
        true
    )
}
Florent Chehab's avatar
Florent Chehab committed
199
200


Florent Chehab's avatar
Florent Chehab committed
201
{% if not obj.read_only %}
Florent Chehab's avatar
Florent Chehab committed
202

Florent Chehab's avatar
Florent Chehab committed
203
204
205
206
export function {{obj.name}}ElIsSaving(bool) {
    return {
        type: {{obj.NAME}}_EL_IS_SAVING,
        isSaving: bool
Florent Chehab's avatar
Florent Chehab committed
207
208
209
    };
}

210
export function {{obj.name}}ElSavingHasError(bool, error=null) {
Florent Chehab's avatar
Florent Chehab committed
211
    return {
212
213
214
        type: {{obj.NAME}}_EL_SAVING_HAS_ERROR,
        hasError: bool,
        error
Florent Chehab's avatar
Florent Chehab committed
215
216
217
    };
}

218

Florent Chehab's avatar
Florent Chehab committed
219
export function {{obj.name}}ElSaveData(data) {
220
    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
221
222
223
}
{% endif %}

224
{% endfor %}