Fix date filter

bis

bis
This commit is contained in:
Timothy Armes
2024-05-08 20:34:28 +02:00
parent 4ca0914060
commit ae532b2a3c
3 changed files with 34 additions and 20 deletions
@@ -138,7 +138,6 @@ const getTournaments = async () => {
town: t.town, town: t.town,
department: t.department, department: t.department,
date: t.date, date: t.date,
isoDate: formatISO(date),
url: t.url, url: t.url,
timeControl, timeControl,
latLng: { lat: t.coordinates[0], lng: t.coordinates[1] }, latLng: { lat: t.coordinates[0], lng: t.coordinates[1] },
+34 -18
View File
@@ -1,7 +1,16 @@
import { formatISO, setDefaultOptions } from "date-fns"; import {
endOfDay,
formatISO,
isAfter,
isBefore,
parse,
setDefaultOptions,
startOfDay,
} from "date-fns";
import { fr } from "date-fns/locale"; import { fr } from "date-fns/locale";
import { atom } from "jotai"; import { atom } from "jotai";
import { LatLngBounds } from "leaflet"; import { LatLngBounds } from "leaflet";
import { c } from "next-safe-action/dist/index-EKyvnpX_.mjs";
import { Club, TimeControl, Tournament } from "@/types"; import { Club, TimeControl, Tournament } from "@/types";
import atomWithDebounce from "@/utils/atomWithDebounce"; import atomWithDebounce from "@/utils/atomWithDebounce";
@@ -41,23 +50,27 @@ export const filteredTournamentsByTimeControlAtom = atom((get) => {
const other = get(otherAtom); const other = get(otherAtom);
const dateRange = get(dateRangeAtom); const dateRange = get(dateRangeAtom);
const startDate = formatISO(dateRange[0].startDate); const { startDate, endDate } = dateRange[0];
const endDate =
dateRange[0].endDate !== undefined
? formatISO(dateRange[0].endDate)
: undefined;
return tournaments.filter( const filteredTournaments = tournaments.filter((tournament) => {
(tournament) => const tournamentDate = startOfDay(
tournament.isoDate >= startDate && parse(tournament.date, "dd/MM/yyyy", new Date()),
(endDate === undefined || tournament.isoDate <= endDate) && );
!tournament.pending &&
tournament.status === "scheduled" && return (
((tournament.timeControl === TimeControl.Classic && classic) || !isBefore(tournamentDate, startDate) &&
(tournament.timeControl === TimeControl.Rapid && rapid) || (endDate === undefined ||
(tournament.timeControl === TimeControl.Blitz && blitz) || (!isAfter(tournamentDate, endDate) &&
(tournament.timeControl === TimeControl.Other && other)), !tournament.pending &&
); tournament.status === "scheduled" &&
((tournament.timeControl === TimeControl.Classic && classic) ||
(tournament.timeControl === TimeControl.Rapid && rapid) ||
(tournament.timeControl === TimeControl.Blitz && blitz) ||
(tournament.timeControl === TimeControl.Other && other))))
);
});
return filteredTournaments;
}); });
export const filteredTournamentsListAtom = atom((get) => { export const filteredTournamentsListAtom = atom((get) => {
@@ -116,7 +129,10 @@ export const datePickerIsOpenAtom = atom(false);
export const maxDateAtom = atom((get) => { export const maxDateAtom = atom((get) => {
const tournaments = get(tournamentsAtom); const tournaments = get(tournamentsAtom);
const dateTimestamps = tournaments.map((t) => new Date(t.isoDate).getTime()); const dateTimestamps = tournaments.map((t) =>
endOfDay(parse(t.date, "dd/MM/yyyy", new Date())).getTime(),
);
return new Date(Math.max(...dateTimestamps)); return new Date(Math.max(...dateTimestamps));
}); });
-1
View File
@@ -54,7 +54,6 @@ export type Tournament = {
url: string; url: string;
timeControl: TimeControl; timeControl: TimeControl;
date: string; date: string;
isoDate: string;
latLng: LatLngLiteral; latLng: LatLngLiteral;
norm: boolean; norm: boolean;
pending: boolean; pending: boolean;