fix: client delete fix
This commit is contained in:
@@ -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>;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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,35 +11,65 @@ 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[]>([]);
|
||||||
label: client.name,
|
const [deletedClientIds, setDeletedClientIds] = useState<Set<number>>(new Set());
|
||||||
value: client.id.toString(),
|
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 (
|
return (
|
||||||
<Select
|
<>
|
||||||
searchable
|
<Select
|
||||||
placeholder={"Выберите клиента"}
|
searchable
|
||||||
value={
|
placeholder={"Выберите клиента"}
|
||||||
value &&
|
value={
|
||||||
options.find(client => client.value == value.id.toString())
|
value &&
|
||||||
?.value
|
options.find(client => client.value == value.id.toString())
|
||||||
}
|
?.value
|
||||||
onChange={event => {
|
}
|
||||||
if (!event) return;
|
onChange={event => {
|
||||||
const client = clients.find(
|
if (!event) return;
|
||||||
client => client.id == parseInt(event)
|
const client = clients.find(
|
||||||
);
|
client => client.id == parseInt(event),
|
||||||
if (!client) return;
|
);
|
||||||
onChange(client);
|
if (!client) return;
|
||||||
}}
|
onChange(client);
|
||||||
data={options}
|
}}
|
||||||
label={withLabel && "Клиент"}
|
data={options}
|
||||||
error={error}
|
label={withLabel && "Клиент"}
|
||||||
inputContainer={inputContainer}
|
error={error}
|
||||||
disabled={disabled}
|
inputContainer={inputContainer}
|
||||||
/>
|
disabled={disabled}
|
||||||
|
/>
|
||||||
|
{errorMsg && <Text size={"sm"} c={"red"}>{errorMsg}</Text>}
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
export default ClientSelect;
|
export default ClientSelect;
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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({
|
||||||
|
|||||||
@@ -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 };
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user