diff --git a/TODO b/TODO deleted file mode 100644 index 354bb00..0000000 --- a/TODO +++ /dev/null @@ -1 +0,0 @@ -marker legend diff --git a/components/Legend.tsx b/components/Legend.tsx new file mode 100644 index 0000000..c14f218 --- /dev/null +++ b/components/Legend.tsx @@ -0,0 +1,34 @@ +import { useMap } from "react-leaflet"; +import { useEffect } from "react"; +import L from "leaflet"; + +const Legend = () => { + const map = useMap(); + + useEffect(() => { + if (map) { + // @ts-ignore + const legend = L.control({ position: "bottomleft" }); + + legend.onAdd = () => { + const div = L.DomUtil.create("div", "map-legend"); + div.setAttribute( + "style", + "background: white; color: black; border: 2px solid grey; border-radius: 6px; padding: 10px;" + ); + div.innerHTML = ``; + return div; + }; + + legend.addTo(map); + } + }, [map]); + return null; +}; + +export default Legend; diff --git a/components/SearchBar.tsx b/components/SearchBar.tsx new file mode 100644 index 0000000..7ce0ce3 --- /dev/null +++ b/components/SearchBar.tsx @@ -0,0 +1,47 @@ +import { Dispatch, SetStateAction } from "react"; + +type SearchBarProps = { + tournamentFilter: string; + setTournamentFilter: Dispatch>; +}; + +const SearchBar = ({ + tournamentFilter, + setTournamentFilter, +}: SearchBarProps) => { + return ( + <> +
+ +
+
+ + + +
+ setTournamentFilter(e.target.value)} + /> +
+
+ + ); +}; + +export default SearchBar; diff --git a/components/TournamentMap.tsx b/components/TournamentMap.tsx index 15d6fdc..5427697 100644 --- a/components/TournamentMap.tsx +++ b/components/TournamentMap.tsx @@ -1,18 +1,15 @@ "use client"; -// Types import { TournamentDataProps } from "@/types"; import { LatLngLiteral } from "leaflet"; -// Leaflet + icon fixes import "leaflet/dist/leaflet.css"; import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.css"; -import L from "leaflet"; import "leaflet-defaulticon-compatibility"; import { MapContainer, TileLayer, LayersControl, useMap } from "react-leaflet"; import { createLayerGroups } from "@/utils/layerGroups"; -import { useEffect } from "react"; +import Legend from "@/components/Legend"; export default function TournamentMap({ tournamentData }: TournamentDataProps) { const center: LatLngLiteral = { lat: 47.0844, lng: 2.3964 }; @@ -24,33 +21,6 @@ export default function TournamentMap({ tournamentData }: TournamentDataProps) { const blitzMarkers = createLayerGroups("Blitz", "yellow", { tournamentData }); const otherMarkers = createLayerGroups("1h KO", "red", { tournamentData }); - // TODO move into its own hook - function Legend() { - const map = useMap(); - - useEffect(() => { - if (map) { - const legend = L.control({ position: "bottomleft" }); - - legend.onAdd = () => { - const div = L.DomUtil.create("div", "map-legend"); - div.style = - "background: white; color: black; border: 2px solid grey; border-radius: 6px; padding: 10px;"; - div.innerHTML = ``; - return div; - }; - - legend.addTo(map); - } - }, [map]); - return null; - } - return (
{ - setFilteredTournamentData( - tournamentData.filter((t) => - t.town.includes(tournamentFilter.toUpperCase()) - ) - ); + setFilteredTournamentData(tournamentFilter); }, [tournamentFilter]); - const tournaments = filteredTournamentData.map((t) => ( - - - - {t.date} - - - - - {t.town} - - - - - {t.tournament} - - - - - {t.time_control} - - - - )); - return (
-
- -
-
- - - -
- setTournamentFilter(e.target.value)} - /> -
-
- + +
@@ -90,7 +34,7 @@ export default function TournamentTable({ - {tournaments} + {filteredTournamentData}
DateCadence
); diff --git a/hooks/useTournamentFilter.tsx b/hooks/useTournamentFilter.tsx new file mode 100644 index 0000000..dbee2b9 --- /dev/null +++ b/hooks/useTournamentFilter.tsx @@ -0,0 +1,59 @@ +import { Tournament } from "@/types"; +import { useState } from "react"; + +// TODO change 'any' +const useTournamentDataFilter = ( + initialState: Tournament[] +): (any | ((searchQuery: string) => void))[] => { + const [filtered, setFiltered] = useState(initialState); + let state; + const setState = (searchQuery: string) => { + setFiltered( + initialState.filter((t) => t.town.includes(searchQuery.toUpperCase())) + ); + }; + + console.log(filtered); + + if (filtered.length === 0) { + state = ( + + + No tournaments found + + + ); + } else { + state = filtered.map((t) => ( + + + + {t.date} + + + + + {t.town} + + + + + {t.tournament} + + + + + {t.time_control} + + + + )); + } + + return [state, setState]; +}; + +export default useTournamentDataFilter;