Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Rex Dri
Rex Dri
Commits
fae1c3c3
Commit
fae1c3c3
authored
Sep 04, 2019
by
Florent Chehab
Committed by
Florent Chehab
Jan 12, 2020
Browse files
Clean(front): services / helpers
parent
ba5de11e
Changes
13
Hide whitespace changes
Inline
Side-by-side
frontend/src/components/app/App.jsx
View file @
fae1c3c3
/** 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
=
{
{
...
...
frontend/src/components/common/CustomComponentForAPI.jsx
View file @
fae1c3c3
...
...
@@ -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
=
{
...
...
frontend/src/components/filter/Filter.jsx
View file @
fae1c3c3
...
...
@@ -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
.
getUnivForUniv
Id
(
id
)
UniversityService
.
getUniversityBy
Id
(
id
)
);
if
(
selectedSemesters
.
length
>
0
)
{
...
...
frontend/src/components/form/renderFieldsMixIn.jsx
View file @
fae1c3c3
...
...
@@ -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
:
Language
sHelper
.
getLanguagesOptions
()
options
:
Language
Service
.
getLanguagesOptions
()
});
},
...
...
frontend/src/components/recommendation/view/UnivBlock.jsx
View file @
fae1c3c3
...
...
@@ -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
{
University
Helper
}
from
"
../../../
redux/api/helpers
"
;
import
University
Service
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
=
University
Helper
.
getUniv
(
univId
);
const
univInfo
=
University
Service
.
getUniv
ersityById
(
univId
);
const
options
=
University
Helper
.
getUniversitiesOptions
();
const
options
=
University
Service
.
getUniversitiesOptions
();
function
handleAppreciationChange
(
value
)
{
const
regex
=
/
[^\d]
*/gi
;
...
...
frontend/src/components/university/common/UnivInfoProvider.jsx
View file @
fae1c3c3
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
=
University
Helper
.
getUniv
(
univIdParsed
);
const
univ
=
University
Service
.
getUniv
ersityById
(
univIdParsed
);
const
{
city
,
country
}
=
UniversityService
.
getUnivCityAndCountry
(
univIdParsed
);
...
...
frontend/src/components/university/modules/previousExchangeFeedback/CourseFeedback.jsx
View file @
fae1c3c3
...
...
@@ -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
=
Language
sHelper
.
getLanguageName
(
languageCode
);
const
languageName
=
Language
Service
.
getLanguageName
(
languageCode
);
const
secondaryChipLabel
=
`
${
courseCategory
?
`
${
courseCategory
}
`
:
""
}${
courseProfile
?
` —
${
courseProfile
}
`
:
""
}
`
;
...
...
@@ -109,7 +109,7 @@ export function CourseFeedbackCore(props) {
{
languageName
?
(
<
Typography
>
Cours enseigné en
{
Language
sHelper
.
getLanguageName
(
languageCode
)
}
.
{
Language
Service
.
getLanguageName
(
languageCode
)
}
.
</
Typography
>
)
:
(
<></>
...
...
frontend/src/redux/api/helpers.js
deleted
100644 → 0
View file @
ba5de11e
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
;
}
}
frontend/src/services/data/CityService.js
View file @
fae1c3c3
...
...
@@ -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
);
}
}
...
...
frontend/src/services/data/CountryService.js
View file @
fae1c3c3
...
...
@@ -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
);
}
...
...
frontend/src/services/data/CurrencyService.js
View file @
fae1c3c3
...
...
@@ -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
);
}
}
...
...
frontend/src/services/data/LanguageService.js
0 → 100644
View file @
fae1c3c3
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
();
frontend/src/services/data/UniversityService.js
View file @
fae1c3c3
...
...
@@ -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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment