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

Saving selected universities to store ok

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