feat(admins): Move filter state to URL query params
Refactors the `useAdminsPresenter` hook to manage the admin table's filter and pagination state through URL search parameters instead of local component state. This change introduces the `useSearchParams` hook to read the state from the URL and a `useEffect` to synchronize it with the component's state and the filter form. The search function now updates the URL using `router.push`, making the URL the single source of truth. This enables several key improvements: - Users can now bookmark and share links to filtered views of the admin table. - The browser's back and forward buttons will correctly navigate through filter changes. - The filter form state is preserved on page refresh.
This commit is contained in:
@@ -2,16 +2,16 @@
|
|||||||
|
|
||||||
import { apiDefaultParams } from "@/constants/Api_Params";
|
import { apiDefaultParams } from "@/constants/Api_Params";
|
||||||
import { AdminStatus } from "@/constants/enums";
|
import { AdminStatus } from "@/constants/enums";
|
||||||
import { deleteEmptyKeys } from "@/utils/shared";
|
|
||||||
import {
|
import {
|
||||||
useChangeAdminStatus,
|
useChangeAdminStatus,
|
||||||
useDeleteAdmin,
|
useDeleteAdmin,
|
||||||
useGetAdminListQuery,
|
useGetAdminListQuery,
|
||||||
} from "@/hooks/queries";
|
} from "@/hooks/queries";
|
||||||
import { API_ENDPOINTS, IUser, TChangeAdminStatusCredentials } from "@/lib/api";
|
import { IUser, TChangeAdminStatusCredentials } from "@/lib/api";
|
||||||
import { useIsMutating, useQueryClient } from "@tanstack/react-query";
|
import { deleteEmptyKeys } from "@/utils/shared";
|
||||||
import { usePathname, useRouter } from "next/navigation";
|
import { useIsMutating } from "@tanstack/react-query";
|
||||||
import { useState } from "react";
|
import { usePathname, useRouter, useSearchParams } from "next/navigation";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
|
|
||||||
export const useAdminsPresenter = () => {
|
export const useAdminsPresenter = () => {
|
||||||
@@ -24,9 +24,12 @@ export const useAdminsPresenter = () => {
|
|||||||
);
|
);
|
||||||
const isMutating = useIsMutating();
|
const isMutating = useIsMutating();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { register: filterRegister, handleSubmit: handleFilterSubmit } =
|
const searchParams = useSearchParams();
|
||||||
useForm();
|
const {
|
||||||
const queryClient = useQueryClient();
|
register: filterRegister,
|
||||||
|
handleSubmit: handleFilterSubmit,
|
||||||
|
reset: filterFormReset,
|
||||||
|
} = useForm();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
register,
|
register,
|
||||||
@@ -38,12 +41,21 @@ export const useAdminsPresenter = () => {
|
|||||||
status: currentUser?.status,
|
status: currentUser?.status,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const [params, setParams] = useState<Record<string, any>>({
|
const [params, setParams] = useState<Record<string, any>>({});
|
||||||
|
const pathName = usePathname();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const newParams: Record<string, any> = {
|
||||||
...apiDefaultParams,
|
...apiDefaultParams,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
limit: 10,
|
limit: 10,
|
||||||
});
|
};
|
||||||
const pathName = usePathname();
|
for (const [key, value] of searchParams.entries()) {
|
||||||
|
newParams[key] = value;
|
||||||
|
}
|
||||||
|
setParams(newParams);
|
||||||
|
filterFormReset(newParams);
|
||||||
|
}, [searchParams, filterFormReset]);
|
||||||
const { data, isPending } = useGetAdminListQuery(params);
|
const { data, isPending } = useGetAdminListQuery(params);
|
||||||
const { mutate: deleteAdmin } = useDeleteAdmin(() => {
|
const { mutate: deleteAdmin } = useDeleteAdmin(() => {
|
||||||
setIsModalOpen(false);
|
setIsModalOpen(false);
|
||||||
@@ -62,10 +74,10 @@ export const useAdminsPresenter = () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const search = (data: any) => {
|
const search = (data: any) => {
|
||||||
setParams((prevParams) => {
|
const newParams = { ...params, ...data, offset: 0 };
|
||||||
const newParams = { ...prevParams, ...data, offset: 0 };
|
const cleanedParams = deleteEmptyKeys(newParams);
|
||||||
return deleteEmptyKeys(newParams);
|
const queryString = new URLSearchParams(cleanedParams).toString();
|
||||||
});
|
router.push(`${pathName}?${queryString}`);
|
||||||
setIsFilterModalOpen(false);
|
setIsFilterModalOpen(false);
|
||||||
};
|
};
|
||||||
const allUsers = data?.data?.users ?? [];
|
const allUsers = data?.data?.users ?? [];
|
||||||
|
|||||||
Reference in New Issue
Block a user