-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve search page further. fix pagination
- Loading branch information
Showing
8 changed files
with
255 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Card, CardContent, Divider, Grid, Typography } from "@mui/material"; | ||
import React from "react"; | ||
import { useLocation } from "react-router-dom"; | ||
import Loading from "../loading"; | ||
|
||
import { useGetSearchTypesQuery } from "../../api/gram/search"; | ||
import { CenteredPage } from "../elements/CenteredPage"; | ||
import { SearchResultBox } from "./SearchResultBox"; | ||
|
||
export default function SearchPage() { | ||
const { search } = useLocation(); | ||
const query = new URLSearchParams(search); | ||
const queryValue = query.get("query") || ""; | ||
|
||
const { data, isLoading } = useGetSearchTypesQuery(); | ||
|
||
return ( | ||
<CenteredPage> | ||
<Card sx={{ maxWidth: "md" }}> | ||
<CardContent> | ||
<Typography variant="h5"> | ||
Search results for "{queryValue}" | ||
</Typography> | ||
</CardContent> | ||
</Card> | ||
<br /> | ||
<Divider /> | ||
<br /> | ||
<> | ||
{!isLoading ? ( | ||
<Grid container spacing={2}> | ||
{data?.map((searchType) => ( | ||
<Grid item md={4}> | ||
<SearchResultBox searchText={queryValue} type={searchType} /> | ||
</Grid> | ||
))} | ||
</Grid> | ||
) : ( | ||
<Loading /> | ||
)} | ||
</> | ||
</CenteredPage> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
Card, | ||
CardContent, | ||
CircularProgress, | ||
List, | ||
ListItemButton, | ||
ListItemText, | ||
Pagination, | ||
Typography, | ||
} from "@mui/material"; | ||
import { useState } from "react"; | ||
import { Link } from "react-router-dom"; | ||
import { useSearchQuery } from "../../api/gram/search"; | ||
|
||
export function SearchResultBox({ searchText, type }) { | ||
const [page, setPage] = useState(1); | ||
|
||
const { data, isLoading } = useSearchQuery({ | ||
searchText, | ||
types: [type.key], | ||
page: page - 1, | ||
}); | ||
|
||
const r = isLoading ? null : data[0]; | ||
|
||
if (isLoading || !r) { | ||
return <CircularProgress />; | ||
} | ||
|
||
return ( | ||
<Card sx={{ maxWidth: "sm" }}> | ||
<CardContent> | ||
<Typography variant="h5"> | ||
{type.label}s {r.count > 0 && <>({r.count})</>} | ||
</Typography> | ||
{r.items && r.items.length === 0 && ( | ||
<Typography variant="body2" sx={{ marginTop: 1 }}> | ||
No {type.label}s found | ||
</Typography> | ||
)} | ||
<List> | ||
{r.items && | ||
r.items.map((item) => ( | ||
<ListItemButton | ||
component={Link} | ||
to={item.url} | ||
key={item.id} | ||
style={{ lineHeight: 1, margin: 0, padding: 0 }} | ||
> | ||
<ListItemText primary={item.label} secondary={item.id} /> | ||
</ListItemButton> | ||
))} | ||
</List> | ||
{r.count > 0 && ( | ||
<Pagination | ||
count={Math.floor(r.count / 10) || 1} | ||
page={page} | ||
onChange={(_, np) => setPage(np)} | ||
/> | ||
)} | ||
</CardContent> | ||
</Card> | ||
); | ||
} |
Oops, something went wrong.