Filter.jsx 5.75 KB
Newer Older
1
import React, { useEffect } from "react";
2
3
4
5
6
import ExpansionPanel from "@material-ui/core/ExpansionPanel";
import ExpansionPanelSummary from "@material-ui/core/ExpansionPanelSummary";
import ExpansionPanelDetails from "@material-ui/core/ExpansionPanelDetails";
import ExpandMoreIcon from "@material-ui/icons/ExpandMore";
import Typography from "@material-ui/core/Typography";
7
import { v4 as uuidv4 } from "uuid";
8
import { makeStyles } from "@material-ui/styles";
9
10
11
import { Checkbox } from "@material-ui/core";
import FormControl from "@material-ui/core/FormControl";
import FormControlLabel from "@material-ui/core/FormControlLabel";
12
import DownshiftMultiple from "../common/DownshiftMultiple";
13
import useGlobalState from "../../hooks/useGlobalState";
14
15
import FilterService from "../../services/FilterService";
import FilterStatus from "./FilterStatus";
Florent Chehab's avatar
Florent Chehab committed
16
import { useSetSelectedUniversities } from "../../hooks/wrappers/useSelectedUniversities";
17

18
const useStyles = makeStyles((theme) => ({
Florent Chehab's avatar
Florent Chehab committed
19
  root: {
20
    width: "100%",
Florent Chehab's avatar
Florent Chehab committed
21
22
23
  },
  heading: {
    fontSize: theme.typography.pxToRem(15),
24
    fontWeight: theme.typography.fontWeightMedium,
Florent Chehab's avatar
Florent Chehab committed
25
  },
Florent Chehab's avatar
Florent Chehab committed
26
27
  input: {
    marginTop: theme.spacing(1),
28
    marginBottom: theme.spacing(0.5),
Florent Chehab's avatar
Florent Chehab committed
29
30
  },
  spacer1: {
31
    marginTop: theme.spacing(1),
Florent Chehab's avatar
Florent Chehab committed
32
33
  },
  spacer2: {
34
35
    marginTop: theme.spacing(2),
  },
36
37
}));

38
39
40
const DOWNSHIFT_COUNTRIES_ID = uuidv4();
const DOWNSHIFT_SEMESTERS_ID = uuidv4();
const DOWNSHIFT_MAJORS_ID = uuidv4();
41
const MINIMUM_NUMBER_OPEN_DESTINATIONS = 100;
42
43
44
45
46
47

/**
 * Implementation of a filter component
 */
function Filter() {
  const classes = useStyles();
Florent Chehab's avatar
Florent Chehab committed
48
  const saveSelectedUniversities = useSetSelectedUniversities();
49

50
51
52
  const [isOpened, setIsOpened] = useGlobalState("filter-open", false);
  const [countries, setCountries] = useGlobalState("filter-countries", []);
  const [semesters, setSemesters] = useGlobalState(
53
54
55
    "filter-semesters",
    [...FilterService.defaultSemesters].reverse()
  );
56

57
  const [majorMinors, setMajorMinors] = useGlobalState(
58
59
60
61
    "filter-major-minors",
    []
  );

62
63
64
  const enoughDestinationsAreOpen =
    FilterService.destinationOpenCount >= MINIMUM_NUMBER_OPEN_DESTINATIONS;

65
66
  const [onlyOpenDestinations, setOnlyOpenDestinations] = useGlobalState(
    "filter-only-open-destinations",
67
    enoughDestinationsAreOpen
68
69
  );

70
71
72
73
  useEffect(() => {
    const selectedUniversities = FilterService.getSelection(
      countries,
      semesters,
74
75
      majorMinors,
      onlyOpenDestinations
76
77
    );

78
    const hasSelection =
79
      [countries, semesters, majorMinors].some((arr) => arr.length !== 0) ||
80
      onlyOpenDestinations === true;
Florent Chehab's avatar
Florent Chehab committed
81
    saveSelectedUniversities(hasSelection ? selectedUniversities : null);
82
  }, [countries, semesters, majorMinors, onlyOpenDestinations]);
83
84
85
86

  const {
    countriesOptions,
    majorMinorOptions,
87
    semesterOptions,
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  } = FilterService;

  return (
    <ExpansionPanel
      expanded={isOpened}
      onChange={() => {
        setIsOpened(!isOpened);
      }}
    >
      <ExpansionPanelSummary expandIcon={<ExpandMoreIcon />}>
        <Typography className={classes.heading}>
          Appliquer des filtres
        </Typography>
        <FilterStatus />
      </ExpansionPanelSummary>
      <ExpansionPanelDetails style={{ display: "block" }}>
        <Typography variant="caption">
          Le options internes des filtres sont composées avec un « ou » logique.
          Les filtres sont composés entre eux avec un « et » logique.
        </Typography>
        <div className={classes.spacer1} />
        <div className={classes.input}>
          <DownshiftMultiple
            fieldPlaceholder="Filter par pays"
            options={countriesOptions}
            onChange={setCountries}
            cacheId={DOWNSHIFT_COUNTRIES_ID}
          />
        </div>
        <div className={classes.spacer2} />
        <Typography>Filtres avancés</Typography>
        <Typography variant="caption">
          REX-DRI s'efforce d'être à jour avec l'ENT. Toutefois, seul l'ENT fait
          foi à 100% concernant les possibilités passées et actuelles
          d'échanges.
        </Typography>
        <div className={classes.input}>
          <DownshiftMultiple
            fieldPlaceholder={
              "Filter par semestre (lorsqu'un échange est/était possible)"
            }
            options={semesterOptions}
            onChange={setSemesters}
            cacheId={DOWNSHIFT_SEMESTERS_ID}
            value={semesters}
          />
        </div>
        <div className={classes.input}>
          <DownshiftMultiple
            fieldPlaceholder={
              "Filter par branches et filières (lorsqu'un échange est/était possible)"
            }
            options={majorMinorOptions}
            onChange={setMajorMinors}
            cacheId={DOWNSHIFT_MAJORS_ID}
          />
          <Typography variant="caption">
145
146
147
148
149
150
            Ce filtre prend en compte les échanges effectués depuis le semestre
            d'automne 2008, et les possibilités d'échanges enregistrées par la
            DRI depuis juillet 2019.
            <br />
            Attention, lorsqu'un filtre comportant une filière est appliqué,
            seuls les échanges déjà effectués sont pris en compte.
151
152
          </Typography>
        </div>
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
        <div className={classes.input}>
          <FormControl>
            <FormControlLabel
              control={
                <Checkbox
                  color="primary"
                  checked={onlyOpenDestinations}
                  onChange={() =>
                    setOnlyOpenDestinations(!onlyOpenDestinations)
                  }
                />
              }
              label="Filtrer par destinations disponibles actuellement"
              labelPlacement="end"
            />
          </FormControl>
        </div>
170
171
172
173
      </ExpansionPanelDetails>
    </ExpansionPanel>
  );
}
Florent Chehab's avatar
Florent Chehab committed
174

175
export default Filter;