Commit 4c296c75 authored by Florent Chehab's avatar Florent Chehab

File generation should be ok

parent a157cc69
......@@ -11,4 +11,9 @@ export const {{obj.NAME}}_IS_LOADING = '{{obj.NAME}}_IS_LOADING';
export const {{obj.NAME}}_FETCH_DATA_SUCCESS = '{{obj.NAME}}_FETCH_DATA_SUCCESS';
export const {{obj.NAME}}_INVALIDATED = '{{obj.NAME}}_INVALIDATED';
export const {{obj.NAME}}_EL_HAS_ERROR = '{{obj.NAME}}_HAS_ERROR';
export const {{obj.NAME}}_EL_INVALIDATED = '{{obj.NAME}}_EL_INVALIDATED';
export const {{obj.NAME}}_EL_IS_SAVING = '{{obj.NAME}}_IS_POSTING';
export const {{obj.NAME}}_EL_SAVING_DATA_SUCCESS = '{{obj.NAME}}_POST_DATA_SUCCESS';
{% endfor %}
......@@ -4,12 +4,23 @@
// MODIFY THE FILE ABOVE IF YOUR NOT SATISFIED
// THIS WARNING DOESN'T APPLY TO .tpl FILES...
import Cookies from 'js-cookie';
import {
{% for obj in data %}
{{obj.NAME}}_HAS_ERROR,
{{obj.NAME}}_IS_LOADING,
{{obj.NAME}}_FETCH_DATA_SUCCESS,
{{obj.NAME}}_INVALIDATED,
{{obj.NAME}}_EL_INVALIDATED,
{{obj.NAME}}_EL_HAS_ERROR,
{{obj.NAME}}_EL_IS_SAVING,
{{obj.NAME}}_EL_SAVING_DATA_SUCCESS,
{{obj.NAME}}_EL_IS_PUTTING,
{{obj.NAME}}_EL_PUT_DATA_SUCCESS,
{% endfor %}
} from "./action-types";
......@@ -68,4 +79,109 @@ export function {{obj.name}}FetchData(pk="") {
.catch(() => dispatch({{obj.name}}HasError(true)));
};
}
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
};
}
export function {{obj.name}}ElIsSaving(bool) {
return {
type: {{obj.NAME}}_EL_IS_SAVING,
isSaving: bool
};
}
export function {{obj.name}}ElSaveDataSuccess({{obj.name}}El) {
{{obj.name}}ElInvalidated(false)
return {
type: {{obj.NAME}}_EL_SAVING_DATA_SUCCESS,
{{obj.name}}El,
{{obj.name}}ElSavedAt: Date.now()
};
}
export function {{obj.name}}ElPostData(data) {
return (dispatch) => {
dispatch({{obj.name}}ElIsSaving(true));
let token = Cookies.get('csrftoken');
fetch("{{obj.api_url}}", {
method: 'POST',
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({{obj.name}}ElIsSaving(false));
return response;
})
.then((response) => response.json())
.then(({{obj.name}}El) => {
dispatch({{obj.name}}ElInvalidated(false));
dispatch({{obj.name}}ElSaveDataSuccess({{obj.name}}El));
})
.catch(() => dispatch({{obj.name}}ElHasError(true)));
};
}
export function {{obj.name}}ElPutData(data) {
return (dispatch) => {
dispatch({{obj.name}}ElIsSaving(true));
let token = Cookies.get('csrftoken');
fetch("{{obj.api_url}}", {
method: 'PUT',
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({{obj.name}}ElIsSaving(false));
return response;
})
.then((response) => response.json())
.then(({{obj.name}}El) => {
dispatch({{obj.name}}ElInvalidated(false));
dispatch({{obj.name}}ElSaveDataSuccess({{obj.name}}El));
})
.catch(() => dispatch({{obj.name}}ElHasError(true)));
};
}
{% endfor %}
\ No newline at end of file
......@@ -12,14 +12,25 @@ import {
{{obj.name}}HasError,
{{obj.name}}IsLoading,
{{obj.name}}Invalidated,
{{obj.name}}ElHasError,
{{obj.name}}ElInvalidated,
{{obj.name}}ElIsSaving,
{{obj.name}}ElSaved,
{% endfor %}
} from './reducers';
{% for obj in data %}
export const {{obj.name}}Reducers = combineReducers({
fetched: {{obj.name}}Fetched,
invalidated: {{obj.name}}Invalidated,
hasError: {{obj.name}}HasError,
isLoading: {{obj.name}}IsLoading,
invalidated: {{obj.name}}Invalidated,
fetched: {{obj.name}}Fetched,
elInvalidated: {{obj.name}}ElInvalidated,
elHasError: {{obj.name}}ElHasError,
elIsPosting: {{obj.name}}ElIsSaving,
elSaved: {{obj.name}}ElSaved,
})
{% endfor %}
......@@ -10,6 +10,14 @@ import {
{{obj.NAME}}_IS_LOADING,
{{obj.NAME}}_FETCH_DATA_SUCCESS,
{{obj.NAME}}_INVALIDATED,
{{obj.NAME}}_EL_INVALIDATED,
{{obj.NAME}}_EL_HAS_ERROR,
{{obj.NAME}}_EL_IS_SAVING,
{{obj.NAME}}_EL_SAVING_DATA_SUCCESS,
{{obj.NAME}}_EL_IS_PUTTING,
{{obj.NAME}}_EL_PUT_DATA_SUCCESS,
{% endfor %}
} from "./action-types";
......@@ -57,4 +65,50 @@ export function {{obj.name}}Fetched(state = { {{obj.name}}: [], {{obj.name}}Fetc
}
}
// Handling POST and PUT
export function {{obj.name}}ElHasError(state = false, action) {
switch (action.type) {
case {{obj.NAME}}_El_HAS_ERROR:
return action.hasError;
default:
return state;
}
}
export function {{obj.name}}ElInvalidated(state = false, action) {
switch (action.type) {
case {{obj.NAME}}_EL_INVALIDATED:
return action.invalidated;
default:
return state;
}
}
export function {{obj.name}}ElIsSaving(state = false, action) {
switch (action.type) {
case {{obj.NAME}}_EL_IS_SAVING:
return action.isPosting;
default:
return state;
}
}
export function {{obj.name}}ElSaved(state = { {{obj.name}}El: [], {{obj.name}}ElSavedAt: null }, action) {
switch (action.type) {
case {{obj.NAME}}_EL_SAVING_DATA_SUCCESS:
return {
{{obj.name}}El: action.{{obj.name}}El,
fetchedAt: action.{{obj.name}}ElSavedAt
}
default:
return state;
}
}
{% endfor %}
\ No newline at end of file
......@@ -4202,6 +4202,11 @@
"whatwg-fetch": ">=0.10.0"
}
},
"js-cookie": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-2.2.0.tgz",
"integrity": "sha1-Gywnmm7s44ChIWi5JIUmWzWx7/s="
},
"js-tokens": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment