Skip to content

Commit a809051

Browse files
committed
[DOP-33475] Добавить компонент для работы с SQL
1 parent 1389a91 commit a809051

8 files changed

Lines changed: 45 additions & 10 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export * from './prepareTransformationRequest';
21
export * from './prepareTransformationForm';
2+
export * from './prepareTransformationFormError';
3+
export * from './prepareTransformationRequest';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { FormFieldError } from '@shared/config';
2+
3+
import { TransformationType } from '../../types';
4+
5+
/** Util for preparing received errors for use on a form */
6+
export const prepareTransformationFormError = (errors: FormFieldError[]): FormFieldError[] => {
7+
return errors.map((error) => {
8+
const { location } = error;
9+
if (location[1] !== 'transformations') return error;
10+
11+
switch ((location as string[])[3] as TransformationType) {
12+
case TransformationType.FILTER_SQL:
13+
//location : ['body', 'transformations', 3, 'sql', 'query']
14+
/* From the backend, the error record comes in a simple form in accordance with the api.
15+
Return it to the state it was stored in the form */
16+
const newLocation = location.slice(0, 4).concat(0).concat(location.slice(4));
17+
return { ...error, location: newLocation } as FormFieldError;
18+
19+
default:
20+
return error;
21+
}
22+
});
23+
};

src/shared/config/errors/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface DefaultError {
66
type LocationTypeError = 'body' | 'query';
77

88
export interface FormFieldError extends DefaultError {
9-
location: [Extract<LocationTypeError, 'body'>, string];
9+
location: [Extract<LocationTypeError, 'body'>, string | number];
1010
}
1111

1212
export interface MessageError {

src/shared/ui/ManagedForm/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React, { useLayoutEffect, useState } from 'react';
1+
import { useLayoutEffect, useState } from 'react';
22
import { Form, notification, Spin } from 'antd';
33
import { PropsWithChildren } from 'react';
44
import { useMutation, useQueryClient } from '@tanstack/react-query';
55
import { clsx } from 'clsx';
6-
import { checkIsFormFieldsError, getErrorMessage } from '@shared/config';
6+
import { checkIsFormFieldsError, FormFieldError, getErrorMessage } from '@shared/config';
77
import { useTranslation } from 'react-i18next';
88

99
import * as classes from './styles.module.less';
@@ -19,6 +19,7 @@ export const ManagedForm = <T extends object, R>({
1919
keysInvalidateQueries = [],
2020
isHiddenLoadingOnSuccess = false,
2121
onError = () => undefined,
22+
prepareFormErrors,
2223
...props
2324
}: PropsWithChildren<ManagedFormProps<T, R>>) => {
2425
const { t, i18n } = useTranslation('error');
@@ -51,7 +52,9 @@ export const ManagedForm = <T extends object, R>({
5152

5253
if (checkIsFormFieldsError(error) && error.response) {
5354
message = t('formErrorHasOccurred');
54-
showErrorsInFields(form, error.response.data.error.details);
55+
const errors: FormFieldError[] = error.response.data.error.details;
56+
const preparedErrors = prepareFormErrors ? prepareFormErrors(errors) : errors;
57+
showErrorsInFields(form, preparedErrors);
5558
}
5659

5760
notification.error({

src/shared/ui/ManagedForm/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FormFieldError } from '@shared/config';
12
import { QueryClient } from '@tanstack/react-query';
23
import { FormProps } from 'antd';
34

@@ -20,4 +21,6 @@ export interface ManagedFormProps<T, R> extends Omit<FormProps<T>, 'form' | 'onF
2021
keysInvalidateQueries?: Parameters<QueryClient['invalidateQueries']>[];
2122
/** Flag that hides a spin if the request is successful */
2223
isHiddenLoadingOnSuccess?: boolean;
24+
/** Callback to prepare received errors */
25+
prepareFormErrors?: (errors: FormFieldError[]) => FormFieldError[];
2326
}

src/shared/ui/ManagedForm/utils/showErrorsInFields/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export const showErrorsInFields = (form: FormInstance, errors: FormFieldError[])
1212
const fieldErrors = errors
1313
.map((field) => {
1414
// Keep only valid parts of the path
15-
const validPath = field.location.reduce<string[]>((acc, key) => {
15+
const validPath = field.location.reduce<Array<string | number>>((acc, key) => {
1616
const currentPath = [...acc, key];
1717
// Check if the path to the current key exists
18-
if (typeof form.getFieldValue(currentPath) === 'object' || form.getFieldInstance(currentPath)) {
18+
if (typeof form.getFieldValue(currentPath) === 'object' || form.isFieldTouched(currentPath)) {
1919
acc.push(key);
2020
}
2121
return acc;

src/widgets/transfer/CreateTransfer/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import React from 'react';
21
import { ManagedForm } from '@shared/ui';
32
import { useNavigate } from 'react-router-dom';
43
import { prepareTransferResourcesRequest, Transfer, TransferQueryKey, transferService } from '@entities/transfer';
54
import { MutateTransferForm } from '@features/transfer';
6-
import { prepareTransformationRequest } from '@entities/transformation';
5+
import { prepareTransformationFormError, prepareTransformationRequest } from '@entities/transformation';
76
import { useTranslation } from 'react-i18next';
87

98
import { CreateTransferForm, CreateTransferProps } from './types';
@@ -36,6 +35,7 @@ export const CreateTransfer = ({ group }: CreateTransferProps) => {
3635
mutationFunction={handleCreateTransfer}
3736
onSuccess={onSuccess}
3837
successMessage={t('createTransferSuccess')}
38+
prepareFormErrors={prepareTransformationFormError}
3939
keysInvalidateQueries={[[{ queryKey: [TransferQueryKey.GET_TRANSFERS, group.id] }]]}
4040
>
4141
<MutateTransferForm group={group} onCancel={onCancel} />

src/widgets/transfer/UpdateTransfer/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
transferService,
1010
} from '@entities/transfer';
1111
import { MutateTransferForm } from '@features/transfer';
12-
import { prepareTransformationForm, prepareTransformationRequest } from '@entities/transformation';
12+
import {
13+
prepareTransformationForm,
14+
prepareTransformationFormError,
15+
prepareTransformationRequest,
16+
} from '@entities/transformation';
1317
import { useTranslation } from 'react-i18next';
1418

1519
import { UpdateTransferForm, UpdateTransferProps } from './types';
@@ -47,6 +51,7 @@ export const UpdateTransfer = ({ transfer, group }: UpdateTransferProps) => {
4751
mutationFunction={handleUpdateTransfer}
4852
onSuccess={onSuccess}
4953
successMessage={t('updateTransferSuccess')}
54+
prepareFormErrors={prepareTransformationFormError}
5055
keysInvalidateQueries={[
5156
[{ queryKey: [TransferQueryKey.GET_TRANSFERS, group.id] }],
5257
[{ queryKey: [TransferQueryKey.GET_TRANSFER, transfer.id] }],

0 commit comments

Comments
 (0)