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 { ConditionalTooltip } from "../../../../../components/common/conditional-tooltip/index.ts" import { Form } from "../../../../../components/common/form/index.ts" import { RouteDrawer, useRouteModal, } from "../../../../../components/modals/index.ts" import { KeyboundForm } from "../../../../../components/utilities/keybound-form/keybound-form.tsx" import { useUpdateCustomer } from "../../../../../hooks/api/customers.tsx" type EditCustomerFormProps = { customer: HttpTypes.AdminCustomer } const EditCustomerSchema = 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 EditCustomerForm = ({ customer }: EditCustomerFormProps) => { const { t } = useTranslation() const { handleSuccess } = useRouteModal() const form = useForm>({ defaultValues: { email: customer.email || "", first_name: customer.first_name || "", last_name: customer.last_name || "", company_name: customer.company_name || "", phone: customer.phone || "", }, resolver: zodResolver(EditCustomerSchema), }) const { mutateAsync, isPending } = useUpdateCustomer(customer.id) const handleSubmit = form.handleSubmit(async (data) => { await mutateAsync( { email: customer.has_account ? undefined : data.email, first_name: data.first_name || null, last_name: data.last_name || null, phone: data.phone || null, company_name: data.company_name || null, }, { onSuccess: ({ customer }) => { toast.success( t("customers.edit.successToast", { email: customer.email, }) ) handleSuccess() }, onError: (error) => { toast.error(error.message) }, } ) }) return (
{ return ( {t("fields.email")} ) }} /> { return ( {t("fields.firstName")} ) }} /> { return ( {t("fields.lastName")} ) }} /> { return ( {t("fields.company")} ) }} /> { return ( {t("fields.phone")} ) }} />
) }