Commit 430e4e66 authored by Florent Chehab's avatar Florent Chehab
Browse files

Invalidated data added

parent 6aa128a8
import {UNIVERSITIES_HAS_ERROR, UNIVERSITIES_IS_LOADING, UNIVERSITIES_FETCH_DATA_SUCCESS} from "../constants/action-types";
import {
UNIVERSITIES_HAS_ERROR,
UNIVERSITIES_IS_LOADING,
UNIVERSITIES_FETCH_DATA_SUCCESS,
UNIVERSITIES_INVALIDATED
} from "../constants/action-types";
export function universitiesHasError(bool) {
return {
......@@ -14,7 +20,16 @@ export function universitiesIsLoading(bool) {
};
}
export function universitiesInvalidated(bool) {
return {
type: UNIVERSITIES_INVALIDATED,
invalidated: bool
};
}
export function universitiesFetchDataSuccess(universities) {
universitiesInvalidated(false)
return {
type: UNIVERSITIES_FETCH_DATA_SUCCESS,
universities
......@@ -37,7 +52,10 @@ export function universitiesFetchData(url) {
return response;
})
.then((response) => response.json())
.then((universities) => dispatch(universitiesFetchDataSuccess(universities)))
.then((universities) => {
dispatch(universitiesInvalidated(false));
dispatch(universitiesFetchDataSuccess(universities));
})
.catch(() => dispatch(universitiesHasError(true)));
};
}
\ No newline at end of file
......@@ -10,12 +10,18 @@ class UnivMap extends Component {
this.props.fetchData('http://127.0.0.1:8000/api/university');
}
componentDidUpdate(){
if (this.props.invalidated){
this.props.fetchData('http://127.0.0.1:8000/api/university');
}
}
render() {
if (this.props.hasError) {
return <p>Sorry! There was an error loading the items</p>;
}
if (this.props.isLoading) {
if (this.props.isLoading || this.props.invalidated) {
return <p>Loading</p>;
}
......@@ -67,8 +73,6 @@ class UnivMap extends Component {
</Marker>
))
}
</Map>
);
......@@ -80,7 +84,8 @@ const mapStateToProps = (state) => {
return {
universities: state.universities,
hasError: state.universitiesHasError,
isLoading: state.universitiesIsLoading
isLoading: state.universitiesIsLoading,
invalidated: state.universitiesInvalidated
};
};
......
export const UNIVERSITIES_HAS_ERROR = 'UNIVERSITIES_HAS_ERROR'
export const UNIVERSITIES_IS_LOADING = 'UNIVERSITIES_IS_LOADING'
export const UNIVERSITIES_FETCH_DATA_SUCCESS = 'UNIVERSITIES_FETCH_DATA_SUCCESS'
export const UNIVERSITIES_INVALIDATED = 'UNIVERSITIES_INVALIDATED'
import { combineReducers } from 'redux';
import { universities, universitiesHasError, universitiesIsLoading } from './universities';
import {
universities,
universitiesHasError,
universitiesIsLoading,
universitiesInvalidated
} from './universities';
const rootReducer = combineReducers({
universities,
universitiesHasError,
universitiesIsLoading
universitiesIsLoading,
universitiesInvalidated
})
export default rootReducer
......
import {UNIVERSITIES_HAS_ERROR, UNIVERSITIES_IS_LOADING, UNIVERSITIES_FETCH_DATA_SUCCESS} from "../constants/action-types";
import {
UNIVERSITIES_HAS_ERROR,
UNIVERSITIES_IS_LOADING,
UNIVERSITIES_FETCH_DATA_SUCCESS,
UNIVERSITIES_INVALIDATED
} from "../constants/action-types";
export function universitiesHasError(state = false, action) {
......@@ -21,6 +26,16 @@ export function universitiesIsLoading(state = false, action) {
}
}
export function universitiesInvalidated(state = false, action) {
switch (action.type) {
case UNIVERSITIES_INVALIDATED:
return action.invalidated;
default:
return state;
}
}
export function universities(state = [], action) {
switch (action.type) {
case UNIVERSITIES_FETCH_DATA_SUCCESS:
......
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