Skip to content

Commit

Permalink
feat: filter games by query
Browse files Browse the repository at this point in the history
  • Loading branch information
ngolapnguyen committed Mar 10, 2023
1 parent c7c4ff6 commit 742d24a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/stores/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ interface State {

minigame?: Minigame;
getMinigames: (
params: Partial<Pagination>
params: Partial<Pagination> & { query?: string }
) => Promise<FullResponse<Minigame>>;
startMinigame: (game: Minigame) => void;
stopMinigame: () => void;
Expand Down
19 changes: 12 additions & 7 deletions src/ui/components/Menu/MinigameMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ const PAGE_SIZE = 12;
export const MinigameMenu = () => {
const { closeMenu, startMinigame, getActiveScene, getMinigames } =
useGameState();
const [searchQuery, setSearchQuery] = useState("");
const [filter, setFilter] = useState({
page: 1,
size: PAGE_SIZE,
query: "",
});

const [page, setPage] = useState(1);
const { data, isLoading } = useSWR(["minigames", page], () =>
getMinigames({ page, size: PAGE_SIZE })
const { data, isLoading } = useSWR(["minigames", filter], () =>
getMinigames(filter)
);
const minigames = data?.data ?? [];
const isLastPage = minigames.length < PAGE_SIZE;
Expand Down Expand Up @@ -130,7 +133,9 @@ export const MinigameMenu = () => {
<GradientContainer className="overflow-hidden">
<input
id="search"
onChange={(e) => setSearchQuery(e.target.value)}
onChange={(e) =>
setFilter((o) => ({ ...o, query: e.target.value, page: 1 }))
}
placeholder="Search..."
className="bg-black/30 px-4 py-2 !border-none !outline-none"
/>
Expand Down Expand Up @@ -169,8 +174,8 @@ export const MinigameMenu = () => {
</div>
<Pagination
className="mt-auto"
page={page}
onChange={(v) => setPage(v)}
page={filter.page}
onChange={(v) => setFilter((o) => ({ ...o, page: v }))}
hideOnSinglePage
isLastPage={isLastPage}
/>
Expand Down

0 comments on commit 742d24a

Please sign in to comment.