Skip to content

Commit

Permalink
fix: add possibility to choose ISE if not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
eMerzh committed Sep 30, 2023
1 parent 62f6a2f commit c179d37
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
41 changes: 33 additions & 8 deletions src/AppContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Alert, Container, Modal, Text } from "@mantine/core";
import { useMemo, useState } from "react";
import { BooleanParam, JsonParam } from "use-query-params";
import { BooleanParam, JsonParam, NumberParam, withDefault } from "use-query-params";
import { useQueryParam, StringParam } from "use-query-params";
import { useDisclosure, useLocalStorage } from "@mantine/hooks";

import { ComputeResult, computeAll, primarySchools, secondarySchools } from "./compute";
import { ComputeResult, UnexistingSchool, computeAll, primarySchools, secondarySchools } from "./compute";
import { NamedLoc } from "./GeoAutoComplete";
import { InputConfig } from "./InputConfig";
import ResultTable from "./ResultTable";
Expand Down Expand Up @@ -42,6 +42,7 @@ function useConfiguration() {

const [immersion, setImmersion] = useQueryParam("immersion", BooleanParam);
const [date, setDate] = useQueryParam("date", StringParam);
const [ise, setIse] = useQueryParam("ise", withDefault(NumberParam, 10));

return {
idPrimaire,
Expand Down Expand Up @@ -69,6 +70,11 @@ function useConfiguration() {
setDate(v);
refresh();
},
ise,
setIse: (v: number) => {
setIse(v);
refresh();
},
};
}
function AppContainer() {
Expand All @@ -83,20 +89,28 @@ function AppContainer() {
setImmersion,
date,
setDate,
ise,
setIse,
} = useConfiguration();
const [opened, { open, close }] = useDisclosure(false);
const school_prim = primarySchools.find((school) => school.id === idPrimaire);
const detailsSecondaire = secondarySchools.find((school) => school.id === idSecondaire);

const scores = useMemo<ComputeResult[] | null>(() => {
const scores = useMemo<ComputeResult[] | UnexistingSchool | null>(() => {
if (!school_prim || !locHome || !date) return null;

const results = computeAll(secondarySchools, school_prim, locHome, date, immersion);
return results;
try {
const results = computeAll(secondarySchools, school_prim, locHome, date, immersion, ise);
return results;
} catch (e) {
console.error(e);
if (e instanceof UnexistingSchool) {
return e;
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [school_prim?.id, locHome?.lat, locHome?.lon, immersion, date]);
}, [school_prim?.id, locHome?.lat, locHome?.lon, immersion, date, ise]);

if (!school_prim || !locHome || !date) {
if (!scores || scores instanceof UnexistingSchool) {
return (
<Container>
<Warning />
Expand All @@ -110,7 +124,15 @@ function AppContainer() {
setImmersion={setImmersion}
date={date}
setDate={setDate}
ise={ise}
setIse={setIse}
isFaultyDate={scores instanceof UnexistingSchool}
/>
{scores instanceof UnexistingSchool && (
<Alert title="Erreur" color="red" mt={10} mb={10}>
Selon les informations que vous avez entrées, l'école primaire n'existait pas au moment de l'inscription.
</Alert>
)}
</Container>
);
}
Expand All @@ -128,6 +150,8 @@ function AppContainer() {
setImmersion={setImmersion}
date={date}
setDate={setDate}
ise={ise}
setIse={setIse}
/>
<hr />
<Modal
Expand All @@ -146,6 +170,7 @@ function AppContainer() {
locHome={locHome}
date={date}
immersion={immersion}
ise={ise}
/>
)}
</Modal>
Expand Down
25 changes: 22 additions & 3 deletions src/InputConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Checkbox, Group, Select, Text } from "@mantine/core";
import GeoAutoComplete, { NamedLoc } from "./GeoAutoComplete";
import type { School } from "./compute";
import { IconCalendar, IconSchool } from "@tabler/icons-react";
import { forwardRef } from "react";
import { IconCalendar, IconMoneybag, IconSchool } from "@tabler/icons-react";
import { forwardRef, useMemo } from "react";

interface ItemProps extends React.ComponentPropsWithoutRef<"div"> {
city: string;
Expand Down Expand Up @@ -32,6 +32,9 @@ export function InputConfig({
setImmersion,
date,
setDate,
ise,
setIse,
isFaultyDate,
}: {
primarySchools: School[];
idPrimaire: string;
Expand All @@ -42,13 +45,19 @@ export function InputConfig({
setImmersion: (v: boolean) => void;
date: string;
setDate: (v: string) => void;
ise: number;
setIse: (v: number) => void;
isFaultyDate: boolean;
}) {
const prim = primarySchools.map((school) => ({
value: school.id,
label: school.name,
address: school.address,
city: school.city,
}));
const primSchool = useMemo(() => {
return idPrimaire ? primarySchools.find((p) => p.id === idPrimaire) : null;
}, [idPrimaire, primarySchools]);

return (
<>
Expand All @@ -70,11 +79,21 @@ export function InputConfig({
<Select
label="Inscription en 1ere primaire"
data={["2018", "2019", "2020", "2021", "2022"]}
icon={<IconCalendar size="1rem" color={date ? "green" : "#adb5bd"} />}
icon={<IconCalendar size="1rem" color={isFaultyDate ? "red" : date ? "green" : "#adb5bd"} />}
value={date}
onChange={setDate}
mt={"md"}
/>
{idPrimaire && !primSchool.ise && (
<Select
label="Indice Socio-Économique (1= moins favorisé)"
data={Array.from({ length: 20 }, (_, i) => i + 1).map((i) => ({ label: `${i}`, value: `${i}` }))}
icon={<IconMoneybag size="1rem" color={date ? "green" : "#adb5bd"} />}
value={`${ise}`}
onChange={(v) => setIse(parseInt(v, 10))}
mt={"md"}
/>
)}
<Checkbox
label="Immersion"
checked={immersion}
Expand Down
6 changes: 4 additions & 2 deletions src/SchoolDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,14 @@ const SchoolDetail = ({
locHome,
date,
immersion,
ise,
}: {
school: School;
scores: ComputeResult[];
locHome: GeoLoc;
date: string;
immersion: boolean;
ise?: number;
}) => {
let result: ComputeResult | undefined;
const [gridOpened, handlers] = useDisclosure(false);
Expand All @@ -190,8 +192,8 @@ const SchoolDetail = ({
const gridResult = useMemo(() => {
if (!gridOpened) return null;
console.log("compute");
return getScoreGrid(school, result.primarySchool, locHome, date, immersion);
}, [school, result?.primarySchool, locHome, date, immersion, gridOpened]);
return getScoreGrid(school, result.primarySchool, locHome, date, immersion, ise);
}, [school, result?.primarySchool, locHome, date, immersion, gridOpened, ise]);

return (
<Container>
Expand Down
20 changes: 16 additions & 4 deletions src/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ export function hasBothSchoolsNetworkInCity(schools: School[], city: string) {
return !!confessional && !!other;
}

export class UnexistingSchool extends Error {
constructor(message) {
super(message);
this.name = "UnexistingSchool";
}
}

export function compute(
// note: for perf reasons, primary schools should already by filtered by network, newest removed, and ordered by distance
primary: School[],
Expand All @@ -172,11 +179,14 @@ export function compute(
school_sec: School,
locHome: GeoLoc,
immersion: boolean,
ise?: number,
) {
const coef_1 = rankCoef1[0]; // LA PRÉFÉRENCE 1.5 pour 1°

const rank_2 = findNearestRank(primary, school_prim, locHome);

if (rank_2 === 0) {
throw new UnexistingSchool(`Missing primary school ${school_prim.name}`);
}
// const coef_2 = 1.3 // LA PROXIMITÉ ENTRE LE DOMICILE ET L’ÉCOLE PRIMAIRE (meme réseau)
const coef_2 = rankCoef2[rank_2 - 1];

Expand All @@ -197,7 +207,7 @@ export function compute(

const coef_7 = coef_6 == 1.51 || !school_prim.partenaria || school_prim.partenaria.id !== school_sec.id ? 1 : 1.51; // partenaria peda soit 1 soit 1.51
// LA CLASSE D'ENCADREMENT DE L'ÉCOLE PRIMAIRE (socio-économique)
const coef_8 = coef_8Table[school_prim.ise || 10]; // if not found, it's an average of students /o\
const coef_8 = coef_8Table[school_prim.ise || ise || 10]; // if not found, it's an average of students /o\

return {
coef_1,
Expand Down Expand Up @@ -244,6 +254,7 @@ export function computeAll(
locHome: GeoLoc,
date: string,
immersion: boolean,
ise?: number,
): ComputeResult[] {
console.time("computeAll");

Expand All @@ -257,7 +268,7 @@ export function computeAll(
school: school,
primarySchools: prim,
secondarySchools: sec,
score: compute(prim, sec, primarySchool, school, locHome, immersion),
score: compute(prim, sec, primarySchool, school, locHome, immersion, ise),
home: locHome,
distance: distance(school.geo, locHome),
primarySchool: primarySchool,
Expand Down Expand Up @@ -287,6 +298,7 @@ export function getScoreGrid(
locHome: GeoLoc,
date: string,
immersion: boolean,
ise?: number,
): { grid: object; min: number; max: number; lines: object } {
console.time("getScoreGrid");

Expand Down Expand Up @@ -314,7 +326,7 @@ export function getScoreGrid(
const nLoc = { lon: currentFeature.geometry.coordinates[0], lat: currentFeature.geometry.coordinates[1] };
const prim = filterNewestAndOrderSchool(primarySchools, primarySchool.network, inscriptionDate, nLoc);
const sec = Array.from(secondarySchools).sort(schoolSorter(nLoc));
const score = compute(prim, sec, primarySchool, secondarySchool, nLoc, immersion);
const score = compute(prim, sec, primarySchool, secondarySchool, nLoc, immersion, ise);
currentFeature.properties = {
score: score.total,
};
Expand Down

0 comments on commit c179d37

Please sign in to comment.