Skip to content

Commit

Permalink
fixed filter with multiple params
Browse files Browse the repository at this point in the history
  • Loading branch information
jjoel1630 committed Oct 25, 2024
1 parent 7ebed12 commit 1ced654
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 24 deletions.
16 changes: 14 additions & 2 deletions src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,25 @@ function SearchBarWithFilter({ setSearchFilters }: SearchBarWithFilterProps) {
function handleFilterChange(
e: React.ChangeEvent<HTMLSelectElement | HTMLInputElement>,
) {
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,
}));
}
Expand Down
107 changes: 85 additions & 22 deletions src/routes/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {
// ...(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<string, string> = {
...(location ? { location } : {}),
...(eventType ? { eventType } : {}),
...(date ? { date } : {}),
...(notes ? { notes } : {}),
...(afterDate ? { afterDate } : {}),
...(beforeDate ? { beforeDate } : {}),
const buildParams = (additionalParams: Record<string, string> = {}) => {
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<Response>[] = [];

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 () => {
Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type MCAPFileInformation = {

type SearchFilter = {
location?: string;
filename?: string;
date?: string;
notes?: string;
eventType?: string;
Expand Down

0 comments on commit 1ced654

Please sign in to comment.