Commit 985357c2 authored by Florent Chehab's avatar Florent Chehab
Browse files

Saving selected universities to store ok

parent 6cc84c53
export const SAVE_MAIN_MAP_POSITION = 'SAVE_MAIN_MAP_POSITION'; export const SAVE_MAIN_MAP_POSITION = 'SAVE_MAIN_MAP_POSITION';
export const SAVE_SELECTED_UNIVERSITIES = 'SAVE_SELECTED_UNIVERSITIES';
...@@ -4,10 +4,10 @@ import { ...@@ -4,10 +4,10 @@ import {
} from "./action-types"; } from "./action-types";
export function saveMainMapPosition(new_position) { export function saveMainMapPosition(new_position) {
return { return {
type: SAVE_MAIN_MAP_POSITION, type: SAVE_MAIN_MAP_POSITION,
new_position new_position
}; };
} }
import {
SAVE_SELECTED_UNIVERSITIES
} from "./action-types";
export function saveSelectedUniversities(new_selection) {
return {
type: SAVE_SELECTED_UNIVERSITIES,
new_selection
};
}
...@@ -4,6 +4,7 @@ import DownshiftMultiple from './DownshiftMultiple'; ...@@ -4,6 +4,7 @@ import DownshiftMultiple from './DownshiftMultiple';
import MyComponent from '../MyComponent' import MyComponent from '../MyComponent'
import { connect } from "react-redux"; import { connect } from "react-redux";
import _ from 'underscore'; import _ from 'underscore';
import { saveSelectedUniversities } from '../../actions/selectedUniversities';
import { import {
universitiesFetchData, universitiesFetchData,
...@@ -24,33 +25,62 @@ class Search extends MyComponent { ...@@ -24,33 +25,62 @@ class Search extends MyComponent {
} }
joinCampus(campus) {
const { universities, countries, cities } = this.getAllFetchedData();
let res = Object.assign({}, campus); //copy for safety
res.university = universities[campus.university];
res.city = cities[campus.city]
res.country = countries[res.city.country]
return res;
}
// getUnivFromCampus(campus) {
// const { universities } = this.props;
// return universities[campus.univ]
// }
// getCountryFromUniversity(univ) {
// const { countries } = this.props;
// return countries[univ.country]
// }
// getCountryFromCampus(campus) {
// const univ = this.getUnivFromCampus(campus);
// return this.getCountryFromUniversity(univ);
// }
getCountriesWhereThereAreUniversities() { getCountriesWhereThereAreUniversities() {
const { universities, const { mainCampuses } = this.getAllFetchedData();
mainCampuses,
countries,
cities } = this.getAllFetchedData();
let res = []; let res = [];
_.each(mainCampuses, (campus) => { _.each(mainCampuses, (campus) => {
let univ = universities[campus.university] const campusFull = this.joinCampus(campus)
if (univ && campus) { res.push(campusFull.country)
let city = cities[campus.city]
let country = countries[city.country]
res.push(country)
}
}); });
return _.uniq(res, false, _.iteratee((val) => { return val.iso_alpha2_code })); return _.uniq(res, false, (c) => { return c.iso_alpha2_code });
} }
myRender() { updateSelectedUniversities(selection) {
let options = []; const { mainCampuses } = this.getAllFetchedData();
this.getCountriesWhereThereAreUniversities().forEach(country => { const listOfCountries = _.map(selection, (s) => s.id);
options.push({ id: country.iso_alpha2_code, label: country.name });
let selected_universities = []
_.each(mainCampuses, (campus) => {
const campusFull = this.joinCampus(campus)
if (_.indexOf(listOfCountries, campusFull.country.iso_alpha2_code) > -1) {
selected_universities.push(campusFull.university.id);
}
}) })
this.props.saveSelection(selected_universities);
}
myRender() {
const options = _.map(this.getCountriesWhereThereAreUniversities(),
(c) => { return { id: c.iso_alpha2_code, label: c.name } })
return ( return (
<DownshiftMultiple options={options} onChange={(selection) => console.log(selection)} /> <DownshiftMultiple options={options} onChange={(selection) => this.updateSelectedUniversities(selection)} />
); );
} }
} }
...@@ -73,7 +103,8 @@ const mapDispatchToProps = (dispatch) => { ...@@ -73,7 +103,8 @@ const mapDispatchToProps = (dispatch) => {
mainCampuses: () => dispatch(mainCampusesFetchData()), mainCampuses: () => dispatch(mainCampusesFetchData()),
cities: () => dispatch(citiesFetchData()), cities: () => dispatch(citiesFetchData()),
countries: () => dispatch(countriesFetchData()) countries: () => dispatch(countriesFetchData())
} },
saveSelection: (selectedUniversities) => dispatch(saveSelectedUniversities(selectedUniversities)),
}; };
}; };
......
...@@ -11,7 +11,9 @@ import App from "./components/App"; ...@@ -11,7 +11,9 @@ import App from "./components/App";
const MainReactEntry = () => ( const MainReactEntry = () => (
<Provider store={store}> <Provider store={store}>
<Router> <Router>
<Route path="/:filter?" component={App} /> <React.StrictMode>
<Route path="/:filter?" component={App} />
</React.StrictMode>
</Router> </Router>
</Provider> </Provider>
); );
......
...@@ -10,12 +10,12 @@ import { ...@@ -10,12 +10,12 @@ import {
citiesReducers, citiesReducers,
} from '../generated/combinedReducers'; } from '../generated/combinedReducers';
import { import { saveMainMapPosition } from './map'
saveMainMapPosition import { saveSelectedUniversities } from './selectedUniversities'
} from './map'
const appReducers = combineReducers({ const appReducers = combineReducers({
mainMap: saveMainMapPosition, mainMap: saveMainMapPosition,
selectedUniversities: saveSelectedUniversities
}) })
const rootReducer = combineReducers({ const rootReducer = combineReducers({
......
import { SAVE_SELECTED_UNIVERSITIES } from '../actions/action-types'
export function saveSelectedUniversities(state = [], action) {
switch (action.type) {
case SAVE_SELECTED_UNIVERSITIES:
return {
selection: action.new_selection,
}
default:
return state;
}
}
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