import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { getProductDetails, getStorages } from "../utils/uxFncs"; import { CircularProgress, Typography, Select, Option, Input, Button, Chip, Divider, Box, Alert, } from "@mui/joy"; import { useTranslation } from "react-i18next"; import { useEffect, useState } from "react"; import { useForm } from "@tanstack/react-form"; import { mutateProduct } from "../utils/uxFncs"; import { toInputDate } from "../utils/uxFncs"; import type { ProductFormValues } from "../misc/interfaces"; import type { productDetailsInterface } from "../misc/interfaces"; interface ViewProductProps { uuid: string; } export const ViewProduct = (props: ViewProductProps) => { const uuid = props.uuid; const { t } = useTranslation(); const queryClient = useQueryClient(); const [success, setSuccess] = useState(false); const { data: productDetails, isLoading: productDetailsLoading, isSuccess, } = useQuery({ queryKey: ["product", uuid], queryFn: () => getProductDetails(uuid), }); 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 }) => { if (!productDetails?.uuid) { return; } mutate({ values: value, uuid: productDetails.uuid }); }, }); const { mutate, isPending } = useMutation({ mutationFn: ({ values, uuid, }: { values: ProductFormValues; uuid: string; }) => mutateProduct(values, uuid), onSuccess: (_data, variables) => { setSuccess(true); queryClient.invalidateQueries({ queryKey: ["product", variables.uuid] }); }, }); useEffect(() => { if (!productDetails) { return; } form.setFieldValue("amount", productDetails.amount ?? 0); form.setFieldValue( "bottling_date", toInputDate(productDetails.bottling_date), ); form.setFieldValue("description", productDetails.description ?? ""); form.setFieldValue("expiry_date", toInputDate(productDetails.expiry_date)); form.setFieldValue("name", productDetails.name ?? ""); form.setFieldValue("price", productDetails.price ?? ""); form.setFieldValue( "storage_location_uuid", productDetails.storage_location_uuid ?? "", ); }, [form, productDetails]); return ( <>
{t("product-details")} {t("inventory-header")}
{t("details")}
{productDetailsLoading && }
{isSuccess && (
{ e.preventDefault(); form.handleSubmit(); }} >
{t("product-name")} {(field) => ( field.handleChange(e.target.value)} onBlur={field.handleBlur} size="lg" variant="outlined" className="rounded-2xl bg-white/90 shadow-[0_10px_24px_rgba(15,23,42,0.08)]" /> )}
{t("description")} {(field) => ( field.handleChange(e.target.value)} onBlur={field.handleBlur} size="lg" variant="outlined" className="rounded-2xl bg-white/90 shadow-[0_10px_24px_rgba(15,23,42,0.08)]" /> )}
{t("expiry-date")} {(field) => ( field.handleChange(e.target.value)} onBlur={field.handleBlur} size="lg" variant="outlined" className="rounded-2xl bg-white/90" /> )}
{t("bottling-date")} {(field) => ( field.handleChange(e.target.value)} onBlur={field.handleBlur} size="lg" variant="outlined" className="rounded-2xl bg-white/90" /> )}
{t("inventory")}
{t("amount")} {(field) => ( { const nextValue = Number(e.target.value); field.handleChange( Number.isNaN(nextValue) ? 0 : nextValue, ); }} onBlur={field.handleBlur} className="rounded-2xl bg-white/80" /> )}
{t("price")} {(field) => ( field.handleChange(e.target.value)} onBlur={field.handleBlur} size="lg" variant="outlined" className="rounded-2xl bg-white/90" /> )} {t("currency")}
{t("storage-place")} {(field) => ( )}
{t("product-details")}
{success && (
{t("success")}
)}
)} ); };