AURORA-1671: добавленна отображения ошибки crm_id_exists напрямую в форму

This commit is contained in:
Grog 2026-10-26 14:11:48 +03:00
parent 633c4d3fed
commit daec128fc3
7 changed files with 63 additions and 104 deletions

View File

@ -1,7 +1,6 @@
import { PlusOutlined } from '@ant-design/icons';
import { Button, Space } from 'antd';
import { useEffect, useState } from 'react';
import { ApiError } from '../api/client';
import PageTitle from '../components/PageTitle';
import { createCatalogueJur } from '../services/api/directories/catalogue_jur';
import { createCompanyGroup, fetchRelatedGroups } from '../services/api/directories/company_groups';
@ -13,7 +12,6 @@ import type { RelatedGroupOut } from '../types/api';
import type { CatalogueJurFormProps } from '../types/catalogueJur/catalogueJur';
import type { CreateCompanyGroupProps } from '../types/companyGroups/companyGroups';
import type { CreateEntityKind, RelatedGroupFormType } from '../types/relatedGroup/relatedGroup';
import { useNotificationApi } from '../utils/useNotification';
import { CataloguGroupTabs, type CatalogueGroupTabKey } from '../widgets/CataloguGroupTabs/CataloguGroupTabs';
import { CompanyGroupHeader } from '../widgets/CatalogueGroupHeader/CatalogueGroupHeader';
import { CatalogueJurForm } from '../widgets/CatalogueJurForm/CatalogueJurForm';
@ -26,7 +24,6 @@ import { RelatedGroupTable } from '../widgets/RelatedGroupTable/RelatedGroupTabl
export function CatalogueGroup() {
const dispatch = useAppDispatch();
const notification = useNotificationApi();
const isGettingArchive = useAppSelector((state) => state.catalogue_jur.isGettingArchive);
@ -53,55 +50,30 @@ export function CatalogueGroup() {
}
async function createRelatedGroupHandler(values: RelatedGroupFormType) {
if (values.company_group_id) {
try {
await createRelatedGroup(values.company_group_id, {
...values,
company_group_id: undefined,
});
getRelatedGroupsHandler();
setKindForm(null);
} catch (e) {
if (e instanceof ApiError) {
notification.error({
description: e.message,
});
}
}
await createRelatedGroup(values.company_group_id, {
...values,
company_group_id: undefined,
});
getRelatedGroupsHandler();
setKindForm(null);
}
}
async function createCompanyGroupHandler(values: CreateCompanyGroupProps) {
try {
await createCompanyGroup(values);
dispatch(getCompanyGroupsThunk());
getRelatedGroupsHandler();
setKindForm(null);
} catch (e) {
if (e instanceof ApiError) {
notification.error({
description: e.message,
});
}
}
await createCompanyGroup(values);
dispatch(getCompanyGroupsThunk());
getRelatedGroupsHandler();
setKindForm(null);
}
async function getRelatedGroupsHandler() {
if (activeCompanyGroup?.id) {
setRelatedGroupLoading(true);
try {
const res = await fetchRelatedGroups(activeCompanyGroup?.id);
const gsks = res.filter((elem) => elem.kind === 'gsk');
const gszs = res.filter((elem) => elem.kind === 'gsz');
setGskData(gsks);
setGszData(gszs);
} catch (e) {
if (e instanceof ApiError) {
notification.error({
description: e.message,
});
}
} finally {
setRelatedGroupLoading(false);
}
const res = await fetchRelatedGroups(activeCompanyGroup?.id);
const gsks = res.filter((elem) => elem.kind === 'gsk');
const gszs = res.filter((elem) => elem.kind === 'gsz');
setGskData(gsks);
setGszData(gszs);
setRelatedGroupLoading(false);
}
}
@ -197,9 +169,7 @@ export function CatalogueGroup() {
onCancel={() => {
setKindForm(null);
}}
onOk={async (values) => {
await createRelatedGroupHandler(values);
}}
onOk={createRelatedGroupHandler}
/>
)}
{kindForm === 'gk' && (
@ -208,9 +178,7 @@ export function CatalogueGroup() {
onCancel={() => {
setKindForm(null);
}}
onOk={async (values) => {
await createCompanyGroupHandler(values);
}}
onOk={createCompanyGroupHandler}
/>
)}
</div>

View File

@ -1,41 +1,30 @@
import { PlusOutlined } from '@ant-design/icons';
import { Button, Space } from 'antd';
import { useState } from 'react';
import { ApiError } from '../api/client';
import PageTitle from '../components/PageTitle';
import { createCatalogueJur } from '../services/api/directories/catalogue_jur';
import { getCatalogueJurThunk } from '../store/slices/catalogueJurSlice/thunks';
import { useAppDispatch, useAppSelector } from '../store/store';
import type { CatalogueJurFormProps } from '../types/catalogueJur/catalogueJur';
import { useNotificationApi } from '../utils/useNotification';
import { CatalogueJurForm } from '../widgets/CatalogueJurForm/CatalogueJurForm';
import { CatalogueJurTable } from '../widgets/CatalogueJurTable/CatalogueJurTable';
export function CatalogueJur() {
const dispatch = useAppDispatch();
const notification = useNotificationApi();
const isGettingArchive = useAppSelector((state) => state.catalogue_jur.isGettingArchive);
const [createFormOpen, setCreateFormOpen] = useState(false);
const outside_group = true;
async function createJurHandler(values: CatalogueJurFormProps) {
try {
await createCatalogueJur(values);
dispatch(
getCatalogueJurThunk({
closed: isGettingArchive,
outside_group: outside_group,
}),
);
setCreateFormOpen(false);
} catch (e) {
if (e instanceof ApiError) {
notification.error({
description: e.message,
});
}
}
await createCatalogueJur(values);
dispatch(
getCatalogueJurThunk({
closed: isGettingArchive,
outside_group: outside_group,
}),
);
setCreateFormOpen(false);
}
return (
@ -59,9 +48,7 @@ export function CatalogueJur() {
onCancel={() => {
setCreateFormOpen(false);
}}
onOk={async (values) => {
await createJurHandler(values);
}}
onOk={createJurHandler}
/>
)}
</div>

View File

@ -124,6 +124,14 @@ export const CatalogueJurForm: React.FC<Props> = ({ open, onCancel, onOk, jurId
},
]);
}
if (e.code === 'CRM_ID_EXISTS') {
form.setFields([
{
name: 'crm_id',
errors: [e.message],
},
]);
}
notification.error({ description: e.message });
}
} finally {

View File

@ -122,21 +122,13 @@ export function CatalogueJurTable({ outside_group, tabsComponent }: CatalogueJur
}
async function editJurHandler(values: CatalogueJurFormProps) {
try {
await editCatalogueJur(jurId, values);
getCatalogueJur();
closeEditCatalogueJurHandler();
} catch (e) {
if (e instanceof ApiError) {
notification.error({
description: e.message,
});
}
}
await editCatalogueJur(jurId, values);
getCatalogueJur();
closeEditCatalogueJurHandler();
}
useEffect(() => {
getCatalogueJur();
getCatalogueJur();
}, [isGettingArchive, activeCompanyGroup, outside_group]);
const extraColumns: ColumnsType<ICatalogueJur> = useMemo(
@ -327,9 +319,7 @@ export function CatalogueJurTable({ outside_group, tabsComponent }: CatalogueJur
onCancel={() => {
closeEditCatalogueJurHandler();
}}
onOk={async (values) => {
await editJurHandler(values);
}}
onOk={editJurHandler}
/>
)}
</div>

View File

@ -49,6 +49,14 @@ export const CompanyGroupForm: React.FC<Props> = ({ open, onCancel, onOk }) => {
});
} catch (e) {
if (e instanceof ApiError) {
if (e.code === 'CRM_ID_EXISTS') {
form.setFields([
{
name: 'crm_id',
errors: [e.message],
},
]);
}
notification.error({
description: e.message,
});

View File

@ -59,7 +59,7 @@ export const RelatedGroupForm: React.FC<Props> = ({ open, onCancel, onOk, relate
try {
const { created_date, name, crm_id, company_group_id } = await form.validateFields();
await onOk({
created_date: created_date ?? (relatedGroupId ? null : dayjs().format('YYYY-MM-DD') ),
created_date: created_date ?? (relatedGroupId ? null : dayjs().format('YYYY-MM-DD')),
name,
crm_id,
company_group_id: relatedGroupId ? undefined : company_group_id,
@ -67,6 +67,14 @@ export const RelatedGroupForm: React.FC<Props> = ({ open, onCancel, onOk, relate
});
} catch (e) {
if (e instanceof ApiError) {
if (e.code === 'CRM_ID_EXISTS') {
form.setFields([
{
name: 'crm_id',
errors: [e.message],
},
]);
}
notification.error({
description: e.message,
});
@ -101,8 +109,8 @@ export const RelatedGroupForm: React.FC<Props> = ({ open, onCancel, onOk, relate
loading={initialDataLoading}
confirmLoading={confirmLoading}>
<Form form={form} layout='vertical'>
<FormItem name='crm_id' label='ID в CRM'>
<Input />
<FormItem name='crm_id' label='ID в CRM' >
<Input />
</FormItem>
<FormItem name='name' label='Наименование' rules={[{ required: true, message: 'Укажите наименование' }]}>
<Input />

View File

@ -62,17 +62,9 @@ export function RelatedGroupTable({ tabsComponent, data, loading, getData, activ
}
async function editRelatedGroupHandler(values: RelatedGroupFormType) {
try {
await editRelatedGroup(groupId, values);
getData();
closeEditRelatedGroupHandler();
} catch (e) {
if (e instanceof ApiError) {
notification.error({
description: e.message,
});
}
}
await editRelatedGroup(groupId, values);
getData();
closeEditRelatedGroupHandler();
}
const extraColumns: ColumnsType<RelatedGroupOut> = useMemo(
@ -167,9 +159,7 @@ export function RelatedGroupTable({ tabsComponent, data, loading, getData, activ
onCancel={() => {
closeEditRelatedGroupHandler();
}}
onOk={async (values) => {
await editRelatedGroupHandler(values);
}}
onOk={editRelatedGroupHandler}
/>
)}
{deleteModalOpen && (