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:
@@ -115,7 +115,11 @@ export function Select<T extends FieldValues>({
|
|||||||
clearable && value && "pr-20",
|
clearable && value && "pr-20",
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{placeholder && <option value="">{placeholder}</option>}
|
{placeholder && (
|
||||||
|
<option value="" disabled>
|
||||||
|
{placeholder}
|
||||||
|
</option>
|
||||||
|
)}
|
||||||
|
|
||||||
{items.map((item) => (
|
{items.map((item) => (
|
||||||
<option key={item.value} value={item.value}>
|
<option key={item.value} value={item.value}>
|
||||||
|
|||||||
@@ -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 { Select } from "@/components/FormElements/select";
|
||||||
import { Building } from "@/components/Layouts/sidebar/icons";
|
import { Building } from "@/components/Layouts/sidebar/icons";
|
||||||
import { useCompanyFullList } from "@/hooks/queries";
|
import { ApiResponse, TAssignCustomerToCompanyCredentials, TCompaniesResponse } from "@/lib/api";
|
||||||
import { TAssignCustomerToCompanyCredentials } from "@/lib/api";
|
|
||||||
import { ILabelValue } from "@/types/shared";
|
import { ILabelValue } from "@/types/shared";
|
||||||
|
import { UseQueryResult } from "@tanstack/react-query";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
|
companiesQuery: UseQueryResult<ApiResponse<TCompaniesResponse>, Error>;
|
||||||
setCompanies: Dispatch<
|
setCompanies: Dispatch<
|
||||||
SetStateAction<TAssignCustomerToCompanyCredentials | undefined>
|
SetStateAction<TAssignCustomerToCompanyCredentials | undefined>
|
||||||
>;
|
>;
|
||||||
defaultValues?: TAssignCustomerToCompanyCredentials;
|
defaultValues?: TAssignCustomerToCompanyCredentials;
|
||||||
}
|
}
|
||||||
const AssignToCompanyModalContent: FC<IProps> = ({
|
const AssignToCompanyModalContent: FC<IProps> = ({
|
||||||
|
companiesQuery,
|
||||||
setCompanies,
|
setCompanies,
|
||||||
defaultValues,
|
defaultValues,
|
||||||
}) => {
|
}) => {
|
||||||
const [options, setOptions] = useState<ILabelValue[]>([]);
|
const [options, setOptions] = useState<ILabelValue[]>([]);
|
||||||
|
|
||||||
const { data, status, isSuccess } = useCompanyFullList();
|
const { data, status, isSuccess } = companiesQuery;
|
||||||
const { handleSubmit, watch, setValue } =
|
const { handleSubmit, watch, setValue } =
|
||||||
useForm<TAssignCustomerToCompanyCredentials>({
|
useForm<TAssignCustomerToCompanyCredentials>({
|
||||||
mode: "onChange",
|
mode: "onChange",
|
||||||
@@ -54,6 +63,7 @@ const AssignToCompanyModalContent: FC<IProps> = ({
|
|||||||
<form className="mt-5" onSubmit={handleSubmit(assignCompany)}>
|
<form className="mt-5" onSubmit={handleSubmit(assignCompany)}>
|
||||||
<Select
|
<Select
|
||||||
name="company_ids"
|
name="company_ids"
|
||||||
|
clearable
|
||||||
label="Companies"
|
label="Companies"
|
||||||
items={options}
|
items={options}
|
||||||
prefixIcon={<Building />}
|
prefixIcon={<Building />}
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ const CustomersTable = () => {
|
|||||||
setFilterValue,
|
setFilterValue,
|
||||||
isMutating,
|
isMutating,
|
||||||
setParams,
|
setParams,
|
||||||
|
assignCompanyCompaniesQuery,
|
||||||
} = useCustomerListPresenter();
|
} = useCustomerListPresenter();
|
||||||
return (
|
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">
|
<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>
|
</Modal>
|
||||||
<div className="w-1/12">
|
<div className="w-1/12">
|
||||||
<Modal
|
<Modal
|
||||||
|
classNames="w-full xl:w-1/2"
|
||||||
onConfirm={() => {
|
onConfirm={() => {
|
||||||
if (!selectedCompanies || !currentCustomer) {
|
if (!selectedCompanies || !currentCustomer) {
|
||||||
toast.error("Invalid credentials");
|
toast.error("Invalid credentials");
|
||||||
@@ -216,6 +218,7 @@ const CustomersTable = () => {
|
|||||||
onClose={() => setIsAssignCompanyModalOpen(false)}
|
onClose={() => setIsAssignCompanyModalOpen(false)}
|
||||||
>
|
>
|
||||||
<AssignToCompanyModalContent
|
<AssignToCompanyModalContent
|
||||||
|
companiesQuery={assignCompanyCompaniesQuery}
|
||||||
setCompanies={setSelectedCompanies}
|
setCompanies={setSelectedCompanies}
|
||||||
defaultValues={selectedCompanies}
|
defaultValues={selectedCompanies}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
useAssignCompany,
|
useAssignCompany,
|
||||||
|
useCompanyFullList,
|
||||||
useCustomersQuery,
|
useCustomersQuery,
|
||||||
useDeleteCustomerQuery,
|
useDeleteCustomerQuery,
|
||||||
} from "@/hooks/queries";
|
} from "@/hooks/queries";
|
||||||
@@ -41,6 +42,9 @@ const useCustomerListPresenter = () => {
|
|||||||
} = useForm();
|
} = useForm();
|
||||||
|
|
||||||
const { data, isPending } = useCustomersQuery(params);
|
const { data, isPending } = useCustomersQuery(params);
|
||||||
|
const assignCompanyCompaniesQuery = useCompanyFullList({
|
||||||
|
enabled: isAssignCompanyModalOpen,
|
||||||
|
});
|
||||||
const { mutate: deleteCustomer } = useDeleteCustomerQuery(() =>
|
const { mutate: deleteCustomer } = useDeleteCustomerQuery(() =>
|
||||||
setIsModalOpen(false),
|
setIsModalOpen(false),
|
||||||
);
|
);
|
||||||
@@ -104,6 +108,7 @@ const useCustomerListPresenter = () => {
|
|||||||
setFilterValue,
|
setFilterValue,
|
||||||
isMutating,
|
isMutating,
|
||||||
setParams,
|
setParams,
|
||||||
|
assignCompanyCompaniesQuery,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -83,10 +83,12 @@ export const useDeleteCompanyQuery = (successCallback?: () => void) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
export const useCompanyFullList = () => {
|
export const useCompanyFullList = (options?: { enabled?: boolean }) => {
|
||||||
|
const enabled = options?.enabled ?? true;
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: ["READ ALL Companies"],
|
queryKey: ["READ ALL Companies"],
|
||||||
queryFn: companiesService.getCompanies,
|
queryFn: companiesService.getCompanies,
|
||||||
|
enabled,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
export const useCompaniesInfiniteQuery = (params?: Record<string, any>) => {
|
export const useCompaniesInfiniteQuery = (params?: Record<string, any>) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user