Commit 619dd7d0 authored by Florent Chehab's avatar Florent Chehab

Cleaned some JS and better POO

parent fca46abf
...@@ -35,13 +35,18 @@ templates = [ ...@@ -35,13 +35,18 @@ templates = [
'combinedReducers' 'combinedReducers'
] ]
API_BASE = "http://127.0.0.1:8000/api/"
contexts = [ contexts = [
{ {
'name': 'universities' 'name': 'universities',
'api_url': API_BASE + "university/"
}, { }, {
'name': 'countries' 'name': 'countries',
'api_url': API_BASE + "country/"
}, { }, {
'name': 'mainCampus' 'name': 'mainCampus',
'api_url': API_BASE + "main_campus/"
} }
] ]
......
...@@ -46,11 +46,11 @@ export function {{obj.name}}FetchDataSuccess({{obj.name}}) { ...@@ -46,11 +46,11 @@ export function {{obj.name}}FetchDataSuccess({{obj.name}}) {
} }
export function {{obj.name}}FetchData(url) { export function {{obj.name}}FetchData(pk="") {
return (dispatch) => { return (dispatch) => {
dispatch({{obj.name}}IsLoading(true)); dispatch({{obj.name}}IsLoading(true));
fetch(url) fetch("{{obj.api_url}}"+pk)
.then((response) => { .then((response) => {
if (!response.ok) { if (!response.ok) {
throw Error(response.statusText); throw Error(response.statusText);
......
...@@ -115,7 +115,7 @@ class App extends React.Component { ...@@ -115,7 +115,7 @@ class App extends React.Component {
componentDidMount() { componentDidMount() {
if (this.props.countries.countries.length == 0 || this.props.invalidated) { if (this.props.countries.countries.length == 0 || this.props.invalidated) {
console.log("here mother uc", this.props.countries.countries); console.log("here mother uc", this.props.countries.countries);
this.props.fetchData('http://127.0.0.1:8000/api/country'); this.props.fetchData();
} }
} }
...@@ -123,7 +123,7 @@ class App extends React.Component { ...@@ -123,7 +123,7 @@ class App extends React.Component {
// TODO ajouter expire date // TODO ajouter expire date
if (this.props.countries.invalidated) { if (this.props.countries.invalidated) {
this.props.countries.fetchData('http://127.0.0.1:8000/api/country'); this.props.countries.fetchData();
} }
} }
...@@ -211,7 +211,7 @@ const mapStateToProps = (state) => { ...@@ -211,7 +211,7 @@ const mapStateToProps = (state) => {
const mapDispatchToProps = (dispatch) => { const mapDispatchToProps = (dispatch) => {
return { return {
fetchData: (url) => dispatch(countriesFetchData(url)), fetchData: () => dispatch(countriesFetchData()),
}; };
}; };
......
import React, { Component } from 'react';
class MyComponent extends Component {
checkProps(val) {
for (let el in this.props) {
let prop = this.props[el];
if (val in prop && prop[val]) {
return true;
}
}
return false;
}
checkPropsHasError() {
return this.checkProps('hasError');
}
checkPropsIsLoading() {
return this.checkProps('isLoading');
}
}
export default MyComponent;
import React, { Component } from 'react'; import React from 'react';
import MyComponent from '../MyComponent'
import { connect } from "react-redux"; import { connect } from "react-redux";
import Loading from '../other/Loading'; import Loading from '../other/Loading';
import { Map, TileLayer, Marker, Popup, LayersControl, FeatureGroup, Circle, LayerGroup } from 'react-leaflet'; import { Map, TileLayer, Marker, Popup, LayersControl, FeatureGroup, Circle, LayerGroup } from 'react-leaflet';
...@@ -12,25 +13,28 @@ import { ...@@ -12,25 +13,28 @@ import {
import UnivMarkers from './UnivMakers'; import UnivMarkers from './UnivMakers';
class UnivMap extends Component { class UnivMap extends MyComponent {
componentDidMount() { componentDidMount() {
if (this.props.universities.length == 0 || this.props.invalidated) { let univ_data = this.props.universities.fetched.universities;
this.props.fetchUniversitiesData('http://127.0.0.1:8000/api/university'); if (univ_data.length == 0 || this.props.universities.invalidated) {
this.props.fetchUniversities();
} }
if (this.props.mainCampus.length == 0 || this.props.invalidated) {
this.props.fetchMainCampusData('http://127.0.0.1:8000/api/main_campus'); let main_campus_data = this.props.mainCampus.fetched.mainCampus;
if (main_campus_data.length == 0 || this.props.mainCampus.invalidated) {
this.props.fetchMainCampus();
} }
} }
componentDidUpdate() { componentDidUpdate() {
// TODO ajouter expire date // TODO ajouter expire date
if (this.props.universitiesInvalidated) { if (this.props.universities.invalidated) {
this.props.fetchUniversitiesData('http://127.0.0.1:8000/api/university'); this.props.fetchUniversities();
} }
if (this.props.mainCampusInvalidated) { if (this.props.mainCampus.invalidated) {
this.props.fetchMainCampusData('http://127.0.0.1:8000/api/main_campus'); this.props.fetchMainCampus();
} }
} }
...@@ -39,16 +43,11 @@ class UnivMap extends Component { ...@@ -39,16 +43,11 @@ class UnivMap extends Component {
} }
render() { render() {
if (this.props.universitiesHasError || this.props.mainCampusHasError) { if (this.checkPropsHasError()) {
return <p>Sorry! There was an error loading the items</p>; return <p>Sorry! There was an error loading the items</p>;
} }
if (this.props.universitiesIsLoading || this.props.universitiesInvalidated) { if (this.checkPropsIsLoading()) {
return <Loading />;
}
if (this.props.mainCampusIsLoading || this.props.mainCampusInvalidated) {
return <Loading />; return <Loading />;
} }
...@@ -91,7 +90,7 @@ class UnivMap extends Component { ...@@ -91,7 +90,7 @@ class UnivMap extends Component {
</LayersControl.BaseLayer> </LayersControl.BaseLayer>
</LayersControl> </LayersControl>
<UnivMarkers/> <UnivMarkers />
</Map> </Map>
); );
...@@ -101,24 +100,15 @@ class UnivMap extends Component { ...@@ -101,24 +100,15 @@ class UnivMap extends Component {
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
return { return {
universities: state.universities.fetched.universities, universities: state.universities,
universitiesFetchedAt: state.universities.fetched.fetchedAt, mainCampus: state.mainCampus,
universitiesHasError: state.universities.hasError,
universitiesIsLoading: state.universities.isLoading,
universitiesInvalidated: state.universities.invalidated,
mainCampus: state.mainCampus.fetched.mainCampus,
mainCampusFetchedAt: state.mainCampus.fetched.fetchedAt,
mainCampusHasError: state.mainCampus.hasError,
mainCampusIsLoading: state.mainCampus.isLoading,
mainCampusInvalidated: state.mainCampus.invalidated,
}; };
}; };
const mapDispatchToProps = (dispatch) => { const mapDispatchToProps = (dispatch) => {
return { return {
fetchUniversitiesData: (url) => dispatch(universitiesFetchData(url)), fetchUniversities: () => dispatch(universitiesFetchData()),
fetchMainCampusData: (url) => dispatch(mainCampusFetchData(url)), fetchMainCampus: () => dispatch(mainCampusFetchData()),
// invalidateUniversities: (bool) => dispatch(universitiesInvalidated(bool)), // invalidateUniversities: (bool) => dispatch(universitiesInvalidated(bool)),
// invalidateMainCampus: (bool) => dispatch(mainCampusInvalidated(bool)) // invalidateMainCampus: (bool) => dispatch(mainCampusInvalidated(bool))
}; };
......
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