import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { useEffect, useMemo } from "react"; import { FormLabel } from "~/shared/ui/form-label"; import { Input } from "~/shared/ui/input"; import { Button } from "~/shared/ui/button"; import { useRootStore } from "~/shared/stores/root"; import { BackButton } from "~/shared/ui/back-button"; const MIN_PRICE = 0.07; const MIN_RESALE_PRICE = 0.07; const RECOMMENDED_PRICE = 0.15; // const RECOMMENDED_RESALE_PRICE = 0.15; type PriceStepProps = { prevStep(): void; nextStep(): void; }; export const PriceStep = ({ nextStep, prevStep }: PriceStepProps) => { const rootStore = useRootStore(); const formSchema = useMemo(() => { const parsePrice = (value: unknown) => { if (typeof value === "string") { // Replace commas with dots and parse the value const parsedValue = parseFloat(value.replace(",", ".")); return isNaN(parsedValue) ? undefined : parsedValue; } return undefined; }; if (rootStore.allowResale) { return z.object({ price: z.preprocess( parsePrice, z.number().min(MIN_PRICE, `Цена должна быть минимум ${MIN_PRICE} TON.`) ), resaleLicensePrice: z .preprocess(parsePrice, z.number().min(MIN_RESALE_PRICE, `Цена копии должна быть минимум ${MIN_RESALE_PRICE} TON.`)) .optional(), }); } return z.object({ price: z.preprocess( parsePrice, z.number().min(MIN_PRICE, `Цена должна быть минимум ${MIN_PRICE} TON.`) ), }); }, [rootStore.allowResale]); type FormValues = z.infer; const form = useForm({ resolver: zodResolver(formSchema), mode: "onChange", defaultValues: { price: rootStore.price, //@ts-expect-error Fix typings resaleLicensePrice: rootStore?.licenseResalePrice, }, }); useEffect(() => { void form.trigger(); }, [rootStore.allowResale, form]); const handleSubmit = () => { form.handleSubmit(async (values: FormValues) => { try { rootStore.setPrice(values.price); //@ts-expect-error Fix typings if (values?.resaleLicensePrice) { //@ts-expect-error Fix typings rootStore.setLicenseResalePrice(values?.resaleLicensePrice); } nextStep(); } catch (error) { console.error("Error: ", error); } })(); }; return (
/Укажите цену
4/5

Минимальная стоимость {MIN_PRICE} TON.

Рекомендуемая стоимость {RECOMMENDED_PRICE} TON.

); };