actions.tpl 5.11 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
21
    {{obj.NAME}}_EL_IS_LOADING,
    {{obj.NAME}}_EL_FETCH_DATA_SUCCESS,

    {% if 'read_only' not in obj or 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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//////////////////////////////////
// generic function definitions

function _FetchData(pk, api_url, _IsLoading, _FetchDataSuccess, _Invalidated, _HasError, pk_required=false) {
    if (pk_required && pk == ""){
        throw "pk shouldn't be empty when requesting a specific element";
    }
    return (dispatch) => {
        dispatch(_IsLoading(true));

        fetch(api_url)
            .then((response) => {
                if (!response.ok) {
                    throw Error(response.statusText);
                }
                dispatch(_IsLoading(false));
                return response;
            })
            .then((response) => response.json())
            .then((obj) => {
                dispatch(_Invalidated(false));
                dispatch(_FetchDataSuccess(obj));
            })
            .catch(() => dispatch(_HasError(true)));
    };
}



function _ElSaveData(data, api_url, _IsLoading, _FetchDataSuccess, _Invalidated, _HasError) {
    return (dispatch) => {
        let method = "POST";
        let pk = "";
        if ('id' in data){
            method = "PUT";
            pk = data.id;
        }

        dispatch(_ElIsSaving(true));
        let token = Cookies.get('csrftoken');
        fetch(api_url+pk, {
            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);
            }

            dispatch(_ElIsSaving(false));
            return response;
        })
        .then((response) => response.json())
        .then((_El) => {
            dispatch(_ElInvalidated(false));
            dispatch(_ElSaveDataSuccess(_El));
        })
        .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
134
135
136
137
138
139
export function {{obj.name}}FetchData() {
    return _FetchData(
        "", 
        "{{obj.api_url}}", 
        {{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
179
180
181
182
183
184
185
186
export function {{obj.name}}ElFetchData(pk) {
    return _FetchData(
        pk, 
        "{{obj.api_url}}", 
        {{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 'read_only' not in obj or 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
210
211
212
213
export function {{obj.name}}ElSaveData(data) {
    return  _ElSaveData(data, "{{obj.api_url}}", {{obj.name}}ElIsLoading, {{obj.name}}ElFetchDataSuccess, {{obj.name}}ElInvalidated, {{obj.name}}ElHasError) 
}
{% endif %}

214
{% endfor %}