Commit aaa002cf authored by Florent Chehab's avatar Florent Chehab

Merge branch 'feature_post_and_put' into 'master'

Feature post and put

See merge request chehabfl/outgoing_rex!16
parents 8a8e8cf6 02af4292
Pipeline #26703 canceled with stages
in 1 minute and 34 seconds
......@@ -40,13 +40,14 @@ API_BASE = "http://127.0.0.1:8000/api/"
contexts = [
{
'name': 'universities',
'api_url': API_BASE + "university/"
'api_url': API_BASE + "university/",
}, {
'name': 'countries',
'api_url': API_BASE + "country/"
}, {
'name': 'mainCampus',
'api_url': API_BASE + "main_campus/"
'api_url': API_BASE + "main_campus/",
'read_only': True
}
]
......
......@@ -6,9 +6,23 @@
{% for obj in data %}
////////////////
// {{obj.NAME}}
////////////////
export const {{obj.NAME}}_HAS_ERROR = '{{obj.NAME}}_HAS_ERROR';
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_LOADING = '{{obj.NAME}}_IS_LOADING';
export const {{obj.NAME}}_EL_FETCH_DATA_SUCCESS = '{{obj.NAME}}_FETCH_DATA_SUCCESS';
{% if 'read_only' not in obj or not obj.read_only %}
export const {{obj.NAME}}_EL_IS_SAVING = '{{obj.NAME}}_IS_POSTING';
export const {{obj.NAME}}_EL_SAVING_DATA_SUCCESS = '{{obj.NAME}}_POST_DATA_SUCCESS';
{% endif %}
{% endfor %}
......@@ -4,15 +4,97 @@
// 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_LOADING,
{{obj.NAME}}_EL_FETCH_DATA_SUCCESS,
{% if 'read_only' not in obj or not obj.read_only %}
{{obj.NAME}}_EL_IS_SAVING,
{{obj.NAME}}_EL_SAVING_DATA_SUCCESS,
{% endif %}
{% endfor %}
} from "./action-types";
//////////////////////////////////
// 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)));
};
}
/////////////////////////////////
{% for obj in data %}
export function {{obj.name}}HasError(bool) {
return {
......@@ -46,26 +128,87 @@ export function {{obj.name}}FetchDataSuccess({{obj.name}}) {
}
export function {{obj.name}}FetchData(pk="") {
return (dispatch) => {
dispatch({{obj.name}}IsLoading(true));
export function {{obj.name}}FetchData() {
return _FetchData(
"",
"{{obj.api_url}}",
{{obj.name}}IsLoading,
{{obj.name}}FetchDataSuccess,
{{obj.name}}Invalidated,
{{obj.name}}HasError
)
}
fetch("{{obj.api_url}}"+pk)
.then((response) => {
if (!response.ok) {
throw Error(response.statusText);
}
dispatch({{obj.name}}IsLoading(false));
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
};
}
return response;
})
.then((response) => response.json())
.then(({{obj.name}}) => {
dispatch({{obj.name}}Invalidated(false));
dispatch({{obj.name}}FetchDataSuccess({{obj.name}}));
})
.catch(() => dispatch({{obj.name}}HasError(true)));
export function {{obj.name}}ElIsLoading(bool) {
return {
type: {{obj.NAME}}_EL_IS_LOADING,
isLoading: bool
};
}
export function {{obj.name}}ElFetchDataSuccess({{obj.name}}El) {
{{obj.name}}ElInvalidated(false)
return {
type: {{obj.NAME}}_EL_FETCH_DATA_SUCCESS,
{{obj.name}}El,
{{obj.name}}ElFetchedAt: Date.now()
};
}
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
)
}
{% if 'read_only' not in obj or not obj.read_only %}
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}}ElSaveData(data) {
return _ElSaveData(data, "{{obj.api_url}}", {{obj.name}}ElIsLoading, {{obj.name}}ElFetchDataSuccess, {{obj.name}}ElInvalidated, {{obj.name}}ElHasError)
}
{% endif %}
{% endfor %}
\ No newline at end of file
......@@ -8,18 +8,46 @@ import { combineReducers } from 'redux';
import {
{% for obj in data %}
{{obj.name}}Fetched,
{{obj.name}}Invalidated,
{{obj.name}}HasError,
{{obj.name}}IsLoading,
{{obj.name}}Invalidated,
{{obj.name}}Fetched,
{{obj.name}}ElInvalidated,
{{obj.name}}ElHasError,
{{obj.name}}ElIsLoading,
{{obj.name}}ElFetched,
{% if 'read_only' not in obj or not obj.read_only %}
{{obj.name}}ElIsSaving,
{{obj.name}}ElSaved,
{% endif %}
{% 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,
})
export const {{obj.name}}ElReducers = combineReducers({
invalidated: {{obj.name}}ElInvalidated,
hasError: {{obj.name}}ElHasError,
isLoading: {{obj.name}}ElIsLoading,
fetched: {{obj.name}}ElFetched,
{% if 'read_only' not in obj or not obj.read_only %}
isSaving: {{obj.name}}ElIsSaving,
isSaved: {{obj.name}}ElSaved,
{% endif %}
})
{% endfor %}
......@@ -6,10 +6,21 @@
import {
{% for obj in data %}
{{obj.NAME}}_INVALIDATED,
{{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_LOADING,
{{obj.NAME}}_EL_FETCH_DATA_SUCCESS,
{% if 'read_only' not in obj or not obj.read_only %}
{{obj.NAME}}_EL_IS_SAVING,
{{obj.NAME}}_EL_SAVING_DATA_SUCCESS,
{% endif %}
{% endfor %}
} from "./action-types";
......@@ -44,6 +55,16 @@ export function {{obj.name}}Invalidated(state = false, action) {
}
}
export function {{obj.name}}ElIsLoading(state = false, action) {
switch (action.type) {
case {{obj.NAME}}_EL_IS_LOADING:
return action.isLoading;
default:
return state;
}
}
export function {{obj.name}}Fetched(state = { {{obj.name}}: [], {{obj.name}}FetchedAt: null }, action) {
switch (action.type) {
case {{obj.NAME}}_FETCH_DATA_SUCCESS:
......@@ -57,4 +78,68 @@ export function {{obj.name}}Fetched(state = { {{obj.name}}: [], {{obj.name}}Fetc
}
}
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}}ElFetched(state = { {{obj.name}}El: [], {{obj.name}}ElFetchedAt: null }, action) {
switch (action.type) {
case {{obj.NAME}}_EL_FETCH_DATA_SUCCESS:
return {
{{obj.name}}El: action.{{obj.name}}El,
fetchedAt: action.{{obj.name}}ElFetchedAt
}
default:
return state;
}
}
{% if 'read_only' not in obj or not obj.read_only %}
// Handling POST and PUT
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;
}
}
{% endif %}
{% endfor %}
\ No newline at end of file
......@@ -3,7 +3,9 @@ import { combineReducers } from 'redux';
import {
universitiesReducers,
mainCampusReducers,
countriesReducers
countriesReducers,
countriesElReducers,
universitiesElReducers
} from '../generated/combinedReducers';
import {
......@@ -15,9 +17,11 @@ const appReducers = combineReducers({
})
const rootReducer = combineReducers({
countries: countriesReducers,
countriesEl: countriesElReducers,
universities: universitiesReducers,
universitiesEl: universitiesElReducers,
mainCampus: mainCampusReducers,
countries: countriesReducers,
app: appReducers
})
......
......@@ -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