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 { RouteFocusModal, useRouteModal, } from "../../../../../components/modals" import { KeyboundForm } from "../../../../../components/utilities/keybound-form" import { useCreateCustomer } from "../../../../../hooks/api/customers" const CreateCustomerSchema = zod.object({ email: zod.string().email(), first_name: zod.string().optional(), last_name: zod.string().optional(), company_name: zod.string().optional(), phone: zod.string().optional(), }) export const CreateCustomerForm = () => { const { t } = useTranslation() const { handleSuccess } = useRouteModal() const { mutateAsync, isPending } = useCreateCustomer() const form = useForm>({ defaultValues: { email: "", first_name: "", last_name: "", phone: "", company_name: "", }, resolver: zodResolver(CreateCustomerSchema), }) const handleSubmit = form.handleSubmit(async (data) => { await mutateAsync( { email: data.email, first_name: data.first_name || undefined, last_name: data.last_name || undefined, company_name: data.company_name || undefined, phone: data.phone || undefined, }, { onSuccess: ({ customer }) => { toast.success( t("customers.create.successToast", { email: customer.email, }) ) handleSuccess(`/customers/${customer.id}`) }, onError: (error) => { toast.error(error.message) }, } ) }) return (
{t("customers.create.header")} {t("customers.create.hint")}
{ return ( {t("fields.firstName")} ) }} /> { return ( {t("fields.lastName")} ) }} /> { return ( {t("fields.email")} ) }} /> { return ( {t("fields.company")} ) }} /> { return ( {t("fields.phone")} ) }} />
) }