Skip to content

Commit

Permalink
feat: store query in url while filtering people
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Raj <mailme@varunraj.in>
  • Loading branch information
varun-raj committed Sep 7, 2024
1 parent 9af0ba0 commit a596fa4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/components/people/PeopleFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@ import {
DropdownMenuTrigger,
} from "../ui/dropdown-menu";
import { Input } from "../ui/input";
import { IPersonListFilters } from "@/handlers/api/people.handler";

export function PeopleFilters() {
const router = useRouter();
const { updateContext, page, maximumAssetCount } = usePeopleFilterContext();

const handleChange = (data: Partial<IPersonListFilters>) => {
updateContext(data);
router.push({
pathname: router.pathname,
query: {
...router.query,
...data,
page: undefined,
},
});
}
const [nextPage, prevPage] = useMemo(() => {
const pageNum = parseInt(page.toString() || "1", 10);
return [pageNum + 1, pageNum - 1];
Expand All @@ -32,17 +44,17 @@ export function PeopleFilters() {
onChange={(e) => {
try {
const value = parseInt(e.target.value || "0", 10);
updateContext({ maximumAssetCount: value });
handleChange({ maximumAssetCount: value });
} catch (e) {
updateContext({ maximumAssetCount: 0 });
handleChange({ maximumAssetCount: 0 });
}
}}
/>
<div className="flex items-center gap-1">
<Switch
id="nameLessOnly"
onCheckedChange={(checked) => {
updateContext({ nameLessOnly: checked, page: 1 });
handleChange({ nameLessOnly: checked, page: 1 });
}}
/>
<Label className="text-nowrap" htmlFor="nameLessOnly">
Expand All @@ -52,27 +64,27 @@ export function PeopleFilters() {

<Button
disabled={prevPage < 1}
onClick={() => updateContext({ page: prevPage })}
onClick={() => handleChange({ page: prevPage })}
>
<ArrowLeft size={16} />
</Button>

<Button onClick={() => updateContext({ page: nextPage })}>
<Button onClick={() => handleChange({ page: nextPage })}>
<ArrowRight size={16} />
</Button>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant={"secondary"}
onClick={() => updateContext({ page: nextPage })}
onClick={() => handleChange({ page: nextPage })}
>
<SortDesc size={16} />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem
onClick={() =>
updateContext({ sort: "assetCount", sortOrder: "asc" })
handleChange({ sort: "assetCount", sortOrder: "asc" })
}
>
<SortAsc size={16} />
Expand All @@ -81,7 +93,7 @@ export function PeopleFilters() {

<DropdownMenuItem
onClick={() =>
updateContext({ sort: "assetCount", sortOrder: "desc" })
handleChange({ sort: "assetCount", sortOrder: "desc" })
}
>
<SortDesc size={16} />
Expand All @@ -91,7 +103,7 @@ export function PeopleFilters() {

<DropdownMenuItem
onClick={() =>
updateContext({ sort: "updatedAt", sortOrder: "asc" })
handleChange({ sort: "updatedAt", sortOrder: "asc" })
}
>
<SortAsc size={16} />
Expand All @@ -100,7 +112,7 @@ export function PeopleFilters() {

<DropdownMenuItem
onClick={() =>
updateContext({ sort: "updatedAt", sortOrder: "desc" })
handleChange({ sort: "updatedAt", sortOrder: "desc" })
}
>
<SortDesc size={16} />
Expand Down
1 change: 1 addition & 0 deletions src/components/people/PeopleList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function PeopleList() {
const [loading, setLoading] = useState(false);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const [filters, setFilters] = useState<IPersonListFilters>({
...router.query,
page: 1,
});

Expand Down

0 comments on commit a596fa4

Please sign in to comment.