Commit a21ede50 authored by Florent Chehab's avatar Florent Chehab

Clean(front): services / helpers

parent 96b690f5
/** General app JS entry
*/
import React from "react";
import React, { useEffect } from "react";
import { compose } from "recompose";
import { Route, Switch } from "react-router-dom";
import FullScreenDialogServiceComponent from "../services/FullScreenDialogServiceComponent";
......@@ -29,13 +29,29 @@ import withNetworkWrapper, { NetWrapParam } from "../../hoc/withNetworkWrapper";
import FullScreenDialogService from "../../services/FullScreenDialogService";
import NotificationServiceComponent from "../services/NotificationServiceComponent";
import NotificationService from "../../services/NotificationService";
import UniversityService from "../../services/data/UniversityService";
import CityService from "../../services/data/CityService";
import CountryService from "../../services/data/CountryService";
import CurrencyService from "../../services/data/CurrencyService";
import LanguageService from "../../services/data/LanguageService";
const SERVICES_TO_INITIALIZE = [
UniversityService,
CityService,
CountryService,
CurrencyService,
LanguageService
];
// import PageFiles from "../pages/PageFiles";
/**
* Main entry
*/
function App() {
useEffect(() => {
SERVICES_TO_INITIALIZE.forEach(service => service.initialize());
}, []);
return (
<div
style={{
......
......@@ -366,62 +366,6 @@ class CustomComponentForAPI extends Component {
});
return out;
}
// utilities functions shared by subclasses
/**
* Function to retrieve all the information relative to a campus
*
* @param {object} campus instance of Campus
* @returns {object} Campus with replaced university, city and country with the matching instance
* @memberof CustomComponentForAPI
*/
joinCampus(campus) {
const { universities, countries, cities } = this.getLatestReadDataFor([
"universities",
"countries",
"cities"
]);
const res = { ...campus }; // copy for safety
res.university = universities.find(univ => univ.id === campus.university);
res.city = cities.find(city => city.id === campus.city);
res.country = countries.find(country => country.id === res.city.country);
return res;
}
/**
* Function to get the city and the country of a university given a university id
*
* @param {number} univId
* @returns {object} Object with city and country instance of the university (main campus)
* @memberof CustomComponentForAPI
*/
getUnivCityAndCountry(univId) {
const univMainCampus = this.findMainCampus(univId);
const { countries, cities } = this.getLatestReadDataFor([
"countries",
"cities"
]);
const city = cities.find(cityEl => cityEl.id === univMainCampus.city);
const country = countries.find(countryEl => countryEl.id === city.country);
return {
city,
country
};
}
/**
* Function that returns the main campus instance associated with a university
* identified by its id.
*
* @param {number} univId
* @returns {object}
* @memberof CustomComponentForAPI
*/
findMainCampus(univId) {
const mainCampuses = this.getLatestReadData("mainCampuses");
return mainCampuses.find(campus => campus.university === univId);
}
}
CustomComponentForAPI.propTypes = {
......
......@@ -17,8 +17,8 @@ import getActions from "../../redux/api/getActions";
import { saveSelectedUniversities } from "../../redux/actions/filter";
import CustomComponentForAPI from "../common/CustomComponentForAPI";
import DownshiftMultiple from "../common/DownshiftMultiple";
import { UniversityHelper } from "../../redux/api/helpers";
import { getMostNRecentSemesters } from "../../utils/compareSemesters";
import UniversityService from "../../services/data/UniversityService";
// TODO bug when directly loading map
......@@ -148,10 +148,6 @@ class FilterHandler {
return FilterHandler._defaultSemesters;
}
getUnivForUnivId(univId) {
return UniversityHelper.getUniv(univId);
}
getMajorInUniv(univObj) {
return [
...new Set(
......@@ -216,7 +212,7 @@ class FilterHandler {
selectedCountriesCode.length === 0
? this.universities
: this.getUniversitiesInCountries(selectedCountriesCode).map(id =>
this.getUnivForUnivId(id)
UniversityService.getUniversityById(id)
);
if (selectedSemesters.length > 0) {
......
......@@ -18,8 +18,8 @@ import MultiSelectField from "./fields/MultiSelectField";
import NumberField from "./fields/NumberField";
import { getLatestReadDataFromStore } from "../../redux/api/utils";
import { LanguagesHelper } from "../../redux/api/helpers";
import HiddenField from "./fields/HiddenField";
import LanguageService from "../../services/data/LanguageService";
export default {
/**
......@@ -185,7 +185,7 @@ export default {
renderLanguageField(props) {
return this.renderSelectField({
...props,
options: LanguagesHelper.getLanguagesOptions()
options: LanguageService.getLanguagesOptions()
});
},
......
......@@ -12,7 +12,7 @@ import CustomNavLink from "../../common/CustomNavLink";
import APP_ROUTES from "../../../config/appRoutes";
import MetricFeedback from "../../common/MetricFeedback";
import OnBlurContainer from "../../common/OnBlurContainer";
import { UniversityHelper } from "../../../redux/api/helpers";
import UniversityService from "../../../services/data/UniversityService";
const useStyle = makeStyles(theme => ({
paper: {
......@@ -51,9 +51,9 @@ function UnivBlock(props) {
const univId = content.university;
const { appreciation } = content;
const univInfo = UniversityHelper.getUniv(univId);
const univInfo = UniversityService.getUniversityById(univId);
const options = UniversityHelper.getUniversitiesOptions();
const options = UniversityService.getUniversitiesOptions();
function handleAppreciationChange(value) {
const regex = /[^\d]*/gi;
......
import React from "react";
import PropTypes from "prop-types";
import UnivContext from "./UnivContext";
import { UniversityHelper } from "../../../redux/api/helpers";
import UniversityService from "../../../services/data/UniversityService";
/**
......@@ -9,7 +8,7 @@ import UniversityService from "../../../services/data/UniversityService";
*/
function UnivInfoProvider({ univId, children }) {
const univIdParsed = parseInt(univId, 10);
const univ = UniversityHelper.getUniv(univIdParsed);
const univ = UniversityService.getUniversityById(univIdParsed);
const { city, country } = UniversityService.getUnivCityAndCountry(
univIdParsed
);
......
......@@ -8,10 +8,10 @@ import Divider from "@material-ui/core/Divider";
import ChillIcon from "@material-ui/icons/LocalBar";
import HardCoreIcon from "@material-ui/icons/FitnessCenter";
import TruncatedMarkdown from "../../../common/markdown/TruncatedMarkdown";
import { LanguagesHelper } from "../../../../redux/api/helpers";
import MetricFeedback from "../../../common/MetricFeedback";
import TextLink from "../../../common/TextLink";
import { GridColumn, GridLine } from "./common";
import LanguageService from "../../../../services/data/LanguageService";
const useStyles = makeStyles(theme => ({
feedbackHeaderText: {
......@@ -54,7 +54,7 @@ export function CourseFeedbackCore(props) {
wouldRecommend,
untouched
} = props;
const languageName = LanguagesHelper.getLanguageName(languageCode);
const languageName = LanguageService.getLanguageName(languageCode);
const secondaryChipLabel = `${courseCategory ? ` ${courseCategory}` : ""}${
courseProfile ? ` — ${courseProfile}` : ""
}`;
......@@ -109,7 +109,7 @@ export function CourseFeedbackCore(props) {
{languageName ? (
<Typography>
Cours enseigné en
{LanguagesHelper.getLanguageName(languageCode)}.
{LanguageService.getLanguageName(languageCode)}.
</Typography>
) : (
<></>
......
import { getLatestReadDataFromStore } from "./utils";
// TODO move to data services
export class LanguagesHelper {
/**
* @type {Map<number, string>}
* @private
*/
static _languageMap = new Map();
/**
* @private
* @type {array.<object>}
*/
static _options = undefined;
static getLanguageName(code) {
const languages = getLatestReadDataFromStore("languagesAll");
if (!LanguagesHelper._languageMap.has(code)) {
languages.forEach(({ id, name }) =>
LanguagesHelper._languageMap.set(id, name)
);
}
return LanguagesHelper._languageMap.get(code);
}
static getLanguagesOptions() {
if (typeof LanguagesHelper._options === "undefined") {
LanguagesHelper._options = getLatestReadDataFromStore("languagesAll").map(
el => ({
value: el.id,
label: el.name
})
);
}
return LanguagesHelper._options;
}
}
export class UniversityHelper {
/**
* @private
* @type {Map}
*/
static _lookup = undefined;
/**
* @private
* @type {array.<object>}
*/
static _options = undefined;
static getUniv(univId) {
if (typeof UniversityHelper._lookup === "undefined") {
UniversityHelper._lookup = new Map();
getLatestReadDataFromStore("universitiesAll").forEach(univ =>
UniversityHelper._lookup.set(univ.id, univ)
);
}
return UniversityHelper._lookup.get(univId);
}
static getUniversitiesOptions() {
if (typeof UniversityHelper._options === "undefined") {
UniversityHelper._options = getLatestReadDataFromStore(
"universitiesAll"
).map(el => ({
value: el.id,
label: el.name
}));
}
return UniversityHelper._options;
}
}
......@@ -4,13 +4,6 @@ import arrayOfInstancesToMap from "../../utils/arrayOfInstancesToMap";
// TODO add services to documentation
class CityService {
/**
* Has the service been initialized yet (pre computation, etc.)
* @type {boolean}
* @private
*/
_initialized = false;
/**
* Stores the cities by its id
* @type {Map<number, object>}
......@@ -19,13 +12,11 @@ class CityService {
_citiesById = new Map();
/**
* @private
* Must be called once before accessing other methods.
*/
_checkInitialized() {
if (!this._initialized) {
const cities = getLatestReadDataFromStore("citiesAll");
this._citiesById = arrayOfInstancesToMap(cities);
}
initialize() {
const cities = getLatestReadDataFromStore("citiesAll");
this._citiesById = arrayOfInstancesToMap(cities);
}
/**
......@@ -35,8 +26,6 @@ class CityService {
* @returns {Object}
*/
getCityForCityId(cityId) {
this._checkInitialized();
return this._citiesById.get(cityId);
}
}
......
......@@ -2,13 +2,6 @@ import { getLatestReadDataFromStore } from "../../redux/api/utils";
import arrayOfInstancesToMap from "../../utils/arrayOfInstancesToMap";
class CountryService {
/**
* Has the service been initialized yet (pre computation, etc.)
* @type {boolean}
* @private
*/
_initialized = false;
/**
* Stores the countries by its id
* @type {Map<number, object>}
......@@ -17,13 +10,11 @@ class CountryService {
_countriesById = new Map();
/**
* @private
* Must be called once before accessing other methods.
*/
_checkInitialized() {
if (!this._initialized) {
const countries = this.getCountries();
this._countriesById = arrayOfInstancesToMap(countries);
}
initialize() {
const countries = this.getCountries();
this._countriesById = arrayOfInstancesToMap(countries);
}
/**
......@@ -33,8 +24,6 @@ class CountryService {
* @returns {Object}
*/
getCountryForCountryId(countryId) {
this._checkInitialized();
return this._countriesById.get(countryId);
}
......
......@@ -2,13 +2,6 @@ import { getLatestReadDataFromStore } from "../../redux/api/utils";
import arrayOfInstancesToMap from "../../utils/arrayOfInstancesToMap";
class CurrencyService {
/**
* Has the service been initialized yet (pre computation, etc.)
* @type {boolean}
* @private
*/
_initialized = false;
/**
* Stores the currencies by currency id for quick join
* @type {Map<number, object>}
......@@ -16,12 +9,11 @@ class CurrencyService {
*/
_currenciesByCurrencyId = new Map();
_checkInitialized() {
if (!this._initialized) {
this._currenciesByCurrencyId = arrayOfInstancesToMap(
this.getCurrencies()
);
}
/**
* Must be called once before accessing other methods.
*/
initialize() {
this._currenciesByCurrencyId = arrayOfInstancesToMap(this.getCurrencies());
}
getCurrencies() {
......@@ -34,8 +26,6 @@ class CurrencyService {
* @returns {Object}
*/
getCurrencyForCurrencyId(currencyId) {
this._checkInitialized();
return this._currenciesByCurrencyId.get(currencyId);
}
}
......
import { getLatestReadDataFromStore } from "../../redux/api/utils";
import arrayOfInstancesToMap from "../../utils/arrayOfInstancesToMap";
class LanguageService {
/**
* Stores the languages by its id
* @type {Map<number, object>}
* @private
*/
_languagesById = new Map();
/**
* @private
* @type {array.<object>}
*/
_options = [];
/**
* Must be called once before accessing other methods.
*/
initialize() {
const languages = this.getLanguages();
this._languagesById = arrayOfInstancesToMap(languages);
this._options = languages.map(el => ({
value: el.id,
label: el.name
}));
}
/**
* Get the country object for a country id
*
* @param {number} countryId
* @returns {Object}
*/
getLanguageForLanguageId(countryId) {
return this._languagesById.get(countryId);
}
getLanguageName(code) {
return this.getLanguageForLanguageId(code).name;
}
/**
* @returns {Array.<Object>}
*/
getLanguages() {
return getLatestReadDataFromStore("languagesAll");
}
/**
* @returns {Array<Object>}
*/
getLanguagesOptions() {
return this._options;
}
}
export default new LanguageService();
......@@ -4,13 +4,6 @@ import CountryService from "./CountryService";
import arrayOfInstancesToMap from "../../utils/arrayOfInstancesToMap";
class UniversityService {
/**
* Has the service been initialized yet (pre computation, etc.)
* @type {boolean}
* @private
*/
_initialized = false;
/**
* Stores the universities by its id
* @type {Map<number, object>}
......@@ -25,17 +18,29 @@ class UniversityService {
*/
_mainCampusesByUnivId = new Map();
_checkInitialized() {
if (!this._initialized) {
const universities = getLatestReadDataFromStore("universitiesAll");
this._universitiesById = arrayOfInstancesToMap(universities);
/**
* @private
* @type {array.<object>}
*/
_options = [];
/**
* Must be called once before accessing other methods.
*/
initialize() {
const universities = getLatestReadDataFromStore("universitiesAll");
this._universitiesById = arrayOfInstancesToMap(universities);
const mainCampuses = this.getMainCampuses();
const mainCampuses = this.getMainCampuses();
mainCampuses.forEach(campus => {
this._mainCampusesByUnivId.set(campus.university, campus);
});
}
mainCampuses.forEach(campus => {
this._mainCampusesByUnivId.set(campus.university, campus);
});
this._options = universities.map(el => ({
value: el.id,
label: el.name
}));
}
/**
......@@ -46,6 +51,10 @@ class UniversityService {
return getLatestReadDataFromStore("universitiesAll");
}
getUniversitiesOptions() {
return this._options;
}
/**
* Get the full list of main campuses in the app
* @returns {array<Object>}
......@@ -59,8 +68,6 @@ class UniversityService {
* @returns {Object}
*/
getUniversityById(univId) {
this._checkInitialized();
return this._universitiesById.get(univId);
}
......@@ -71,8 +78,6 @@ class UniversityService {
* @returns {Object}
*/
getCampusForUnivId(univId) {
this._checkInitialized();
return this._mainCampusesByUnivId.get(univId);
}
......@@ -83,8 +88,6 @@ class UniversityService {
* @returns {object} Object with city and country instance of the university (main campus)
*/
getUnivCityAndCountry(univId) {
this._checkInitialized();
const univMainCampus = this.getCampusForUnivId(univId);
const city = CityService.getCityForCityId(univMainCampus.city);
const country = CountryService.getCountryForCountryId(city.country);
......
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