From a9a9b3cadd94fc5b8f3f88828a588042cfaa74f3 Mon Sep 17 00:00:00 2001 From: RustamRu Date: Sun, 19 Jan 2025 10:55:44 +0300 Subject: [PATCH] feat: create order api --- src/components/order-form/form/helper.ts | 20 ++++++++++++++++---- stubs/api/index.js | 6 ++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/components/order-form/form/helper.ts b/src/components/order-form/form/helper.ts index d052752..53f8bd8 100644 --- a/src/components/order-form/form/helper.ts +++ b/src/components/order-form/form/helper.ts @@ -1,4 +1,5 @@ import { useTranslation } from "react-i18next"; +import { getConfigValue } from '@brojs/cli'; import { InputProps, SelectProps } from "@chakra-ui/react"; import { Order } from "../../../models/landing"; @@ -56,11 +57,22 @@ export const formatFormValues = ({ phone, carNumber, carBody, carColor, carLocat }; }; -export const onSubmit = (values: OrderFormValues) => { - return new Promise((resolve) => { - console.log(formatFormValues(values)); - resolve(formatFormValues(values)); +const endpoint = getConfigValue('dry-wash.api'); + +export const onSubmit = async (values: OrderFormValues) => { + const response = await fetch(`${endpoint}/order/create`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(formatFormValues(values)), }); + + if (!response.ok) { + throw new Error(`Failed to create order: ${response.status}`); + } + + return await response.json(); }; export const inputCommonStyles: Partial = { diff --git a/stubs/api/index.js b/stubs/api/index.js index f360596..23f8fd5 100644 --- a/stubs/api/index.js +++ b/stubs/api/index.js @@ -76,6 +76,12 @@ router.get('/order/:orderId', ({ params }, res) => { ); }); +router.post('/order/create', (req, res) => { + res + .status(200) + .send({ success: true, body: { ok: true } }); +}); + router.use('/admin', require('./admin')); module.exports = router;