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 { useCreateCustomerGroup } from "../../../../../hooks/api/customer-groups" export const CreateCustomerGroupSchema = zod.object({ name: zod.string().min(1), }) export const CreateCustomerGroupForm = () => { const { t } = useTranslation() const { handleSuccess } = useRouteModal() const form = useForm>({ defaultValues: { name: "", }, resolver: zodResolver(CreateCustomerGroupSchema), }) const { mutateAsync, isPending } = useCreateCustomerGroup() const handleSubmit = form.handleSubmit(async (data) => { await mutateAsync( { name: data.name, }, { onSuccess: ({ customer_group }) => { toast.success( t("customerGroups.create.successToast", { name: customer_group.name, }) ) handleSuccess(`/customer-groups/${customer_group.id}`) }, onError: (error) => { toast.error(error.message) }, } ) }) return (
{t("customerGroups.create.header")} {t("customerGroups.create.hint")}
{ return ( {t("fields.name")} ) }} />
) }