add tournament duration filter

remove expired tournaments from test data

apply auto formatting rules

condense norm tournament text

revert duration to pill buttons

use select for duration filter

duration filter

show date range on map marker

return start/end dates and calculated duration for tournaments

inline returns etc

new seed data for dev
This commit is contained in:
2026-10-07 23:31:41 +02:00
committed by owenrees
parent 6517af51c9
commit 6f32be40d4
9 changed files with 11291 additions and 10358 deletions
+11195 -10337
View File
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,50 @@
import { useAtom, useAtomValue } from "jotai";
import { useTranslations } from "next-intl";
import { twMerge } from "tailwind-merge";
import {
DurationFilter as DurationFilterValue,
durationFilterAtom,
tournamentsAtom,
} from "@/atoms";
const DurationFilter = () => {
const t = useTranslations("Tournaments");
const tournaments = useAtomValue(tournamentsAtom);
const [durationFilter, setDurationFilter] = useAtom(durationFilterAtom);
const show3Plus = tournaments.some((tt) => tt.durationDays >= 3);
const options: { value: DurationFilterValue; label: string }[] = [
{ value: "any", label: t("durationAny") },
{ value: "1", label: t("duration1Day") },
{ value: "2", label: t("duration2Days") },
...(show3Plus ? [{ value: "3+" as const, label: t("duration3PlusDays") }] : []),
];
return (
<div className="flex flex-wrap items-center gap-1">
{options.map(({ value, label }) => {
const isActive = durationFilter === value;
return (
<button
key={value}
type="button"
aria-pressed={isActive}
onClick={() => setDurationFilter(value)}
className={twMerge(
"rounded-full border px-3 py-1 text-sm transition-colors focus:outline-none",
isActive
? "border-primary bg-primary text-white"
: "border-gray-400 text-gray-600 hover:border-primary hover:text-primary dark:border-gray-600 dark:text-gray-300 dark:hover:text-primary",
)}
>
{label}
</button>
);
})}
</div>
);
};
export default DurationFilter;
@@ -4,7 +4,6 @@ import { forwardRef, useImperativeHandle, useMemo, useRef } from "react";
import { useSetAtom } from "jotai";
import L from "leaflet";
import { last } from "lodash";
import { useTranslations } from "next-intl";
import { FaTrophy } from "react-icons/fa";
import { Marker, MarkerProps, Popup } from "react-leaflet";
@@ -37,7 +36,7 @@ export const TournamentMarker = forwardRef<MarkerRef, TournamentMarkerProps>(
[],
);
const { date, latLng, groupId, timeControl } = tournamentGroup[0];
const { startDate, endDate, latLng, groupId, timeControl } = tournamentGroup[0];
const setHoveredMapId = useSetAtom(debouncedHoveredMapIdAtom);
@@ -58,9 +57,6 @@ export const TournamentMarker = forwardRef<MarkerRef, TournamentMarkerProps>(
[timeControl],
);
const startDate = date;
const endDate = last(tournamentGroup)!.date;
return (
<Marker
ref={markerRef}
@@ -74,10 +70,7 @@ export const TournamentMarker = forwardRef<MarkerRef, TournamentMarkerProps>(
>
<Popup maxWidth={10000}>
<div className="flex max-w-[calc(100vw-80px)] flex-col gap-3 lg:max-w-[calc(100vw/2-80px)]">
<b>
{date}
{endDate !== startDate && ` - ${endDate}`}
</b>
<b>{startDate !== endDate ? `${startDate} -> ${endDate}` : startDate}</b>
<div className="flex flex-col gap-0">
{tournamentGroup.map((tournament) => (
@@ -25,6 +25,7 @@ import { useBreakpoint } from "@/hooks/tailwind";
import useDatePickerWidth from "@/hooks/useDatePickerWidth";
import { DatePickerDirection } from "@/types";
import DurationFilter from "./DurationFilter";
import TimeControlFilters from "./TimeControlFilters";
const TournamentTable = () => {
@@ -75,6 +76,7 @@ const TournamentTable = () => {
<div className="z-10 flex w-full flex-wrap items-center justify-start gap-3 p-3">
<div className="flex w-full flex-col gap-3 sm:flex-row">
<SearchBar className="w-full sm:w-auto" />
<RegionSelect
syncTitle={t("syncWithMapOption")}
classNameOverrides={{
@@ -89,6 +91,8 @@ const TournamentTable = () => {
onClick={handleDatePickerClick}
/>
<DurationFilter />
<div className="flex text-gray-900 dark:text-white">
<label>
<input
+13 -8
View File
@@ -7,7 +7,7 @@ import { unstable_cache } from "next/cache";
import { baseUrl } from "@/constants";
import { tournamentModelSchema } from "@/server/models/tournamentModel";
import { collections, dbConnect } from "@/server/mongodb";
import { TimeControl, Tournament, tcMap } from "@/types";
import { tcMap, TimeControl, Tournament } from "@/types";
import { errorLog } from "@/utils/logger";
import TournamentsDisplay from "./TournamentsDisplay";
@@ -65,7 +65,7 @@ const getTournaments = async () => {
const bad = data.filter((t) => {
const result = tournamentModelSchema.safeParse(t);
if (result.success === false) {
if (!result.success) {
console.log(JSON.stringify(result, null, 2));
console.log(JSON.stringify(t, null, 2));
@@ -93,7 +93,7 @@ const getTournaments = async () => {
}
}
return result.success === false;
return !result.success;
});
const badIds = bad.map((t) => t._id.toString());
@@ -108,7 +108,7 @@ const getTournaments = async () => {
const tournaments = groupedByLocation[location];
// Note that this works since the tournaments are sorted by date
const dateRanges = tournaments.reduce<Date[][]>(
dateRangesByLocation[location] = tournaments.reduce<Date[][]>(
(acc, tournament) => {
const group = acc[acc.length - 1];
const date = parse(tournament.date, "dd/MM/yyyy", new Date());
@@ -122,8 +122,6 @@ const getTournaments = async () => {
},
[[]],
);
dateRangesByLocation[location] = dateRanges;
}
return goodData.map<Tournament>((t) => {
@@ -137,6 +135,11 @@ const getTournaments = async () => {
const timeControl = tcMap[t.time_control] ?? TimeControl.Other;
const groupId = `${location}_${rangeIndex}_${timeControl}`;
// calculate duration of the tournament in days, parsing the French locale date
const startDateISO = parse(t.start_date, "dd/MM/yyyy", new Date());
const endDateISO = parse(t.end_date, "dd/MM/yyyy", new Date());
const durationDays = differenceInDays(endDateISO, startDateISO) + 1;
return {
id: t._id.toString(),
ffeId: t.tournament_id,
@@ -151,6 +154,9 @@ const getTournaments = async () => {
norm: t.norm_tournament ?? false,
pending: t.pending ?? false,
status: t.status,
startDate: t.start_date,
endDate: t.end_date,
durationDays,
};
});
} catch (error) {
@@ -162,8 +168,7 @@ const getTournaments = async () => {
export default async function Tournaments() {
const tournaments = await unstable_cache(
async () => {
const data = await getTournaments();
return data;
return getTournaments();
},
["tournaments"],
{
+12
View File
@@ -41,6 +41,9 @@ export const searchStringAtom = atom("");
export const tournamentsAtom = atom<Tournament[]>([]);
export const normsOnlyAtom = atom(false);
export type DurationFilter = "any" | "1" | "2" | "3+";
export const durationFilterAtom = atom<DurationFilter>("any");
export const classicAtom = atom(true);
export const rapidAtom = atom(true);
export const blitzAtom = atom(true);
@@ -69,14 +72,23 @@ export const filteredTournamentsByTimeControlAndZoneAtom = atom((get) => {
const dateRange = get(dateRangeAtom);
const { startDate, endDate } = dateRange[0];
const durationFilter = get(durationFilterAtom);
const filterByTimeControl = tournaments.filter((tournament) => {
const tournamentDate = startOfDay(parse(tournament.date, "dd/MM/yyyy", new Date()));
const durationMatch =
durationFilter === "any" ||
(durationFilter === "1" && tournament.durationDays === 1) ||
(durationFilter === "2" && tournament.durationDays === 2) ||
(durationFilter === "3+" && tournament.durationDays >= 3);
return (
!isBefore(tournamentDate, startDate) &&
(endDate === undefined || !isAfter(tournamentDate, endDate)) &&
!tournament.pending &&
tournament.status === "scheduled" &&
durationMatch &&
((tournament.timeControl === TimeControl.Classic && classic) ||
(tournament.timeControl === TimeControl.Rapid && rapid) ||
(tournament.timeControl === TimeControl.Blitz && blitz) ||
+6 -2
View File
@@ -82,7 +82,7 @@
"searchLabel": "Search",
"searchPlaceholder": "Search",
"syncWithMapOption": "Only tournaments visible in the map view",
"normsOnly": "Only norm tournaments",
"normsOnly": "Norms tournaments",
"noneFound": "No tournaments found",
"date": "Date",
"town": "Town",
@@ -91,7 +91,11 @@
"timeControl": "Time Control",
"approx": "Geo-location is approximative",
"norm": "Norm Tournament",
"clearButton": "Clear"
"clearButton": "Clear",
"durationAny": "Any",
"duration1Day": "1 day",
"duration2Days": "2 days",
"duration3PlusDays": "3+ days"
},
"Clubs": {
+6 -2
View File
@@ -81,7 +81,7 @@
"searchLabel": "Rechercher",
"searchPlaceholder": "Rechercher",
"syncWithMapOption": "Seuls les tournois visibles dans la vue carte",
"normsOnly": "Uniquement tournois à normes",
"normsOnly": "Tournois à normes",
"noneFound": "Pas de tournois trouvé",
"date": "Date",
"town": "Ville",
@@ -90,7 +90,11 @@
"timeControl": "Cadence",
"approx": "Géolocalisation approximative",
"norm": "Tournoi à norme",
"clearButton": "Effacer"
"clearButton": "Effacer",
"durationAny": "Tous",
"duration1Day": "1 jour",
"duration2Days": "2 jours",
"duration3PlusDays": "3+ jours"
},
"Clubs": {
+3
View File
@@ -25,6 +25,9 @@ export type Tournament = {
norm: boolean;
pending: boolean;
status: Status;
startDate: string;
endDate: string;
durationDays: number;
};
export const tcMap: Record<TournamentModel["time_control"], TimeControl> = {