19a3f72325
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.
123 lines
3.2 KiB
TypeScript
123 lines
3.2 KiB
TypeScript
"use client";
|
|
|
|
import { apiDefaultParams } from "@/constants/Api_Params";
|
|
import { AdminStatus } from "@/constants/enums";
|
|
import {
|
|
useChangeAdminStatus,
|
|
useDeleteAdmin,
|
|
useGetAdminListQuery,
|
|
} from "@/hooks/queries";
|
|
import { IUser, TChangeAdminStatusCredentials } from "@/lib/api";
|
|
import { deleteEmptyKeys } from "@/utils/shared";
|
|
import { useIsMutating } from "@tanstack/react-query";
|
|
import { usePathname, useRouter, useSearchParams } from "next/navigation";
|
|
import { useEffect, useState } from "react";
|
|
import { useForm } from "react-hook-form";
|
|
|
|
export const useAdminsPresenter = () => {
|
|
const [isChangeStatusModalOpen, setIsChangeStatusModalOpen] = useState(false);
|
|
const [isFilterModalOpen, setIsFilterModalOpen] = useState(false);
|
|
const [currentUser, setCurrentUser] = useState<IUser | null>(null);
|
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
const [status, setStatus] = useState<string>(
|
|
currentUser?.status ?? AdminStatus.ACTIVE,
|
|
);
|
|
const isMutating = useIsMutating();
|
|
const router = useRouter();
|
|
const searchParams = useSearchParams();
|
|
const {
|
|
register: filterRegister,
|
|
handleSubmit: handleFilterSubmit,
|
|
reset: filterFormReset,
|
|
} = useForm();
|
|
|
|
const {
|
|
register,
|
|
formState: { errors },
|
|
handleSubmit,
|
|
} = useForm<TChangeAdminStatusCredentials>({
|
|
mode: "onChange",
|
|
defaultValues: {
|
|
status: currentUser?.status,
|
|
},
|
|
});
|
|
const [params, setParams] = useState<Record<string, any>>({});
|
|
const pathName = usePathname();
|
|
|
|
useEffect(() => {
|
|
const newParams: Record<string, any> = {
|
|
...apiDefaultParams,
|
|
offset: 0,
|
|
limit: 10,
|
|
};
|
|
for (const [key, value] of searchParams.entries()) {
|
|
newParams[key] = value;
|
|
}
|
|
setParams(newParams);
|
|
filterFormReset(newParams);
|
|
}, [searchParams, filterFormReset]);
|
|
const { data, isPending } = useGetAdminListQuery(params);
|
|
const { mutate: deleteAdmin } = useDeleteAdmin(() => {
|
|
setIsModalOpen(false);
|
|
});
|
|
|
|
const onConfirmDelete = () => {
|
|
if (currentUser) {
|
|
deleteAdmin(currentUser.id);
|
|
}
|
|
};
|
|
const { mutate: changeStatus } = useChangeAdminStatus({
|
|
id: currentUser?.id ?? "",
|
|
successCallback: () => {
|
|
setIsChangeStatusModalOpen(false);
|
|
},
|
|
});
|
|
|
|
const search = (data: any) => {
|
|
const newParams = { ...params, ...data, offset: 0 };
|
|
const cleanedParams = deleteEmptyKeys(newParams);
|
|
const queryString = new URLSearchParams(cleanedParams).toString();
|
|
router.push(`${pathName}?${queryString}`);
|
|
setIsFilterModalOpen(false);
|
|
};
|
|
const allUsers = data?.data?.users ?? [];
|
|
const metadata = data?.meta;
|
|
const columns = [
|
|
"row",
|
|
"full name",
|
|
"email",
|
|
"role",
|
|
"username",
|
|
"status",
|
|
"actions",
|
|
];
|
|
return {
|
|
allUsers,
|
|
metadata,
|
|
columns,
|
|
isPending,
|
|
onConfirmDelete,
|
|
isModalOpen,
|
|
setIsModalOpen,
|
|
isMutating,
|
|
router,
|
|
setCurrentUser,
|
|
pathName,
|
|
status,
|
|
setStatus,
|
|
changeStatus,
|
|
isChangeStatusModalOpen,
|
|
currentUser,
|
|
setIsChangeStatusModalOpen,
|
|
register,
|
|
errors,
|
|
handleSubmit,
|
|
isFilterModalOpen,
|
|
setIsFilterModalOpen,
|
|
setParams,
|
|
filterRegister,
|
|
handleFilterSubmit,
|
|
search,
|
|
};
|
|
};
|