Commit 5749fb2f authored by Florent Chehab's avatar Florent Chehab

Less ambigous naming of read data

parent 5cc3220b
Pipeline #35807 passed with stages
in 9 minutes and 37 seconds
...@@ -191,13 +191,14 @@ class CustomComponentForAPI extends Component { ...@@ -191,13 +191,14 @@ class CustomComponentForAPI extends Component {
} }
/** /**
* Get the data that was read from the api given the `propName` and the time at which is was read * Get the latest data that was read from the api given the `propName` and the time at which is was read
* read, create and update are taken into account
* *
* @param {string} propName * @param {string} propName
* @returns {Any} * @returns {Any}
* @memberof CustomComponentForAPI * @memberof CustomComponentForAPI
*/ */
getReadDataAndTime(propName) { getLatestReadDataAndTime(propName) {
const prop = this.props[propName], const prop = this.props[propName],
out = getLatestRead(prop); out = getLatestRead(prop);
...@@ -210,56 +211,71 @@ class CustomComponentForAPI extends Component { ...@@ -210,56 +211,71 @@ class CustomComponentForAPI extends Component {
/** /**
* Get the data that was read from the api given the `propName` * Get the latest data that was read from the api given the `propName`
* read, create and update are taken into account
* *
* @param {string} propName * @param {string} propName
* @param {boolean} [returnLatest=true] Should the lastest data from read, create or update response be returned?
* @returns {Any} * @returns {Any}
* @memberof CustomComponentForAPI * @memberof CustomComponentForAPI
*/ */
getReadData(propName, returnLatest = true) { getLatestReadData(propName) {
if (!returnLatest) { return this.getLatestReadDataAndTime(propName).data;
return this.props[propName].readSucceeded.data; }
}
return this.getReadDataAndTime(propName).data; /**
* In some very rare case, we need to consider only the data that was read and not create ou updated.
* That what this function does.
*
* @param {string} propName
* @returns
* @memberof CustomComponentForAPI
*/
getOnlyReadData(propName) {
return this.props[propName].readSucceeded.data;
} }
/** /**
* Get the time at which the latest data from the api corresponding to `propName` was read * Get the latest time at which the latest data from the api corresponding to `propName` was read
*
* read, create and update are taken into account
* *
* @param {string} propName * @param {string} propName
* @returns {Any} * @returns {Any}
* @memberof CustomComponentForAPI * @memberof CustomComponentForAPI
*/ */
getReadTime(propName) { getLatestReadTime(propName) {
return this.getReadDataAndTime(propName).readAt; return this.getLatestReadDataAndTime(propName).readAt;
} }
/** /**
* Access to all the data loaded from the api given the props * Access to all the latest data loaded from the api given the props
*
* read, create and update are taken into account
* *
* @returns {object} * @returns {object}
* @memberof CustomComponentForAPI * @memberof CustomComponentForAPI
*/ */
getAllReadData() { getAllLatestReadData() {
let out = Object(); let out = Object();
this.apiProps.forEach((propName) => { this.apiProps.forEach((propName) => {
out[propName] = this.getReadData(propName); out[propName] = this.getLatestReadData(propName);
}); });
return out; return out;
} }
/** /**
* Access to the read data from the propNames array * Access to the latest read data from the propNames array
* This should be used instead of getAllReadData to optimize things * This should be used instead of getAllLatestReadData to optimize things
*
* read, create and update are taken into account
* *
* @param {array} propNames Array of the prop names you want to read data from * @param {array} propNames Array of the prop names you want to read data from
* @returns {object} * @returns {object}
* @memberof CustomComponentForAPI * @memberof CustomComponentForAPI
*/ */
getReadDataFor(propNames) { getLatestReadDataFor(propNames) {
let out = Object(); let out = Object();
propNames.forEach(propName => out[propName] = this.getReadData(propName)); propNames.forEach(propName => out[propName] = this.getLatestReadData(propName));
return out; return out;
} }
...@@ -274,7 +290,7 @@ class CustomComponentForAPI extends Component { ...@@ -274,7 +290,7 @@ class CustomComponentForAPI extends Component {
* @memberof CustomComponentForAPI * @memberof CustomComponentForAPI
*/ */
joinCampus(campus) { joinCampus(campus) {
const { universities, countries, cities } = this.getReadDataFor(["universities", "countries", "cities"]); const { universities, countries, cities } = this.getLatestReadDataFor(["universities", "countries", "cities"]);
let res = Object.assign({}, campus); //copy for safety let res = Object.assign({}, campus); //copy for safety
res.university = universities.find(univ => univ.id = campus.university); res.university = universities.find(univ => univ.id = campus.university);
res.city = cities.find(city => city.id = campus.city); res.city = cities.find(city => city.id = campus.city);
...@@ -292,7 +308,7 @@ class CustomComponentForAPI extends Component { ...@@ -292,7 +308,7 @@ class CustomComponentForAPI extends Component {
*/ */
getUnivCityAndCountry(univId) { getUnivCityAndCountry(univId) {
const univMainCampus = this.findMainCampus(univId), const univMainCampus = this.findMainCampus(univId),
{ countries, cities } = this.getReadDataFor(["countries", "cities"]), { countries, cities } = this.getLatestReadDataFor(["countries", "cities"]),
city = cities.find(city => city.id == univMainCampus.city), city = cities.find(city => city.id == univMainCampus.city),
country = countries.find(country => country.id == city.country); country = countries.find(country => country.id == city.country);
return { city, country }; return { city, country };
...@@ -307,7 +323,7 @@ class CustomComponentForAPI extends Component { ...@@ -307,7 +323,7 @@ class CustomComponentForAPI extends Component {
* @memberof CustomComponentForAPI * @memberof CustomComponentForAPI
*/ */
findMainCampus(univId) { findMainCampus(univId) {
const mainCampuses = this.getReadData("mainCampuses"); const mainCampuses = this.getLatestReadData("mainCampuses");
return mainCampuses.find(campus => campus.university == univId); return mainCampuses.find(campus => campus.university == univId);
} }
} }
......
...@@ -37,13 +37,13 @@ class ThemeProvider extends CustomComponentForAPI { ...@@ -37,13 +37,13 @@ class ThemeProvider extends CustomComponentForAPI {
const currentTheme = this.state.theme, const currentTheme = this.state.theme,
themeSavedInTheApp = this.props.themeSavedInTheApp.theme, themeSavedInTheApp = this.props.themeSavedInTheApp.theme,
themeSavedInTheAppAt = this.props.themeSavedInTheApp.savedAt, themeSavedInTheAppAt = this.props.themeSavedInTheApp.savedAt,
userDataAndTime = this.getReadDataAndTime("userData"), userDataAndTime = this.getLatestReadDataAndTime("userData"),
themeOnServer = userDataAndTime.data.config.theme, themeOnServer = userDataAndTime.data.config.theme,
themeOnServerRetrievedAt = userDataAndTime.readAt; themeOnServerRetrievedAt = userDataAndTime.readAt;
if (!themeOnServer) { if (!themeOnServer) {
// config on server is raw, we need to add the default one. // config on server is raw, we need to add the default one.
const userData = this.getReadData("userData"); const userData = this.getLatestReadData("userData");
let newUserData = Object.assign({}, userData); let newUserData = Object.assign({}, userData);
newUserData.config.theme = this.state.theme; newUserData.config.theme = this.state.theme;
this.props.saveUserData(newUserData); this.props.saveUserData(newUserData);
......
...@@ -59,7 +59,7 @@ class Filter extends CustomComponentForAPI { ...@@ -59,7 +59,7 @@ class Filter extends CustomComponentForAPI {
* @memberof Filter * @memberof Filter
*/ */
getCountriesWhereThereAreUniversities() { getCountriesWhereThereAreUniversities() {
const mainCampuses = this.getReadData("mainCampuses"); const mainCampuses = this.getLatestReadData("mainCampuses");
// use of map to get only each country once // use of map to get only each country once
let res = new Map(); let res = new Map();
...@@ -76,7 +76,7 @@ class Filter extends CustomComponentForAPI { ...@@ -76,7 +76,7 @@ class Filter extends CustomComponentForAPI {
updateSelectedUniversities(selection) { updateSelectedUniversities(selection) {
const mainCampuses = this.getReadData("mainCampuses"), const mainCampuses = this.getLatestReadData("mainCampuses"),
listOfCountries = __map(selection, (s) => s.id); listOfCountries = __map(selection, (s) => s.id);
let selectedUniversities = []; let selectedUniversities = [];
......
...@@ -26,7 +26,7 @@ class UnivMarkers extends CustomComponentForAPI { ...@@ -26,7 +26,7 @@ class UnivMarkers extends CustomComponentForAPI {
const { universities, const { universities,
mainCampuses, mainCampuses,
countries, countries,
cities } = this.getAllReadData(); cities } = this.getAllLatestReadData();
// some conversions for optimization (faster search of element in a map) // some conversions for optimization (faster search of element in a map)
const universitiesMap = arrayOfInstancesToMap(universities), const universitiesMap = arrayOfInstancesToMap(universities),
......
...@@ -39,7 +39,7 @@ class PageUniversity extends CustomComponentForAPI { ...@@ -39,7 +39,7 @@ class PageUniversity extends CustomComponentForAPI {
if (this.props.universities.readSucceeded.readAt) { if (this.props.universities.readSucceeded.readAt) {
// we have the university data // we have the university data
const universities = this.getReadData("universities"), const universities = this.getLatestReadData("universities"),
{ match, universityBeingViewed } = this.props, { match, universityBeingViewed } = this.props,
requestedUniversity = match.params.id; requestedUniversity = match.params.id;
...@@ -55,7 +55,7 @@ class PageUniversity extends CustomComponentForAPI { ...@@ -55,7 +55,7 @@ class PageUniversity extends CustomComponentForAPI {
customRender() { customRender() {
const { match, universityBeingViewed } = this.props, const { match, universityBeingViewed } = this.props,
requestedUnivId = match.params.id, requestedUnivId = match.params.id,
universities = this.getReadData("universities"); universities = this.getLatestReadData("universities");
if (requestedUnivId == "undefined") { if (requestedUnivId == "undefined") {
if (universityBeingViewed != null) { if (universityBeingViewed != null) {
......
...@@ -33,7 +33,7 @@ class Search extends CustomComponentForAPI { ...@@ -33,7 +33,7 @@ class Search extends CustomComponentForAPI {
* @memberof Search * @memberof Search
*/ */
getSuggestions(input) { getSuggestions(input) {
const universities = this.getReadData("universities"), const universities = this.getLatestReadData("universities"),
filter = fuzzysort.go(input, universities, { keys: ["name", "acronym"] }); filter = fuzzysort.go(input, universities, { keys: ["name", "acronym"] });
let suggestions = filter.map(item => item.obj); let suggestions = filter.map(item => item.obj);
......
...@@ -74,10 +74,10 @@ class ColorTool extends CustomComponentForAPI { ...@@ -74,10 +74,10 @@ class ColorTool extends CustomComponentForAPI {
return; return;
} }
const configOnServer = this.getReadData("userData").config, const configOnServer = this.getLatestReadData("userData").config,
themeOnServer = configOnServer.theme, themeOnServer = configOnServer.theme,
colorPickerConfigOnServer = configOnServer.colorPicker, colorPickerConfigOnServer = configOnServer.colorPicker,
serverDataRetrievedAt = this.getReadTime("userData"); serverDataRetrievedAt = this.getLatestReadTime("userData");
if ((!themeOnServer) || (!colorPickerConfigOnServer)) { if ((!themeOnServer) || (!colorPickerConfigOnServer)) {
// There is nothing to restore // There is nothing to restore
...@@ -175,7 +175,7 @@ class ColorTool extends CustomComponentForAPI { ...@@ -175,7 +175,7 @@ class ColorTool extends CustomComponentForAPI {
} }
handleSendToServer = () => { handleSendToServer = () => {
const userData = this.getReadData("userData"); const userData = this.getLatestReadData("userData");
let newUserData = Object.assign({}, userData); let newUserData = Object.assign({}, userData);
newUserData.config.theme = this.getThemeFromProps(); newUserData.config.theme = this.getThemeFromProps();
......
...@@ -21,7 +21,7 @@ class UnivInfoProvider extends CustomComponentForAPI { ...@@ -21,7 +21,7 @@ class UnivInfoProvider extends CustomComponentForAPI {
customRender() { customRender() {
const { univId } = this.props, const { univId } = this.props,
{ city, country } = this.getUnivCityAndCountry(univId), { city, country } = this.getUnivCityAndCountry(univId),
{ universities, countries, currencies } = this.getReadDataFor(["universities", "countries", "currencies"]), { universities, countries, currencies } = this.getLatestReadDataFor(["universities", "countries", "currencies"]),
countryId = country.id, countryId = country.id,
cityId = city.id; cityId = city.id;
......
...@@ -29,7 +29,7 @@ function renderCore(rawModelData, classes, outsideData) { ...@@ -29,7 +29,7 @@ function renderCore(rawModelData, classes, outsideData) {
class CountryDri extends Module { class CountryDri extends Module {
customRender() { customRender() {
const countryDriItems = this.getReadData("countryDri", false), const countryDriItems = this.getOnlyReadData("countryDri"),
{ countries, classes } = this.props, { countries, classes } = this.props,
outsideData = { countries }; outsideData = { countries };
......
...@@ -44,7 +44,7 @@ function renderCore(rawModelData, classes, outsideData) { ...@@ -44,7 +44,7 @@ function renderCore(rawModelData, classes, outsideData) {
class CountryScholarships extends Module { class CountryScholarships extends Module {
customRender() { customRender() {
const countryScholarshipsItems = this.getReadData("countryScholarships", false), const countryScholarshipsItems = this.getOnlyReadData("countryScholarships"),
{ countries, currencies, classes } = this.props, { countries, currencies, classes } = this.props,
outsideData = { outsideData = {
countries, countries,
......
...@@ -35,7 +35,7 @@ class UniversityDri extends Module { ...@@ -35,7 +35,7 @@ class UniversityDri extends Module {
customRender() { customRender() {
const { universities, classes } = this.props, const { universities, classes } = this.props,
outsideData = { universities }, outsideData = { universities },
univDriItems = this.getReadData("universityDri", false); univDriItems = this.getLatestReadData("universityDri", false);
return ( return (
<ModuleGroupWrapper <ModuleGroupWrapper
......
...@@ -58,7 +58,7 @@ function renderCore(rawModelData, classes, outsideData) { ...@@ -58,7 +58,7 @@ function renderCore(rawModelData, classes, outsideData) {
class UniversityGeneral extends Module { class UniversityGeneral extends Module {
customRender() { customRender() {
const univInfos = this.getReadData("university"); const univInfos = this.getLatestReadData("university");
const { classes } = this.props; const { classes } = this.props;
const outsideData = { const outsideData = {
city: this.props.city.name, city: this.props.city.name,
......
...@@ -39,7 +39,7 @@ function renderCore(rawModelData, classes, outsideData) { ...@@ -39,7 +39,7 @@ function renderCore(rawModelData, classes, outsideData) {
class UniversityScholarships extends Module { class UniversityScholarships extends Module {
customRender() { customRender() {
const univScholarshipsItems = this.getReadData("universityScholarships", false), const univScholarshipsItems = this.getOnlyReadData("universityScholarships"),
{ universities, currencies, classes } = this.props, { universities, currencies, classes } = this.props,
outsideData = { outsideData = {
universities, universities,
......
...@@ -105,7 +105,7 @@ function renderCore(rawModelData, classes) { ...@@ -105,7 +105,7 @@ function renderCore(rawModelData, classes) {
class UniversitySemestersDates extends Module { class UniversitySemestersDates extends Module {
customRender() { customRender() {
const semestersDates = this.getReadData("universitySemestersDates"); const semestersDates = this.getLatestReadData("universitySemestersDates");
const { classes } = this.props; const { classes } = this.props;
return ( return (
<ModuleWrapper <ModuleWrapper
......
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