From c83c0e5c518b6d96b322a116d01e3ee1ba9a6df2 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Wed, 22 Nov 2023 00:50:07 +0300 Subject: [PATCH] apps/web: connect buttons to backend --- apps/web/api/ius/query.ts | 14 +++++++++-- apps/web/components/Form/Buttons.tsx | 35 ++++++++++++++++++++++++++-- apps/web/store/ius/form.ts | 15 +++++++----- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/apps/web/api/ius/query.ts b/apps/web/api/ius/query.ts index 776fb34..e6b2130 100644 --- a/apps/web/api/ius/query.ts +++ b/apps/web/api/ius/query.ts @@ -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((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) diff --git a/apps/web/components/Form/Buttons.tsx b/apps/web/components/Form/Buttons.tsx index 882588a..c142b05 100644 --- a/apps/web/components/Form/Buttons.tsx +++ b/apps/web/components/Form/Buttons.tsx @@ -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 (
@@ -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); + } + }); }} > Возврат на доработку