fix: client delete fix

This commit is contained in:
2025-07-18 19:28:27 +04:00
parent 655ae077ca
commit 15a085721e
7 changed files with 72 additions and 34 deletions

View File

@@ -15,6 +15,7 @@ export type ClientDetailedSchema = {
comment?: (string | null); comment?: (string | null);
details?: (ClientDetailsSchema | null); details?: (ClientDetailsSchema | null);
chat?: (ChatSchema | null); chat?: (ChatSchema | null);
isDeleted?: (boolean | null);
pallets?: Array<ResidualPalletSchema>; pallets?: Array<ResidualPalletSchema>;
boxes?: Array<ResidualBoxSchema>; boxes?: Array<ResidualBoxSchema>;
}; };

View File

@@ -13,5 +13,6 @@ export type ClientSchema = {
comment?: (string | null); comment?: (string | null);
details?: (ClientDetailsSchema | null); details?: (ClientDetailsSchema | null);
chat?: (ChatSchema | null); chat?: (ChatSchema | null);
isDeleted?: (boolean | null);
}; };

View File

@@ -1,5 +1,5 @@
import { FC, ReactNode } from "react"; import { FC, ReactNode, useEffect, useState } from "react";
import { Select } from "@mantine/core"; import { Select, Text } from "@mantine/core";
import { ClientSchema } from "../../../client"; import { ClientSchema } from "../../../client";
import useClientsList from "../../../pages/ClientsPage/hooks/useClientsList.tsx"; import useClientsList from "../../../pages/ClientsPage/hooks/useClientsList.tsx";
@@ -11,13 +11,41 @@ type Props = {
inputContainer?: (children: ReactNode) => ReactNode; inputContainer?: (children: ReactNode) => ReactNode;
disabled?: boolean; disabled?: boolean;
}; };
type Option = {
label: string;
value: string;
};
const ClientSelect: FC<Props> = ({ value, onChange, error, inputContainer, withLabel = false, disabled = false }) => { const ClientSelect: FC<Props> = ({ value, onChange, error, inputContainer, withLabel = false, disabled = false }) => {
const { clients } = useClientsList(); const { clients } = useClientsList({ all: true });
const options = clients.map(client => ({ 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, label: client.name,
value: client.id.toString(), 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 ( return (
<>
<Select <Select
searchable searchable
placeholder={"Выберите клиента"} placeholder={"Выберите клиента"}
@@ -29,7 +57,7 @@ const ClientSelect: FC<Props> = ({ value, onChange, error, inputContainer, withL
onChange={event => { onChange={event => {
if (!event) return; if (!event) return;
const client = clients.find( const client = clients.find(
client => client.id == parseInt(event) client => client.id == parseInt(event),
); );
if (!client) return; if (!client) return;
onChange(client); onChange(client);
@@ -40,6 +68,8 @@ const ClientSelect: FC<Props> = ({ value, onChange, error, inputContainer, withL
inputContainer={inputContainer} inputContainer={inputContainer}
disabled={disabled} disabled={disabled}
/> />
{errorMsg && <Text size={"sm"} c={"red"}>{errorMsg}</Text>}
</>
); );
}; };
export default ClientSelect; export default ClientSelect;

View File

@@ -8,7 +8,7 @@ import useClientsList from "../../../pages/ClientsPage/hooks/useClientsList.tsx"
type Props = Omit<ObjectSelectProps<ClientSchema>, "data">; type Props = Omit<ObjectSelectProps<ClientSchema>, "data">;
const ClientSelectNew: FC<Props> = props => { const ClientSelectNew: FC<Props> = props => {
const { clients } = useClientsList(); const { clients } = useClientsList({ all: false });
return ( return (
<ObjectSelect <ObjectSelect
searchable searchable

View File

@@ -42,7 +42,7 @@ const ClientTab = () => {
form.setValues(data); form.setValues(data);
}, [card]); }, [card]);
const isEditorDisabled = () => client?.id !== card?.client?.id; const isEditorDisabled = () => client?.id !== card?.client?.id || client?.isDeleted === true;
const handleSubmitClientInfo = (values: ClientSchema) => { const handleSubmitClientInfo = (values: ClientSchema) => {
if (values.details?.phoneNumber === "+7 ") values.details!.phoneNumber = ""; if (values.details?.phoneNumber === "+7 ") values.details!.phoneNumber = "";
@@ -101,6 +101,7 @@ const ClientTab = () => {
mask="+7 000 000-00-00" mask="+7 000 000-00-00"
placeholder={"Введите номер телефона"} placeholder={"Введите номер телефона"}
{...form.getInputProps("details.phoneNumber")} {...form.getInputProps("details.phoneNumber")}
disabled={isEditorDisabled()}
/> />
</Input.Wrapper> </Input.Wrapper>
<TextInput <TextInput

View File

@@ -12,7 +12,7 @@ import ClientsTable from "./components/ClientsTable/ClientsTable.tsx";
import ClientChatDrawer from "./drawers/ClientChatDrawer/ClientChatDrawer.tsx"; import ClientChatDrawer from "./drawers/ClientChatDrawer/ClientChatDrawer.tsx";
const ClientsPage: FC = () => { const ClientsPage: FC = () => {
const { clients, refetch } = useClientsList(); const { clients, refetch } = useClientsList({ all: false });
const { filteredClients, search, setSearch } = useClientsFilter({ clients }); const { filteredClients, search, setSearch } = useClientsFilter({ clients });
const onCreate = (client: ClientSchema) => { const onCreate = (client: ClientSchema) => {
ClientService.createClientApi({ ClientService.createClientApi({

View File

@@ -1,12 +1,17 @@
import { ClientService } from "../../../client"; import { ClientService } from "../../../client";
import { useQuery } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query";
const useClientsList = () => { type Props = {
all?: boolean;
}
const useClientsList = ({ all }: Props) => {
const { isPending, error, data, refetch } = useQuery({ const { isPending, error, data, refetch } = useQuery({
queryKey: ["getAllClients"], queryKey: ["getAllClients"],
queryFn: ClientService.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 }; return { clients, refetch };
}; };