Commit 2eeaf5bc authored by Florent Chehab's avatar Florent Chehab
Browse files

Frontent templates revisited, redux state redesign

parent 61887e05
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# This python file is used to generate js files for redux # This python file is used to generate js files for redux
import os import os
from django import template from django import template
import re
############ ############
# Need to do this first so that Django template engine is working # Need to do this first so that Django template engine is working
...@@ -30,18 +31,16 @@ if not os.path.exists(saving_dir): ...@@ -30,18 +31,16 @@ if not os.path.exists(saving_dir):
templates = [ templates = [
'action-types', 'action-types',
'actions', 'actions',
'reducers' 'reducers',
'combinedReducers'
] ]
contexts = [ contexts = [
{ {
'NAME': 'UNIVERSITIES',
'name': 'universities' 'name': 'universities'
}, { }, {
'NAME': 'COUNTRIES',
'name': 'countries' 'name': 'countries'
}, { }, {
'NAME': 'MAIN_CAMPUS',
'name': 'mainCampus' 'name': 'mainCampus'
} }
] ]
...@@ -57,13 +56,21 @@ def write_file(file, data): ...@@ -57,13 +56,21 @@ def write_file(file, data):
the_file.write(data) the_file.write(data)
def convert(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).upper()
for c in contexts:
c['NAME'] = convert(c['name'])
# print(contexts)
for filename in templates: for filename in templates:
t = template.Template( t = template.Template(
read_file(os.path.join(templates_dir, filename + '.tpl'))) read_file(os.path.join(templates_dir, filename + '.tpl')))
output = "" c = template.Context({'data': contexts})
for tmpC in contexts: output = t.render(c)
c = template.Context(tmpC)
output += t.render(c) + "\n\n"
write_file(os.path.join(saving_dir, filename + '.js'), output) write_file(os.path.join(saving_dir, filename + '.js'), output)
...@@ -4,7 +4,11 @@ ...@@ -4,7 +4,11 @@
// MODIFY THE FILE ABOVE IF YOUR NOT SATISFIED // MODIFY THE FILE ABOVE IF YOUR NOT SATISFIED
// THIS WARNING DOESN'T APPLY TO .tpl FILES... // THIS WARNING DOESN'T APPLY TO .tpl FILES...
export const {{NAME}}_HAS_ERROR = '{{NAME}}_HAS_ERROR';
export const {{NAME}}_IS_LOADING = '{{NAME}}_IS_LOADING'; {% for obj in data %}
export const {{NAME}}_FETCH_DATA_SUCCESS = '{{NAME}}_FETCH_DATA_SUCCESS'; export const {{obj.NAME}}_HAS_ERROR = '{{obj.NAME}}_HAS_ERROR';
export const {{NAME}}_INVALIDATED = '{{NAME}}_INVALIDATED'; 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';
{% endfor %}
...@@ -5,48 +5,50 @@ ...@@ -5,48 +5,50 @@
// THIS WARNING DOESN'T APPLY TO .tpl FILES... // THIS WARNING DOESN'T APPLY TO .tpl FILES...
import { import {
{{NAME}}_HAS_ERROR, {% for obj in data %}
{{NAME}}_IS_LOADING, {{obj.NAME}}_HAS_ERROR,
{{NAME}}_FETCH_DATA_SUCCESS, {{obj.NAME}}_IS_LOADING,
{{NAME}}_INVALIDATED {{obj.NAME}}_FETCH_DATA_SUCCESS,
{{obj.NAME}}_INVALIDATED,
{% endfor %}
} from "./action-types"; } from "./action-types";
{% for obj in data %}
export function {{name}}HasError(bool) { export function {{obj.name}}HasError(bool) {
return { return {
type: {{NAME}}_HAS_ERROR, type: {{obj.NAME}}_HAS_ERROR,
hasError: bool hasError: bool
}; };
} }
export function {{name}}IsLoading(bool) { export function {{obj.name}}IsLoading(bool) {
return { return {
type: {{NAME}}_IS_LOADING, type: {{obj.NAME}}_IS_LOADING,
isLoading: bool isLoading: bool
}; };
} }
export function {{name}}Invalidated(bool) { export function {{obj.name}}Invalidated(bool) {
return { return {
type: {{NAME}}_INVALIDATED, type: {{obj.NAME}}_INVALIDATED,
invalidated: bool invalidated: bool
}; };
} }
export function {{name}}FetchDataSuccess({{name}}) { export function {{obj.name}}FetchDataSuccess({{obj.name}}) {
{{name}}Invalidated(false) {{obj.name}}Invalidated(false)
return { return {
type: {{NAME}}_FETCH_DATA_SUCCESS, type: {{obj.NAME}}_FETCH_DATA_SUCCESS,
{{name}}, {{obj.name}},
{{name}}FetchedAt: Date.now() {{obj.name}}FetchedAt: Date.now()
}; };
} }
export function {{name}}FetchData(url) { export function {{obj.name}}FetchData(url) {
return (dispatch) => { return (dispatch) => {
dispatch({{name}}IsLoading(true)); dispatch({{obj.name}}IsLoading(true));
fetch(url) fetch(url)
.then((response) => { .then((response) => {
...@@ -54,15 +56,16 @@ export function {{name}}FetchData(url) { ...@@ -54,15 +56,16 @@ export function {{name}}FetchData(url) {
throw Error(response.statusText); throw Error(response.statusText);
} }
dispatch({{name}}IsLoading(false)); dispatch({{obj.name}}IsLoading(false));
return response; return response;
}) })
.then((response) => response.json()) .then((response) => response.json())
.then(({{name}}) => { .then(({{obj.name}}) => {
dispatch({{name}}Invalidated(false)); dispatch({{obj.name}}Invalidated(false));
dispatch({{name}}FetchDataSuccess({{name}})); dispatch({{obj.name}}FetchDataSuccess({{obj.name}}));
}) })
.catch(() => dispatch({{name}}HasError(true))); .catch(() => dispatch({{obj.name}}HasError(true)));
}; };
} }
\ No newline at end of file {% endfor %}
\ No newline at end of file
// 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...
import { combineReducers } from 'redux';
import {
{% for obj in data %}
{{obj.name}}Fetched,
{{obj.name}}HasError,
{{obj.name}}IsLoading,
{{obj.name}}Invalidated,
{% endfor %}
} from './reducers';
{% for obj in data %}
export const {{obj.name}}Reducers = combineReducers({
fetched: {{obj.name}}Fetched,
hasError: {{obj.name}}HasError,
isLoading: {{obj.name}}IsLoading,
invalidated: {{obj.name}}Invalidated,
})
{% endfor %}
...@@ -5,16 +5,18 @@ ...@@ -5,16 +5,18 @@
// THIS WARNING DOESN'T APPLY TO .tpl FILES... // THIS WARNING DOESN'T APPLY TO .tpl FILES...
import { import {
{{NAME}}_HAS_ERROR, {% for obj in data %}
{{NAME}}_IS_LOADING, {{obj.NAME}}_HAS_ERROR,
{{NAME}}_FETCH_DATA_SUCCESS, {{obj.NAME}}_IS_LOADING,
{{NAME}}_INVALIDATED {{obj.NAME}}_FETCH_DATA_SUCCESS,
{{obj.NAME}}_INVALIDATED,
{% endfor %}
} from "./action-types"; } from "./action-types";
{% for obj in data %}
export function {{name}}HasError(state = false, action) { export function {{obj.name}}HasError(state = false, action) {
switch (action.type) { switch (action.type) {
case {{NAME}}_HAS_ERROR: case {{obj.NAME}}_HAS_ERROR:
return action.hasError; return action.hasError;
default: default:
...@@ -22,9 +24,9 @@ export function {{name}}HasError(state = false, action) { ...@@ -22,9 +24,9 @@ export function {{name}}HasError(state = false, action) {
} }
} }
export function {{name}}IsLoading(state = false, action) { export function {{obj.name}}IsLoading(state = false, action) {
switch (action.type) { switch (action.type) {
case {{NAME}}_IS_LOADING: case {{obj.NAME}}_IS_LOADING:
return action.isLoading; return action.isLoading;
default: default:
...@@ -32,9 +34,9 @@ export function {{name}}IsLoading(state = false, action) { ...@@ -32,9 +34,9 @@ export function {{name}}IsLoading(state = false, action) {
} }
} }
export function {{name}}Invalidated(state = false, action) { export function {{obj.name}}Invalidated(state = false, action) {
switch (action.type) { switch (action.type) {
case {{NAME}}_INVALIDATED: case {{obj.NAME}}_INVALIDATED:
return action.invalidated; return action.invalidated;
default: default:
...@@ -42,15 +44,17 @@ export function {{name}}Invalidated(state = false, action) { ...@@ -42,15 +44,17 @@ export function {{name}}Invalidated(state = false, action) {
} }
} }
export function {{name}}Fetched(state = { {{name}}: [], {{name}}FetchedAt: null }, action) { export function {{obj.name}}Fetched(state = { {{obj.name}}: [], {{obj.name}}FetchedAt: null }, action) {
switch (action.type) { switch (action.type) {
case {{NAME}}_FETCH_DATA_SUCCESS: case {{obj.NAME}}_FETCH_DATA_SUCCESS:
return { return {
{{name}}: action.{{name}}, {{obj.name}}: action.{{obj.name}},
fetchedAt: action.{{name}}FetchedAt fetchedAt: action.{{obj.name}}FetchedAt
} }
default: default:
return state; return state;
} }
} }
\ No newline at end of file
{% endfor %}
\ No newline at end of file
...@@ -200,11 +200,11 @@ App.propTypes = { ...@@ -200,11 +200,11 @@ App.propTypes = {
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
return { return {
countries: { countries: {
countries: state.countriesFetched.countries, countries: state.countries.fetched.countries,
fetchedAt: state.countriesFetched.fetchedAt, fetchedAt: state.countries.fetched.fetchedAt,
hasError: state.countriesHasError, hasError: state.countries.hasError,
isLoading: state.countriesIsLoading, isLoading: state.countries.isLoading,
invalidated: state.countriesInvalidated invalidated: state.countries.invalidated
} }
}; };
}; };
......
...@@ -122,17 +122,17 @@ class UnivMap extends Component { ...@@ -122,17 +122,17 @@ class UnivMap extends Component {
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
return { return {
universities: state.universitiesFetched.universities, universities: state.universities.fetched.universities,
universitiesFetchedAt: state.universitiesFetched.fetchedAt, universitiesFetchedAt: state.universities.fetched.fetchedAt,
universitiesHasError: state.universitiesHasError, universitiesHasError: state.universities.hasError,
universitiesIsLoading: state.universitiesIsLoading, universitiesIsLoading: state.universities.isLoading,
universitiesInvalidated: state.universitiesInvalidated, universitiesInvalidated: state.universities.invalidated,
mainCampus: state.mainCampusFetched.mainCampus, mainCampus: state.mainCampus.fetched.mainCampus,
mainCampusFetchedAt: state.mainCampusFetched.fetchedAt, mainCampusFetchedAt: state.mainCampus.fetched.fetchedAt,
mainCampusHasError: state.mainCampusHasError, mainCampusHasError: state.mainCampus.hasError,
mainCampusIsLoading: state.mainCampusIsLoading, mainCampusIsLoading: state.mainCampus.isLoading,
mainCampusInvalidated: state.mainCampusInvalidated, mainCampusInvalidated: state.mainCampus.invalidated,
}; };
}; };
......
...@@ -33,7 +33,7 @@ class UnivMapReloadButton extends Component { ...@@ -33,7 +33,7 @@ class UnivMapReloadButton extends Component {
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
return { return {
invalidated: state.universitiesInvalidated invalidated: state.universities.invalidated
}; };
}; };
......
import { combineReducers } from 'redux'; import { combineReducers } from 'redux';
import { import {
universitiesFetched, universitiesReducers,
universitiesHasError, mainCampusReducers,
universitiesIsLoading, countriesReducers
universitiesInvalidated, } from '../generated/combinedReducers';
countriesFetched,
countriesHasError,
countriesIsLoading,
countriesInvalidated,
mainCampusFetched,
mainCampusHasError,
mainCampusIsLoading,
mainCampusInvalidated,
} from '../generated/reducers';
const rootReducer = combineReducers({ const rootReducer = combineReducers({
universitiesFetched, universities: universitiesReducers,
universitiesHasError, mainCampus: mainCampusReducers,
universitiesIsLoading, countries: countriesReducers
universitiesInvalidated,
countriesFetched,
countriesHasError,
countriesIsLoading,
countriesInvalidated,
mainCampusFetched,
mainCampusHasError,
mainCampusIsLoading,
mainCampusInvalidated,
}) })
export default rootReducer export default rootReducer
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