import { CustomerGroup } from "@medusajs/medusa" import { useAdminCreateCustomerGroup, useAdminUpdateCustomerGroup, } from "medusa-react" import { useEffect } from "react" import { useForm } from "react-hook-form" import { CustomerGroupGeneralForm, CustomerGroupGeneralFormType, } from "../../../components/forms/customer-group/customer-group-general-form" import MetadataForm, { getMetadataFormValues, getSubmittableMetadata, MetadataFormType, } from "../../../components/forms/general/metadata-form" import Button from "../../../components/fundamentals/button" import Modal from "../../../components/molecules/modal" import useNotification from "../../../hooks/use-notification" import { getErrorMessage } from "../../../utils/error-messages" import { nestedForm } from "../../../utils/nested-form" import { useTranslation } from "react-i18next" type CustomerGroupModalProps = { open: boolean customerGroup?: CustomerGroup onClose: () => void } type CustomerGroupModalFormType = { general: CustomerGroupGeneralFormType metadata: MetadataFormType } /* * A modal for crating/editing customer groups. */ function CustomerGroupModal({ customerGroup, onClose, open, }: CustomerGroupModalProps) { const { t } = useTranslation() const form = useForm({ defaultValues: getDefaultValues(customerGroup), }) const { mutate: update, isLoading: isUpdating } = useAdminUpdateCustomerGroup( customerGroup?.id! ) const { mutate: create, isLoading: isCreating } = useAdminCreateCustomerGroup() const notification = useNotification() const { reset, handleSubmit: handleFormSubmit } = form useEffect(() => { if (open) { reset(getDefaultValues(customerGroup)) } }, [customerGroup, open, reset]) const onSubmit = handleFormSubmit((data) => { const { general, metadata } = data const onSuccess = () => { const title = customerGroup ? t("groups-group-updated", "Group Updated") : t("groups-group-created", "Group Created") const msg = customerGroup ? t( "groups-the-customer-group-has-been-updated", "The customer group has been updated" ) : t( "groups-the-customer-group-has-been-created", "The customer group has been created" ) notification(title, msg, "success") onClose() } const onError = (err: Error) => { notification("Error", getErrorMessage(err), "error") } if (customerGroup) { update( { name: general.name, metadata: getSubmittableMetadata(metadata), }, { onSuccess, onError, } ) } else { create( { name: general.name, metadata: getSubmittableMetadata(metadata), }, { onSuccess, onError, } ) } onClose() }) return ( {customerGroup ? t("groups-edit-customer-group", "Edit Customer Group") : t( "groups-create-a-new-customer-group", "Create a New Customer Group" )}

{t("groups-details", "Details")}

{t("groups-metadata", "Metadata")}

) } const getDefaultValues = ( initialData?: CustomerGroup ): CustomerGroupModalFormType | undefined => { if (!initialData) { return undefined } return { general: { name: initialData.name, }, metadata: getMetadataFormValues(initialData.metadata), } } export default CustomerGroupModal