apps/web: connect buttons to backend

This commit is contained in:
vchikalkin 2023-11-22 00:50:07 +03:00
parent 3fb32b548c
commit c83c0e5c51
3 changed files with 54 additions and 10 deletions

View File

@ -49,8 +49,18 @@ export async function getConditions({ pageUrlParams }: Input) {
.then((res) => res);
}
export async function validate({ pageUrlParams, payload }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/validate' });
export async function save({ pageUrlParams, payload }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/transfer' });
return api
.post(payload, url)
.res<boolean>((res) => res.ok)
.then((res) => res)
.catch((error: WretchError) => error.json as t.HttpValidationError);
}
export async function retract({ pageUrlParams, payload }: Input) {
const url = createUrl({ ...pageUrlParams, route: '/return' });
return api
.post(payload, url)

View File

@ -1,12 +1,15 @@
/* 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, setValidation, values } = useFormStore();
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">
@ -22,19 +25,47 @@ export function Buttons() {
intent="secondary"
onClick={() => {
setFormStatus('pending');
resetValidation();
apiIus.retract({ pageUrlParams, payload: values }).then((res) => {
if (typeof res !== 'boolean') {
setTimeout(() => {
setFormStatus('edit');
}, 500);
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();
}, 800);
}
});
}}
>
Возврат на доработку
</Button>
<Button
onClick={() => {
apiIus.validate({ pageUrlParams, payload: values }).then((res) => {
setFormStatus('pending');
resetValidation();
apiIus.save({ pageUrlParams, payload: values }).then((res) => {
if (typeof res !== 'boolean') {
setTimeout(() => {
setFormStatus('edit');
}, 500);
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();
}, 800);
}
});
}}

View File

@ -12,6 +12,7 @@ type FormState = {
defaultValues: Values;
init: (values: Values) => void;
reset: () => void;
resetValidation: () => void;
setValidation: (input: { name: string } & ElementValidation) => void;
setValue: ({ name, value }: { name: string; value: Values[number] }) => void;
validation: Record<string, ElementValidation | undefined>;
@ -25,20 +26,22 @@ export const useFormStore = create<FormState>((set) => ({
defaultValues: values,
values,
})),
reset: () => {
reset: () =>
set((state) => ({
validation: {},
values: state.defaultValues,
}));
},
setValidation: ({ message, name, valid }) => {
})),
resetValidation: () =>
set(() => ({
validation: {},
})),
setValidation: ({ message, name, valid }) =>
set((state) => ({
validation: {
...state.validation,
[name]: { message, valid },
},
}));
},
})),
setValue: ({ name, value }) =>
set((state) => ({
validation: {