fix: client delete fix
This commit is contained in:
@@ -15,6 +15,7 @@ export type ClientDetailedSchema = {
|
||||
comment?: (string | null);
|
||||
details?: (ClientDetailsSchema | null);
|
||||
chat?: (ChatSchema | null);
|
||||
isDeleted?: (boolean | null);
|
||||
pallets?: Array<ResidualPalletSchema>;
|
||||
boxes?: Array<ResidualBoxSchema>;
|
||||
};
|
||||
|
||||
@@ -13,5 +13,6 @@ export type ClientSchema = {
|
||||
comment?: (string | null);
|
||||
details?: (ClientDetailsSchema | null);
|
||||
chat?: (ChatSchema | null);
|
||||
isDeleted?: (boolean | null);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { FC, ReactNode } from "react";
|
||||
import { Select } from "@mantine/core";
|
||||
import { FC, ReactNode, useEffect, useState } from "react";
|
||||
import { Select, Text } from "@mantine/core";
|
||||
import { ClientSchema } from "../../../client";
|
||||
import useClientsList from "../../../pages/ClientsPage/hooks/useClientsList.tsx";
|
||||
|
||||
@@ -11,35 +11,65 @@ type Props = {
|
||||
inputContainer?: (children: ReactNode) => ReactNode;
|
||||
disabled?: boolean;
|
||||
};
|
||||
|
||||
type Option = {
|
||||
label: string;
|
||||
value: string;
|
||||
};
|
||||
|
||||
const ClientSelect: FC<Props> = ({ value, onChange, error, inputContainer, withLabel = false, disabled = false }) => {
|
||||
const { clients } = useClientsList();
|
||||
const options = clients.map(client => ({
|
||||
label: client.name,
|
||||
value: client.id.toString(),
|
||||
}));
|
||||
const { clients } = useClientsList({ all: true });
|
||||
const [options, setOptions] = useState<Option[]>([]);
|
||||
const [deletedClientIds, setDeletedClientIds] = useState<Set<number>>(new Set());
|
||||
const [errorMsg, setErrorMsg] = useState<string>("");
|
||||
|
||||
useEffect(() => {
|
||||
const options = clients
|
||||
.filter(client => !client.isDeleted)
|
||||
.map(client => ({
|
||||
label: client.name,
|
||||
value: client.id.toString(),
|
||||
}));
|
||||
setOptions(options);
|
||||
|
||||
const deletedClientIds = clients.filter(client => client.isDeleted).map(client => client.id);
|
||||
setDeletedClientIds(new Set(deletedClientIds));
|
||||
}, [options]);
|
||||
|
||||
useEffect(() => {
|
||||
if (value && deletedClientIds.has(value.id)) {
|
||||
setErrorMsg("Выбран удаленный клиент");
|
||||
} else {
|
||||
setErrorMsg("");
|
||||
}
|
||||
}, [value, deletedClientIds]);
|
||||
|
||||
return (
|
||||
<Select
|
||||
searchable
|
||||
placeholder={"Выберите клиента"}
|
||||
value={
|
||||
value &&
|
||||
options.find(client => client.value == value.id.toString())
|
||||
?.value
|
||||
}
|
||||
onChange={event => {
|
||||
if (!event) return;
|
||||
const client = clients.find(
|
||||
client => client.id == parseInt(event)
|
||||
);
|
||||
if (!client) return;
|
||||
onChange(client);
|
||||
}}
|
||||
data={options}
|
||||
label={withLabel && "Клиент"}
|
||||
error={error}
|
||||
inputContainer={inputContainer}
|
||||
disabled={disabled}
|
||||
/>
|
||||
<>
|
||||
<Select
|
||||
searchable
|
||||
placeholder={"Выберите клиента"}
|
||||
value={
|
||||
value &&
|
||||
options.find(client => client.value == value.id.toString())
|
||||
?.value
|
||||
}
|
||||
onChange={event => {
|
||||
if (!event) return;
|
||||
const client = clients.find(
|
||||
client => client.id == parseInt(event),
|
||||
);
|
||||
if (!client) return;
|
||||
onChange(client);
|
||||
}}
|
||||
data={options}
|
||||
label={withLabel && "Клиент"}
|
||||
error={error}
|
||||
inputContainer={inputContainer}
|
||||
disabled={disabled}
|
||||
/>
|
||||
{errorMsg && <Text size={"sm"} c={"red"}>{errorMsg}</Text>}
|
||||
</>
|
||||
);
|
||||
};
|
||||
export default ClientSelect;
|
||||
|
||||
@@ -8,7 +8,7 @@ import useClientsList from "../../../pages/ClientsPage/hooks/useClientsList.tsx"
|
||||
type Props = Omit<ObjectSelectProps<ClientSchema>, "data">;
|
||||
|
||||
const ClientSelectNew: FC<Props> = props => {
|
||||
const { clients } = useClientsList();
|
||||
const { clients } = useClientsList({ all: false });
|
||||
return (
|
||||
<ObjectSelect
|
||||
searchable
|
||||
|
||||
@@ -42,7 +42,7 @@ const ClientTab = () => {
|
||||
form.setValues(data);
|
||||
}, [card]);
|
||||
|
||||
const isEditorDisabled = () => client?.id !== card?.client?.id;
|
||||
const isEditorDisabled = () => client?.id !== card?.client?.id || client?.isDeleted === true;
|
||||
|
||||
const handleSubmitClientInfo = (values: ClientSchema) => {
|
||||
if (values.details?.phoneNumber === "+7 ") values.details!.phoneNumber = "";
|
||||
@@ -101,6 +101,7 @@ const ClientTab = () => {
|
||||
mask="+7 000 000-00-00"
|
||||
placeholder={"Введите номер телефона"}
|
||||
{...form.getInputProps("details.phoneNumber")}
|
||||
disabled={isEditorDisabled()}
|
||||
/>
|
||||
</Input.Wrapper>
|
||||
<TextInput
|
||||
|
||||
@@ -12,7 +12,7 @@ import ClientsTable from "./components/ClientsTable/ClientsTable.tsx";
|
||||
import ClientChatDrawer from "./drawers/ClientChatDrawer/ClientChatDrawer.tsx";
|
||||
|
||||
const ClientsPage: FC = () => {
|
||||
const { clients, refetch } = useClientsList();
|
||||
const { clients, refetch } = useClientsList({ all: false });
|
||||
const { filteredClients, search, setSearch } = useClientsFilter({ clients });
|
||||
const onCreate = (client: ClientSchema) => {
|
||||
ClientService.createClientApi({
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
import { ClientService } from "../../../client";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
|
||||
const useClientsList = () => {
|
||||
type Props = {
|
||||
all?: boolean;
|
||||
}
|
||||
|
||||
const useClientsList = ({ all }: Props) => {
|
||||
const { isPending, error, data, refetch } = useQuery({
|
||||
queryKey: ["getAllClients"],
|
||||
queryFn: ClientService.getAllClients,
|
||||
});
|
||||
const clients = isPending || error || !data ? [] : data.clients;
|
||||
let clients = isPending || error || !data ? [] : data.clients;
|
||||
if (!all) clients = clients.filter(client => !client.isDeleted);
|
||||
|
||||
return { clients, refetch };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user