refactor(Select, AssignToCompanyModalContent): enhance select placeholder and integrate company query
- 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.
This commit is contained in:
@@ -1,25 +1,34 @@
|
||||
import { ChangeEvent, Dispatch, FC, SetStateAction, useEffect, useState } from "react";
|
||||
import {
|
||||
ChangeEvent,
|
||||
Dispatch,
|
||||
FC,
|
||||
SetStateAction,
|
||||
useEffect,
|
||||
useState,
|
||||
} from "react";
|
||||
|
||||
import { Select } from "@/components/FormElements/select";
|
||||
import { Building } from "@/components/Layouts/sidebar/icons";
|
||||
import { useCompanyFullList } from "@/hooks/queries";
|
||||
import { TAssignCustomerToCompanyCredentials } from "@/lib/api";
|
||||
import { ApiResponse, TAssignCustomerToCompanyCredentials, TCompaniesResponse } from "@/lib/api";
|
||||
import { ILabelValue } from "@/types/shared";
|
||||
import { UseQueryResult } from "@tanstack/react-query";
|
||||
import { useForm } from "react-hook-form";
|
||||
|
||||
interface IProps {
|
||||
companiesQuery: UseQueryResult<ApiResponse<TCompaniesResponse>, Error>;
|
||||
setCompanies: Dispatch<
|
||||
SetStateAction<TAssignCustomerToCompanyCredentials | undefined>
|
||||
>;
|
||||
defaultValues?: TAssignCustomerToCompanyCredentials;
|
||||
}
|
||||
const AssignToCompanyModalContent: FC<IProps> = ({
|
||||
companiesQuery,
|
||||
setCompanies,
|
||||
defaultValues,
|
||||
}) => {
|
||||
const [options, setOptions] = useState<ILabelValue[]>([]);
|
||||
|
||||
const { data, status, isSuccess } = useCompanyFullList();
|
||||
const { data, status, isSuccess } = companiesQuery;
|
||||
const { handleSubmit, watch, setValue } =
|
||||
useForm<TAssignCustomerToCompanyCredentials>({
|
||||
mode: "onChange",
|
||||
@@ -54,6 +63,7 @@ const AssignToCompanyModalContent: FC<IProps> = ({
|
||||
<form className="mt-5" onSubmit={handleSubmit(assignCompany)}>
|
||||
<Select
|
||||
name="company_ids"
|
||||
clearable
|
||||
label="Companies"
|
||||
items={options}
|
||||
prefixIcon={<Building />}
|
||||
|
||||
@@ -51,6 +51,7 @@ const CustomersTable = () => {
|
||||
setFilterValue,
|
||||
isMutating,
|
||||
setParams,
|
||||
assignCompanyCompaniesQuery,
|
||||
} = useCustomerListPresenter();
|
||||
return (
|
||||
<div className="flex flex-col rounded-[10px] bg-white px-7.5 pb-4 pt-7.5 shadow-1 dark:bg-gray-dark dark:shadow-card">
|
||||
@@ -202,6 +203,7 @@ const CustomersTable = () => {
|
||||
</Modal>
|
||||
<div className="w-1/12">
|
||||
<Modal
|
||||
classNames="w-full xl:w-1/2"
|
||||
onConfirm={() => {
|
||||
if (!selectedCompanies || !currentCustomer) {
|
||||
toast.error("Invalid credentials");
|
||||
@@ -216,6 +218,7 @@ const CustomersTable = () => {
|
||||
onClose={() => setIsAssignCompanyModalOpen(false)}
|
||||
>
|
||||
<AssignToCompanyModalContent
|
||||
companiesQuery={assignCompanyCompaniesQuery}
|
||||
setCompanies={setSelectedCompanies}
|
||||
defaultValues={selectedCompanies}
|
||||
/>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import {
|
||||
useAssignCompany,
|
||||
useCompanyFullList,
|
||||
useCustomersQuery,
|
||||
useDeleteCustomerQuery,
|
||||
} from "@/hooks/queries";
|
||||
@@ -41,6 +42,9 @@ const useCustomerListPresenter = () => {
|
||||
} = useForm();
|
||||
|
||||
const { data, isPending } = useCustomersQuery(params);
|
||||
const assignCompanyCompaniesQuery = useCompanyFullList({
|
||||
enabled: isAssignCompanyModalOpen,
|
||||
});
|
||||
const { mutate: deleteCustomer } = useDeleteCustomerQuery(() =>
|
||||
setIsModalOpen(false),
|
||||
);
|
||||
@@ -104,6 +108,7 @@ const useCustomerListPresenter = () => {
|
||||
setFilterValue,
|
||||
isMutating,
|
||||
setParams,
|
||||
assignCompanyCompaniesQuery,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user