Replace 1h KO with a catch all category - display only when required

This commit is contained in:
Timothy Armes
2023-07-07 09:42:05 +02:00
parent 88e75511d7
commit d8a5b4393a
10 changed files with 67 additions and 47 deletions
+33 -20
View File
@@ -1,12 +1,27 @@
import { useMap } from "react-leaflet";
import { useEffect } from "react";
import { useEffect, useMemo } from "react";
import { useTranslations } from "next-intl";
import L from "leaflet";
import { useAtomValue } from "jotai";
import { TimeControl } from "@/types";
import { filteredTournamentsByTimeControlAtom } from "@/app/atoms";
const Legend = () => {
const t = useTranslations("Tournaments");
const map = useMap();
const tournaments = useAtomValue(filteredTournamentsByTimeControlAtom);
const timeControls = useMemo(
() =>
[
{ tc: TimeControl.Classic, colour: "#00ac39" },
{ tc: TimeControl.Rapid, colour: "#0086c7" },
{ tc: TimeControl.Blitz, colour: "#cec348" },
{ tc: TimeControl.Other, colour: "#d10c3e" },
].filter(({ tc }) => tournaments.some((t) => t.timeControl === tc)),
[tournaments]
);
useEffect(() => {
if (map) {
@@ -19,30 +34,28 @@ const Legend = () => {
"style",
"background: white; color: black; border: 2px solid grey; border-radius: 6px; padding: 10px;"
);
div.innerHTML = `<ul>
<li><span style='background:#00ac39; display: block; height: 16px; width: 30px; border: 1px solid #999; float: left; margin-right: 5px'></span>${t(
"timeControlEnum",
{ tc: TimeControl.Classic }
)}</li>
<li><span style='background:#0086c7; display: block; height: 16px; width: 30px; border: 1px solid #999; float: left; margin-right: 5px'></span>${t(
"timeControlEnum",
{ tc: TimeControl.Rapid }
)}</li>
<li><span style='background:#cec348; display: block; height: 16px; width: 30px; border: 1px solid #999; float: left; margin-right: 5px'></span>${t(
"timeControlEnum",
{ tc: TimeControl.Blitz }
)}</li>
<li><span style='background:#d10c3e; display: block; height: 16px; width: 30px; border: 1px solid #999; float: left; margin-right: 5px'></span>${t(
"timeControlEnum",
{ tc: TimeControl.KO }
)}</li>
</ul>`;
div.innerHTML = `
<ul>
${timeControls
.map(
({ tc, colour }) => `
<li>
<span class="block h-4 w-7 border border-[#999] float-left mr-1" style="background: ${colour}"></span>${t(
"timeControlEnum",
{ tc }
)}
</li>
`
)
.join("")}
</ul>`;
return div;
};
legend.addTo(map);
}
}, [map, t]);
}, [map, t, timeControls]);
return null;
};
+20 -12
View File
@@ -1,35 +1,43 @@
import { useAtom } from "jotai";
import { useAtom, useAtomValue } from "jotai";
import { classicAtom, rapidAtom, blitzAtom, oneHourKOAtom } from "@/app/atoms";
import {
classicAtom,
rapidAtom,
blitzAtom,
otherAtom,
tournamentsAtom,
} from "@/app/atoms";
import { useTranslations } from "next-intl";
import { TimeControl } from "@/types";
const TimeControlFilters = () => {
const t = useTranslations("Tournaments");
const tournaments = useAtomValue(tournamentsAtom);
const classic = useAtom(classicAtom);
const rapid = useAtom(rapidAtom);
const blitz = useAtom(blitzAtom);
const oneHourKO = useAtom(oneHourKOAtom);
const other = useAtom(otherAtom);
const checkboxes = [
{ title: t("timeControlEnum", { tc: TimeControl.Classic }), atom: classic },
{ title: t("timeControlEnum", { tc: TimeControl.Rapid }), atom: rapid },
{ title: t("timeControlEnum", { tc: TimeControl.Blitz }), atom: blitz },
{ title: t("timeControlEnum", { tc: TimeControl.KO }), atom: oneHourKO },
];
{ tc: TimeControl.Classic, atom: classic },
{ tc: TimeControl.Rapid, atom: rapid },
{ tc: TimeControl.Blitz, atom: blitz },
{ tc: TimeControl.Other, atom: other },
].filter(({ tc }) => tournaments.some((t) => t.timeControl === tc));
return (
<div className="flex flex-wrap gap-3">
{checkboxes.map((cb, i) => (
{checkboxes.map(({ tc, atom }, i) => (
<div key={i} className="text-gray-900 dark:text-white">
<label>
<input
type="checkbox"
className="mr-2"
checked={cb.atom[0]}
onChange={() => cb.atom[1](!cb.atom[0])}
checked={atom[0]}
onChange={() => atom[1](!atom[0])}
/>
{cb.title}
{t("timeControlEnum", { tc })}
</label>
</div>
))}
+3 -3
View File
@@ -27,7 +27,7 @@ import {
classicAtom,
rapidAtom,
blitzAtom,
oneHourKOAtom,
otherAtom,
} from "@/app/atoms";
import Legend from "./Legend";
@@ -249,7 +249,7 @@ export default function TournamentMap() {
const classic = useAtomValue(classicAtom);
const rapid = useAtomValue(rapidAtom);
const blitz = useAtomValue(blitzAtom);
const other = useAtomValue(oneHourKOAtom);
const other = useAtomValue(otherAtom);
useEffect(() => {
if (hoveredListTournamentId === null) {
@@ -300,7 +300,7 @@ export default function TournamentMap() {
<TimeControlGroup timeControl={TimeControl.Blitz} colour="yellow" />
)}
{other && (
<TimeControlGroup timeControl={TimeControl.KO} colour="red" />
<TimeControlGroup timeControl={TimeControl.Other} colour="red" />
)}
</MapContainer>
+1 -1
View File
@@ -29,7 +29,7 @@ export const TournamentMarker = forwardRef<
? -0.01
: tournament.timeControl === TimeControl.Blitz
? 0.01
: tournament.timeControl === TimeControl.KO
: tournament.timeControl === TimeControl.Other
? 0.02
: 0),
});
+2 -3
View File
@@ -14,7 +14,7 @@ export interface TournamentData {
department: string;
tournament: string;
url: string;
time_control: "Cadence Lente" | "Rapide" | "Blitz" | "1h KO";
time_control: "Cadence Lente" | "Rapide" | "Blitz" | string;
date: string;
coordinates: [number, number];
}
@@ -23,7 +23,6 @@ const tcMap: Record<TournamentData["time_control"], TimeControl> = {
"Cadence Lente": TimeControl.Classic,
Rapide: TimeControl.Rapid,
Blitz: TimeControl.Blitz,
"1h KO": TimeControl.KO,
};
const getTournaments = async () => {
@@ -57,7 +56,7 @@ const getTournaments = async () => {
return data.map<Tournament>((t) => ({
...t,
_id: t._id.toString(),
timeControl: tcMap[t.time_control],
timeControl: tcMap[t.time_control] ?? TimeControl.Other,
latLng: { lat: t.coordinates[0], lng: t.coordinates[1] },
}));
} catch (error) {