diff --git a/backend/routes/app/database/products.database.js b/backend/routes/app/database/products.database.js index b91dfe8..515f354 100644 --- a/backend/routes/app/database/products.database.js +++ b/backend/routes/app/database/products.database.js @@ -20,12 +20,19 @@ export const newProduct = async ( expiry_date, bottling_date, ) => { + let newPrice; + if (price == "") { + newPrice = null; + } else { + newPrice = price; + } + const [result] = await pool.query( "INSERT INTO products (name, description, price, amount, storage_location, expiry_date, bottling_date) VALUES (?, ?, ?, ?, UUID_TO_BIN(?), ?, ?)", [ name, description, - price, + newPrice, amount, storage_location, expiry_date, diff --git a/frontend/src/pages/AddProduct.tsx b/frontend/src/pages/AddProduct.tsx new file mode 100644 index 0000000..5a644ac --- /dev/null +++ b/frontend/src/pages/AddProduct.tsx @@ -0,0 +1,271 @@ +import { useMutation, useQuery } from "@tanstack/react-query"; +import { + Alert, + Box, + Button, + Chip, + Divider, + Input, + Option, + Select, + Typography, +} from "@mui/joy"; +import { useTranslation } from "react-i18next"; +import { useState } from "react"; +import { useForm } from "@tanstack/react-form"; +import { createProduct, getStorages } from "../utils/uxFncs"; +import type { ProductFormValues } from "../misc/interfaces"; + +export const AddProduct = () => { + const { t } = useTranslation(); + const [success, setSuccess] = useState(false); + + const { data: storages } = useQuery({ + queryKey: ["storages"], + queryFn: () => getStorages(), + }); + + const form = useForm({ + defaultValues: { + amount: 0, + bottling_date: "", + description: "", + expiry_date: "", + name: "", + price: "", + storage_location_uuid: "", + }, + onSubmit: async ({ value }) => { + setSuccess(false); + mutate(value); + }, + }); + + const { mutate, isPending } = useMutation({ + mutationFn: (values: ProductFormValues) => createProduct(values), + onSuccess: () => { + setSuccess(true); + }, + }); + + return ( + <> +