Form/Common: pass http errors
This commit is contained in:
parent
08aee48f07
commit
54df128e56
@ -55,7 +55,7 @@ export async function save({ pageUrlParams, payload }: Input) {
|
|||||||
.post(payload, url)
|
.post(payload, url)
|
||||||
.res<boolean>((res) => res.ok)
|
.res<boolean>((res) => res.ok)
|
||||||
.then((res) => res)
|
.then((res) => res)
|
||||||
.catch((error: WretchError) => error.json as t.HttpValidationError);
|
.catch((error: WretchError) => error.json as t.HttpValidationError | t.HttpError);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function retract({ pageUrlParams, payload }: Input) {
|
export async function retract({ pageUrlParams, payload }: Input) {
|
||||||
@ -65,7 +65,7 @@ export async function retract({ pageUrlParams, payload }: Input) {
|
|||||||
.post(payload, url)
|
.post(payload, url)
|
||||||
.res<boolean>((res) => res.ok)
|
.res<boolean>((res) => res.ok)
|
||||||
.then((res) => res)
|
.then((res) => res)
|
||||||
.catch((error: WretchError) => error.json as t.HttpValidationError);
|
.catch((error: WretchError) => error.json as t.HttpValidationError | t.HttpError);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getDocumentTypes({ pageUrlParams }: Input) {
|
export async function getDocumentTypes({ pageUrlParams }: Input) {
|
||||||
|
|||||||
@ -6,6 +6,9 @@ import { useFormStore } from '@/store/ius/form';
|
|||||||
import { Button } from '@repo/ui';
|
import { Button } from '@repo/ui';
|
||||||
import { useCallback, useContext } from 'react';
|
import { useCallback, useContext } from 'react';
|
||||||
|
|
||||||
|
const ERROR_RETRACT = 'Произошла ошибка при возврате на доработку';
|
||||||
|
const ERROR_SAVE = 'Произошла ошибка при сохранении';
|
||||||
|
|
||||||
export function Buttons() {
|
export function Buttons() {
|
||||||
const { reset, resetValidation, setValidation, status, values } = useFormStore();
|
const { reset, resetValidation, setValidation, status, values } = useFormStore();
|
||||||
const { pageUrlParams, setFormState } = useContext(FormContext);
|
const { pageUrlParams, setFormState } = useContext(FormContext);
|
||||||
@ -15,14 +18,23 @@ export function Buttons() {
|
|||||||
resetValidation();
|
resetValidation();
|
||||||
apiIus.save({ pageUrlParams, payload: values }).then((res) => {
|
apiIus.save({ pageUrlParams, payload: values }).then((res) => {
|
||||||
if (typeof res !== 'boolean') {
|
if (typeof res !== 'boolean') {
|
||||||
setTimeout(() => {
|
const { errors } = res;
|
||||||
setFormState({ status: 'edit' });
|
|
||||||
}, 300);
|
if (Array.isArray(errors)) {
|
||||||
Object.keys(res.errors).forEach((name) => {
|
setFormState({ status: 'error', text: errors?.at(0) || ERROR_SAVE });
|
||||||
const elementValidation = res?.errors?.[name];
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.keys(errors).forEach((name) => {
|
||||||
|
const elementValidation = errors?.[name];
|
||||||
if (elementValidation)
|
if (elementValidation)
|
||||||
setValidation({ message: elementValidation[0] ?? '', name, valid: false });
|
setValidation({ message: elementValidation[0] ?? '', name, valid: false });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
setFormState({ status: 'edit' });
|
||||||
|
}, 300);
|
||||||
} else {
|
} else {
|
||||||
setFormState({ status: 'success' });
|
setFormState({ status: 'success' });
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@ -37,11 +49,19 @@ export function Buttons() {
|
|||||||
resetValidation();
|
resetValidation();
|
||||||
apiIus.retract({ pageUrlParams, payload: values }).then((res) => {
|
apiIus.retract({ pageUrlParams, payload: values }).then((res) => {
|
||||||
if (typeof res !== 'boolean') {
|
if (typeof res !== 'boolean') {
|
||||||
|
const { errors } = res;
|
||||||
|
|
||||||
|
if (Array.isArray(errors)) {
|
||||||
|
setFormState({ status: 'error', text: errors?.at(0) || ERROR_RETRACT });
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
setFormState({ status: 'edit' });
|
setFormState({ status: 'edit' });
|
||||||
}, 300);
|
}, 300);
|
||||||
Object.keys(res.errors).forEach((name) => {
|
Object.keys(errors).forEach((name) => {
|
||||||
const elementValidation = res?.errors?.[name];
|
const elementValidation = errors?.[name];
|
||||||
if (elementValidation)
|
if (elementValidation)
|
||||||
setValidation({ message: elementValidation[0] ?? '', name, valid: false });
|
setValidation({ message: elementValidation[0] ?? '', name, valid: false });
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user