import { useAdminCreateGiftCard } from "medusa-react" import React, { useEffect } from "react" import { useForm, useWatch } from "react-hook-form" import { useTranslation } from "react-i18next" import GiftCardBalanceForm, { GiftCardBalanceFormType, } from "../../components/forms/gift-card/gift-card-balance-form" import GiftCardEndsAtForm, { GiftCardEndsAtFormType, } from "../../components/forms/gift-card/gift-card-ends-at-form" import GiftCardReceiverForm, { GiftCardReceiverFormType, } from "../../components/forms/gift-card/gift-card-receiver-form" import GiftCardRegionForm, { GiftCardRegionFormType, } from "../../components/forms/gift-card/gift-card-region-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" type CustomGiftcardProps = { onClose: () => void open: boolean } type CustomGiftCardFormType = { region: GiftCardRegionFormType ends_at: GiftCardEndsAtFormType balance: GiftCardBalanceFormType receiver: GiftCardReceiverFormType } const CustomGiftcard: React.FC = ({ onClose, open }) => { const { t } = useTranslation() const form = useForm() const { handleSubmit, reset, control, formState: { isDirty }, } = form const currencySubscriber = useWatch({ control, name: "region.region_id.currency_code", defaultValue: "usd", }) const notification = useNotification() const { mutate, isLoading: isSubmitting } = useAdminCreateGiftCard() useEffect(() => { if (open) { reset() } }, [open, reset]) const onSubmit = handleSubmit((data) => { mutate( { region_id: data.region.region_id.value, value: data.balance.amount, ends_at: data.ends_at.ends_at || undefined, metadata: { email: data.receiver.email, personal_message: data.receiver.message, }, }, { onSuccess: () => { notification( t("gift-cards-created-gift-card", "Created gift card"), t( "gift-cards-custom-gift-card-was-created-successfully", "Custom gift card was created successfully" ), "success" ) onClose() }, onError: (error) => { notification( t("gift-cards-error", "Error"), getErrorMessage(error), "error" ) }, } ) }) return (

{t("gift-cards-custom-gift-card", "Custom Gift Card")}

{t("gift-cards-details", "Details")}

{t("gift-cards-receiver", "Receiver")}

) } export default CustomGiftcard