Skip to content

Commit

Permalink
Merge pull request #51 from KNU-AEYE/38-search-page-too-slow
Browse files Browse the repository at this point in the history
38 search page too slow
  • Loading branch information
ilp-sys authored May 23, 2024
2 parents 19f699d + 3c83264 commit c03d784
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
11 changes: 8 additions & 3 deletions aeye/app/(nav)/home/SearchResult.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from "react";
import { useRecoilValue } from "recoil";
import { searchQueryState } from "@/app/recoil-states";
import { searchQueryState, selectedTagsState } from "@/app/recoil-states";
import fetchWithInterception from "@/app/fetchWrapper";
import Vidpane from "@/app/components/Vidpane";
import { Grid, Typography, Pagination, Container } from "@mui/material";
Expand Down Expand Up @@ -37,13 +37,18 @@ const SearchResult: React.FC = () => {
const searchQuery = useRecoilValue(searchQueryState);
const [results, setResults] = useState<Vidarr | null>(null);
const [currentPage, setCurrentPage] = useState<number>(0);
const selectedTags = useRecoilValue(selectedTagsState);
const cityPlaceholder = "";
const districtPlaceholder = "";

useEffect(() => {
let searchTimeout: NodeJS.Timeout;

const fetchResults = () => {
fetchWithInterception(
`https://api.a-eye.live/video/search?keyword=${searchQuery}&page=${currentPage}&size=${PAGE_SIZE}`,
`https://api.a-eye.live/video/search/v2?keyword=${searchQuery}&city=${cityPlaceholder}&district=${districtPlaceholder}&tag=${
selectedTags === undefined ? "" : selectedTags
}&page=${currentPage}&size=${PAGE_SIZE}`,
{ method: "GET" }
)
.then((response) => response.json())
Expand All @@ -55,7 +60,7 @@ const SearchResult: React.FC = () => {
searchTimeout = setTimeout(fetchResults, FETCH_TIMEOUT);
}
return () => clearTimeout(searchTimeout);
}, [searchQuery, currentPage]);
}, [searchQuery, currentPage, selectedTags]);

const handlePaginationChange = (
event: React.ChangeEvent<unknown>,
Expand Down
33 changes: 33 additions & 0 deletions aeye/app/(nav)/home/SearchTagList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Chip, Stack } from "@mui/material";
import { selectedTagsState } from "@/app/recoil-states";
import { useRecoilState } from "recoil";

const tags = ["새 객체", "넘어짐", "화재", "자동차", "도로"];

export default function SearchTagList() {
const [selectedTags, setSelectedTags] = useRecoilState(selectedTagsState);

const handleChipClick = (tag: string) => {
setSelectedTags((prevSelectedTags) => {
if (prevSelectedTags.includes(tag)) {
return prevSelectedTags.filter((t) => t !== tag);
} else {
return [...prevSelectedTags, tag];
}
});
};
return (
<Stack flexDirection="row" mb="15px">
{tags.map((tag, index) => (
<Chip
label={tag}
key={index}
variant={selectedTags.includes(tag) ? "filled" : "outlined"}
sx={{ margin: "0.5vh" }}
color={selectedTags.includes(tag) ? "primary" : "default"}
onClick={() => handleChipClick(tag)}
/>
))}
</Stack>
);
}
2 changes: 2 additions & 0 deletions aeye/app/(nav)/home/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import SearchBar from "./SearchBar";
import SearchResult from "./SearchResult";
import SearchTagList from "./SearchTagList";

export default function Home() {
return (
<>
<SearchBar />
<SearchTagList />
<SearchResult />
</>
);
Expand Down
7 changes: 6 additions & 1 deletion aeye/app/recoil-states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ const memberState = atom<Member | null>({
default: null,
});

export { searchQueryState, memberState };
const selectedTagsState = atom<string[]>({
key: "selectedTagsState",
default: [],
});

export { searchQueryState, memberState, selectedTagsState };

0 comments on commit c03d784

Please sign in to comment.