import { zodResolver } from "@hookform/resolvers/zod" import { Button, Heading, Input, Text, toast } from "@medusajs/ui" import { useForm } from "react-hook-form" import { useTranslation } from "react-i18next" import * as zod from "zod" import { Form } from "../../../../../components/common/form" import { CountrySelect } from "../../../../../components/inputs/country-select" import { RouteFocusModal, useRouteModal, } from "../../../../../components/modals" import { KeyboundForm } from "../../../../../components/utilities/keybound-form" import { useCreateStockLocation } from "../../../../../hooks/api/stock-locations" const CreateLocationSchema = zod.object({ name: zod.string().min(1), address: zod.object({ address_1: zod.string().min(1), address_2: zod.string().optional(), country_code: zod.string().min(2).max(2), city: zod.string().optional(), postal_code: zod.string().optional(), province: zod.string().optional(), company: zod.string().optional(), phone: zod.string().optional(), }), }) export const CreateLocationForm = () => { const { t } = useTranslation() const { handleSuccess } = useRouteModal() const form = useForm>({ defaultValues: { name: "", address: { address_1: "", address_2: "", city: "", company: "", country_code: "", phone: "", postal_code: "", province: "", }, }, resolver: zodResolver(CreateLocationSchema), }) const { mutateAsync, isPending } = useCreateStockLocation() const handleSubmit = form.handleSubmit(async (values) => { await mutateAsync( { name: values.name, address: values.address, }, { onSuccess: ({ stock_location }) => { toast.success(t("locations.toast.create")) handleSuccess(`/settings/locations/${stock_location.id}`) }, onError: (e) => { toast.error(e.message) }, } ) }) return (
{t("stockLocations.create.header")} {t("stockLocations.create.hint")}
{ return ( {t("fields.name")} ) }} />
{ return ( {t("fields.address")} ) }} /> { return ( {t("fields.address2")} ) }} /> { return ( {t("fields.postalCode")} ) }} /> { return ( {t("fields.city")} ) }} /> { return ( {t("fields.country")} ) }} /> { return ( {t("fields.state")} ) }} /> { return ( {t("fields.company")} ) }} /> { return ( {t("fields.phone")} ) }} />
) }