From c49e33ae661bb56cee26bb08d516cbf66d753c03 Mon Sep 17 00:00:00 2001 From: Jalil Arfaoui Date: Fri, 8 Sep 2023 23:39:45 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20tri=20les=20r=C3=A9sistants=20avec=20le?= =?UTF-8?q?s=20derniers=20en=20premier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/Resistant.ts | 60 ++++++++++++++++++- src/components/EmbeddedYoutube.tsx | 4 +- src/resistants.ts | 82 +++++++++++++------------- src/routes/Liste/List/ResistantRow.tsx | 28 +++++---- src/routes/Liste/Map/PopupContent.tsx | 17 ++++-- src/routes/Liste/index.tsx | 8 +-- src/routes/PageResistant.tsx | 27 +++++---- yarn.lock | 26 ++++++++ 9 files changed, 176 insertions(+), 77 deletions(-) diff --git a/package.json b/package.json index 4e0c877..d23b641 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@mantine/core": "^6.0.10", "@mantine/hooks": "^6.0.10", "@tabler/icons-react": "^2.2.0", + "date-fns": "^2.30.0", "mapbox-gl": "^2.12.1", "react": "^18.2.0", "react-dom": "^18.2.0", diff --git a/src/Resistant.ts b/src/Resistant.ts index cbfeba2..c41b6a5 100644 --- a/src/Resistant.ts +++ b/src/Resistant.ts @@ -1,3 +1,6 @@ +import { formatWithOptions, isAfter, isBefore, parse } from "date-fns/fp"; +import { fr } from "date-fns/locale"; + export interface Resistant { id: string; noms: string; @@ -14,10 +17,63 @@ export interface Resistant { longitude: number; } -export const isAncienResistant = (resistant: Resistant): boolean => +export interface AncienResistant extends Resistant { + date_fin_resistance: string; +} + +export const isAncienResistant = ( + resistant: Resistant +): resistant is AncienResistant => Boolean( resistant.date_fin_resistance && resistant.date_fin_resistance !== "" ); -export const isResistantActif = (resistant: Resistant): boolean => +export const isResistantActif = (resistant: Resistant) => !isAncienResistant(resistant); + +const r1First = -1; +const r2First = 1; + +export const resistantsOrder = (r1: Resistant, r2: Resistant): number => { + if (r1.id === "ramin-marjorie") return r1First; + if (r2.id === "ramin-marjorie") return r2First; + if (r1.id === "jonathan-caroline") return r1First; + if (r2.id === "jonathan-caroline") return r2First; + + if (isResistantActif(r1) && isAncienResistant(r2)) return r1First; + if (isAncienResistant(r1) && isResistantActif(r2)) return r2First; + + if (isAfter(getResistantDateDeclaration(r2), getResistantDateDeclaration(r1))) + return r1First; + if (isAfter(getResistantDateDeclaration(r1), getResistantDateDeclaration(r2))) + return r2First; + + return 0; +}; + +export const sortResistants = (liste: Array): Array => + liste.sort(resistantsOrder); + +export const getResistantDateDeclaration = (resistant: Resistant): Date => + parse(new Date(), "yyyy-MM-dd", resistant.date_declaration); + +export const getResistantDateDeclarationMoisAnnee = ( + resistant: Resistant +): string => + formatWithOptions( + { locale: fr }, + "MMMM yyyy", + getResistantDateDeclaration(resistant) + ); + +export const getResistantDateFin = (resistant: AncienResistant): Date => + parse(new Date(), "yyyy-MM-dd", resistant.date_fin_resistance); + +export const getResistantDateFinMoisAnnee = ( + resistant: AncienResistant +): string | undefined => + formatWithOptions( + { locale: fr }, + "MMMM yyyy", + getResistantDateFin(resistant) + ); diff --git a/src/components/EmbeddedYoutube.tsx b/src/components/EmbeddedYoutube.tsx index eb94270..f9457d4 100644 --- a/src/components/EmbeddedYoutube.tsx +++ b/src/components/EmbeddedYoutube.tsx @@ -5,7 +5,7 @@ interface Props { width: string; height: string; onClick: () => void; - style: IframeHTMLAttributes["style"]; + style?: IframeHTMLAttributes["style"]; } export const EmbeddedYoutube = ({ @@ -13,7 +13,7 @@ export const EmbeddedYoutube = ({ width, height, onClick, - style, + style = {}, }: Props) => (