Skip to content

Commit

Permalink
fix: Reset slug when clearing all filters
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Jan 19, 2024
1 parent 6117ef2 commit 2b0ff96
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion components/filters/checkbox-filter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import React from "react";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { usePathname, useRouter } from "next/navigation";
import { Checkbox } from "@nextui-org/checkbox";
import { createUrl } from "@/utils/url";

Expand Down
11 changes: 5 additions & 6 deletions components/filters/clear-filters.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import React from "react";
import { usePathname, useSearchParams } from "next/navigation";
import { usePathname } from "next/navigation";
import { useRouter } from "next/navigation";
import { Chip } from "@nextui-org/chip";
import { createUrl } from "@/utils/url";
Expand All @@ -14,11 +14,10 @@ interface IClearFilters {
export const ClearFilters = ({ param, value, onClear }: IClearFilters) => {
const router = useRouter();
const pathname = usePathname();
const params = useSearchParams();

const clearSearchParams = () => {
const handleClear = () => {
if (!!param) {
const newUrl = createUrl(param, null, pathname, params);
const newUrl = createUrl(param, [], pathname);
router.replace(newUrl);
} else {
router.replace(pathname);
Expand All @@ -32,8 +31,8 @@ export const ClearFilters = ({ param, value, onClear }: IClearFilters) => {
className="cursor-pointer"
variant="bordered"
color="danger"
onClick={clearSearchParams}
onClose={clearSearchParams}
onClick={handleClear}
onClose={handleClear}
>
{value}
</Chip>
Expand Down
5 changes: 4 additions & 1 deletion hooks/useFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback, useState } from "react";
import { useRouter } from "next/navigation";
import { FilterKeys, Filters } from "@/types/filters";
import { initFilters } from "@/utils/filters";

Expand All @@ -16,6 +17,7 @@ export interface IFiltersContext {
export const useFilters = ({
initialFilters,
}: IConfigProps): IFiltersContext => {
const router = useRouter();
const [filters, setFilters] = useState(initialFilters);

const updateFilter = useCallback((key: FilterKeys, values: string[]) => {
Expand Down Expand Up @@ -45,7 +47,8 @@ export const useFilters = ({
const clearAllFilters = useCallback(() => {
const emptyFilters = initFilters();
setFilters(emptyFilters);
}, []);
router.replace("/explore/open-contributions", { scroll: false });
}, [router]);

return {
filters,
Expand Down

0 comments on commit 2b0ff96

Please sign in to comment.