diff --git a/src/components/SearchBar.tsx b/src/components/SearchBar.tsx index 8e3f424..ef6bde4 100644 --- a/src/components/SearchBar.tsx +++ b/src/components/SearchBar.tsx @@ -74,13 +74,25 @@ function SearchBarWithFilter({ setSearchFilters }: SearchBarWithFilterProps) { function handleFilterChange( e: React.ChangeEvent, ) { - let { name } = e.target; + const { name } = e.target; const { value } = e.target; - if (name == "") name = "notes"; + const filt: SearchFilter = { + notes: "", + filename: "", + }; + + if (name == "") { + filt.notes = value; + filt.filename = value; + } + + // console.log("search", filt); setSearchFilters((prevFilters) => ({ ...prevFilters, + notes: filt.notes, + filename: filt.filename, [name]: value, })); } diff --git a/src/routes/root.tsx b/src/routes/root.tsx index ba96fd7..2c31790 100644 --- a/src/routes/root.tsx +++ b/src/routes/root.tsx @@ -16,41 +16,104 @@ export default function Root() { afterDate: "", }); + // const fetchData = async (filters: SearchFilter) => { + // const { location, date, notes, eventType, filename } = filters; + // let { afterDate, beforeDate } = filters; + + // beforeDate = beforeDate + // ? `${beforeDate?.split("-")[1]}-${beforeDate?.split("-")[2]}-${beforeDate?.split("-")[0]}` + // : undefined; + // afterDate = afterDate + // ? `${afterDate?.split("-")[1]}-${afterDate?.split("-")[2]}-${afterDate?.split("-")[0]}` + // : undefined; + + // const params: Record = { + // ...(location ? { location } : {}), + // ...(eventType ? { eventType } : {}), + // ...(date ? { date } : {}), + // ...(notes ? { notes } : {}), + // ...(filename ? { filename } : {}), + // ...(afterDate ? { afterDate } : {}), + // ...(beforeDate ? { beforeDate } : {}), + // }; + + // const queryString = new URLSearchParams(params).toString(); + + // const res = await fetch( + // `${import.meta.env.VITE_API_URL}/api/v2/mcap/get?${queryString}`, + // ); + + // const data = await res.json(); + // return data.data; + // }; + const fetchData = async (filters: SearchFilter) => { - const { location, date, notes, eventType } = filters; + const { location, date, notes, eventType, filename } = filters; let { afterDate, beforeDate } = filters; beforeDate = beforeDate - ? `${beforeDate?.split("-")[1]}-${beforeDate?.split("-")[2]}-${beforeDate?.split("-")[0]}` + ? `${beforeDate.split("-")[1]}-${beforeDate.split("-")[2]}-${beforeDate.split("-")[0]}` : undefined; afterDate = afterDate - ? `${afterDate?.split("-")[1]}-${afterDate?.split("-")[2]}-${afterDate?.split("-")[0]}` + ? `${afterDate.split("-")[1]}-${afterDate.split("-")[2]}-${afterDate.split("-")[0]}` : undefined; - // console.log(beforeDate); - // console.log(afterDate); - - const params: Record = { - ...(location ? { location } : {}), - ...(eventType ? { eventType } : {}), - ...(date ? { date } : {}), - ...(notes ? { notes } : {}), - ...(afterDate ? { afterDate } : {}), - ...(beforeDate ? { beforeDate } : {}), + const buildParams = (additionalParams: Record = {}) => { + return { + ...(location ? { location } : {}), + ...(eventType ? { eventType } : {}), + ...(date ? { date } : {}), + ...(afterDate ? { afterDate } : {}), + ...(beforeDate ? { beforeDate } : {}), + ...additionalParams, + }; }; - const queryString = new URLSearchParams(params).toString(); - console.log(queryString); + if (!notes && !filename) { + const params = buildParams(); + const queryString = new URLSearchParams(params).toString(); + + const res = await fetch( + `${import.meta.env.VITE_API_URL}/api/v2/mcap/get?${queryString}`, + ); + const data = await res.json(); + return data.data; + } + + const promises: Promise[] = []; + + if (notes) { + const paramsNotes = buildParams({ notes }); + const queryStringNotes = new URLSearchParams(paramsNotes).toString(); + promises.push( + fetch( + `${import.meta.env.VITE_API_URL}/api/v2/mcap/get?${queryStringNotes}`, + ), + ); + } + + if (filename) { + const paramsFilename = buildParams({ filename }); + const queryStringFilename = new URLSearchParams( + paramsFilename, + ).toString(); + promises.push( + fetch( + `${import.meta.env.VITE_API_URL}/api/v2/mcap/get?${queryStringFilename}`, + ), + ); + } + + const results = await Promise.all(promises); + const data = await Promise.all(results.map((res) => res.json())); - // console.log(queryString); - // console.log(searchFilters); + const combinedData = data.flatMap((entry) => entry.data || []); - const res = await fetch( - `${import.meta.env.VITE_API_URL}/api/v2/mcap/get?${queryString}`, - ); + const uniqueData = Array.from( + new Set(combinedData.map((item) => item.id)), + ).map((id) => combinedData.find((item) => item.id === id)); - const data = await res.json(); - return data.data; + return uniqueData; }; const assignData = async () => { diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 6919104..2e71ea4 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -16,6 +16,7 @@ type MCAPFileInformation = { type SearchFilter = { location?: string; + filename?: string; date?: string; notes?: string; eventType?: string;