import { zodResolver } from "@hookform/resolvers/zod" import { HttpTypes } from "@medusajs/types" import { Button, Input, 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 { RouteDrawer, useRouteModal } from "../../../../../components/modals" import { KeyboundForm } from "../../../../../components/utilities/keybound-form" import { useUpdateStockLocation } from "../../../../../hooks/api/stock-locations" import { CountrySelect } from "../../../../../components/inputs/country-select/country-select" type EditLocationFormProps = { location: HttpTypes.AdminStockLocation } const EditLocationSchema = 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(), // TODO: Add validation }), }) export const EditLocationForm = ({ location }: EditLocationFormProps) => { const { t } = useTranslation() const { handleSuccess } = useRouteModal() const form = useForm>({ defaultValues: { name: location.name, address: { address_1: location.address?.address_1 || "", address_2: location.address?.address_2 || "", city: location.address?.city || "", company: location.address?.company || "", country_code: location.address?.country_code || "", phone: location.address?.phone || "", postal_code: location.address?.postal_code || "", province: location.address?.province || "", }, }, resolver: zodResolver(EditLocationSchema), }) const { mutateAsync, isPending } = useUpdateStockLocation(location.id) const handleSubmit = form.handleSubmit(async (values) => { const { name, address } = values await mutateAsync( { name: name, address: address, }, { onSuccess: () => { toast.success(t("stockLocations.edit.successToast", { name: name })) handleSuccess() }, onError: (e) => { toast.error(e.message) }, } ) }) return (
{ 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")} ) }} />
) }