c93a1ffe8d
- Updated the Select component to disable the placeholder option for better user experience. - Refactored AssignToCompanyModalContent to accept a companiesQuery prop, improving data handling. - Adjusted the useCustomerListPresenter hook to include the company query, ensuring it is only enabled when the assign company modal is open. - Enhanced the CustomersTable component to pass the company query to the modal content.
116 lines
3.0 KiB
TypeScript
116 lines
3.0 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
useAssignCompany,
|
|
useCompanyFullList,
|
|
useCustomersQuery,
|
|
useDeleteCustomerQuery,
|
|
} from "@/hooks/queries";
|
|
import { TAssignCustomerToCompanyCredentials, TCustomer } from "@/lib/api";
|
|
import { apiDefaultParams } from "@/constants/Api_Params";
|
|
import { deleteEmptyKeys } from "@/utils/shared";
|
|
import { useIsMutating } from "@tanstack/react-query";
|
|
import { usePathname, useRouter } from "next/navigation";
|
|
import { useState } from "react";
|
|
import { useForm } from "react-hook-form";
|
|
|
|
const useCustomerListPresenter = () => {
|
|
const [isAssignCompanyModalOpen, setIsAssignCompanyModalOpen] =
|
|
useState(false);
|
|
const [isFilterModalOpen, setIsFilterModalOpen] = useState(false);
|
|
const [selectedCompanies, setSelectedCompanies] = useState<
|
|
TAssignCustomerToCompanyCredentials | undefined
|
|
>();
|
|
const [currentCustomer, setCurrentCustomer] = useState<TCustomer | null>(
|
|
null,
|
|
);
|
|
|
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
const pathName = usePathname();
|
|
const [params, setParams] = useState<Record<string, any>>({
|
|
...apiDefaultParams,
|
|
});
|
|
const router = useRouter();
|
|
const isMutating = useIsMutating();
|
|
|
|
const {
|
|
register: filterRegister,
|
|
handleSubmit: handleFilterSubmit,
|
|
reset: filterFormReset,
|
|
watch,
|
|
setValue: setFilterValue,
|
|
} = useForm();
|
|
|
|
const { data, isPending } = useCustomersQuery(params);
|
|
const assignCompanyCompaniesQuery = useCompanyFullList({
|
|
enabled: isAssignCompanyModalOpen,
|
|
});
|
|
const { mutate: deleteCustomer } = useDeleteCustomerQuery(() =>
|
|
setIsModalOpen(false),
|
|
);
|
|
const { mutate: assignSelectedCompanies } = useAssignCompany(
|
|
currentCustomer?.id ?? "",
|
|
() => {
|
|
setIsAssignCompanyModalOpen(false);
|
|
},
|
|
);
|
|
|
|
const search = (data: any) => {
|
|
const newParams = { ...params, ...data, offset: 0 };
|
|
const cleanedParams = deleteEmptyKeys(newParams);
|
|
setParams(cleanedParams);
|
|
const queryString = new URLSearchParams(cleanedParams).toString();
|
|
router.push(`${pathName}?${queryString}`);
|
|
setIsFilterModalOpen(false);
|
|
};
|
|
|
|
const columns = [
|
|
"row",
|
|
"full name",
|
|
"email",
|
|
"created at",
|
|
"type",
|
|
"status",
|
|
"company",
|
|
"role",
|
|
"actions",
|
|
];
|
|
const onConfirmDelete = () => {
|
|
if (currentCustomer) {
|
|
deleteCustomer(currentCustomer.id);
|
|
}
|
|
};
|
|
|
|
const customers = data?.data?.customers ?? [];
|
|
return {
|
|
allCustomers: customers,
|
|
metadata: data?.meta,
|
|
currentCustomer,
|
|
columns,
|
|
isPending,
|
|
setCurrentCustomer,
|
|
selectedCompanies,
|
|
setSelectedCompanies,
|
|
onConfirmDelete,
|
|
setIsAssignCompanyModalOpen,
|
|
isAssignCompanyModalOpen,
|
|
assignSelectedCompanies,
|
|
pathName,
|
|
router,
|
|
isModalOpen,
|
|
setIsModalOpen,
|
|
isFilterModalOpen,
|
|
setIsFilterModalOpen,
|
|
filterRegister,
|
|
handleFilterSubmit,
|
|
search,
|
|
watch,
|
|
setFilterValue,
|
|
isMutating,
|
|
setParams,
|
|
assignCompanyCompaniesQuery,
|
|
};
|
|
};
|
|
|
|
export default useCustomerListPresenter;
|