78 lines
2.4 KiB
TypeScript
78 lines
2.4 KiB
TypeScript
/* eslint-disable no-negated-condition */
|
||
import { FormContext } from './context/form-context';
|
||
import * as apiIus from '@/api/ius/query';
|
||
import { useFormStore } from '@/store/ius/form';
|
||
import { useRouter } from 'next/navigation';
|
||
import { useContext } from 'react';
|
||
import { Button } from 'ui';
|
||
|
||
export function Buttons() {
|
||
const { reset, resetValidation, setValidation, values } = useFormStore();
|
||
const { pageUrlParams, setFormStatus } = useContext(FormContext);
|
||
const router = useRouter();
|
||
|
||
return (
|
||
<div className="grid grid-cols-1 gap-2 gap-x-4 md:grid-cols-3">
|
||
<Button
|
||
intent="outline-danger"
|
||
onClick={() => {
|
||
reset();
|
||
}}
|
||
>
|
||
Отмена
|
||
</Button>
|
||
<Button
|
||
intent="outline-secondary"
|
||
onClick={() => {
|
||
setFormStatus('pending');
|
||
resetValidation();
|
||
apiIus.retract({ pageUrlParams, payload: values }).then((res) => {
|
||
if (typeof res !== 'boolean') {
|
||
setTimeout(() => {
|
||
setFormStatus('edit');
|
||
}, 300);
|
||
Object.keys(res.errors).forEach((name) => {
|
||
const elementValidation = res?.errors?.[name];
|
||
if (elementValidation)
|
||
setValidation({ message: elementValidation[0] ?? '', name, valid: false });
|
||
});
|
||
} else {
|
||
setFormStatus('success');
|
||
setTimeout(() => {
|
||
router.refresh();
|
||
}, 600);
|
||
}
|
||
});
|
||
}}
|
||
>
|
||
Возврат на доработку
|
||
</Button>
|
||
<Button
|
||
onClick={() => {
|
||
setFormStatus('pending');
|
||
resetValidation();
|
||
apiIus.save({ pageUrlParams, payload: values }).then((res) => {
|
||
if (typeof res !== 'boolean') {
|
||
setTimeout(() => {
|
||
setFormStatus('edit');
|
||
}, 300);
|
||
Object.keys(res.errors).forEach((name) => {
|
||
const elementValidation = res?.errors?.[name];
|
||
if (elementValidation)
|
||
setValidation({ message: elementValidation[0] ?? '', name, valid: false });
|
||
});
|
||
} else {
|
||
setFormStatus('success');
|
||
setTimeout(() => {
|
||
router.refresh();
|
||
}, 600);
|
||
}
|
||
});
|
||
}}
|
||
>
|
||
Сохранить
|
||
</Button>
|
||
</div>
|
||
);
|
||
}
|