UnivInfoProvider.jsx 1003 Bytes
Newer Older
1
2
import React from "react";
import PropTypes from "prop-types";
Florent Chehab's avatar
Florent Chehab committed
3
4
import UnivContext from "../../contexts/UnivContext";
import UniversityService from "../../services/data/UniversityService";
5
import CountryService from "../../services/data/CountryService";
Florent Chehab's avatar
Florent Chehab committed
6

Florent Chehab's avatar
Florent Chehab committed
7
/**
8
 * Univ info provider to pre-fetch some data and make it available down the tree very easily
Florent Chehab's avatar
Florent Chehab committed
9
 */
10
11
function UnivInfoProvider({ univId, children }) {
  const univIdParsed = parseInt(univId, 10);
12
  const univ = UniversityService.getUniversityById(univIdParsed);
13
14
  const country = CountryService.getCountryForCountryId(univ.country);
  const { city } = univ;
15
16
17
18
  const countryId = country.id;

  return (
    <UnivContext.Provider
19
      value={{
20
21
22
23
24
        univId: univIdParsed,
        countryId,
        univ,
        city,
        country
25
26
27
28
29
      }}
    >
      {children}
    </UnivContext.Provider>
  );
Florent Chehab's avatar
Florent Chehab committed
30
31
}

Florent Chehab's avatar
Florent Chehab committed
32
UnivInfoProvider.propTypes = {
33
34
  univId: PropTypes.number.isRequired,
  children: PropTypes.node.isRequired
Florent Chehab's avatar
Florent Chehab committed
35
36
};

37
export default UnivInfoProvider;