import { useAdminCreateProduct, useAdminProducts, useAdminStore, } from "medusa-react" import { Controller, useFieldArray, useForm, useWatch } from "react-hook-form" import { useNavigate } from "react-router-dom" import { useTranslation } from "react-i18next" import FileUploadField from "../../components/atoms/file-upload-field" import Button from "../../components/fundamentals/button" import PlusIcon from "../../components/fundamentals/icons/plus-icon" import TrashIcon from "../../components/fundamentals/icons/trash-icon" import InputField from "../../components/molecules/input" import Modal from "../../components/molecules/modal" import TextArea from "../../components/molecules/textarea" import CurrencyInput from "../../components/organisms/currency-input" import useNotification from "../../hooks/use-notification" import Medusa from "../../services/api" import { ProductStatus } from "../../types/shared" import { getErrorMessage } from "../../utils/error-messages" import { focusByName } from "../../utils/focus-by-name" type NewGiftCardProps = { onClose: () => void } type NewGiftCardFormData = { title: string description: string | undefined thumbnail: { url: string name: string size: number nativeFile: File } | null denominations: { amount: number | null }[] } const NewGiftCard = ({ onClose }: NewGiftCardProps) => { const { store } = useAdminStore() const { refetch } = useAdminProducts() const { mutate, isLoading } = useAdminCreateProduct() const navigate = useNavigate() const notification = useNotification() const { t } = useTranslation() const { register, setValue, handleSubmit, control } = useForm({ shouldUnregister: true, }) const { fields, append, remove } = useFieldArray({ control, name: "denominations", }) const handleFileUpload = (files: File[]) => { const file = files[0] const url = URL.createObjectURL(file) setValue("thumbnail", { url, name: file.name, size: file.size, nativeFile: file, }) } const thumbnail = useWatch({ control, name: "thumbnail", }) const onSubmit = async (data: NewGiftCardFormData) => { const trimmedName = data.title.trim() if (!trimmedName) { notification( t("gift-cards-error", "Error"), t( "gift-cards-please-enter-a-name-for-the-gift-card", "Please enter a name for the Gift Card" ), "error" ) focusByName("name") return } if (!data.denominations?.length) { notification( t("gift-cards-error", "Error"), t( "gift-cards-please-add-at-least-one-denomination", "Please add at least one denomination" ), "error" ) focusByName("add-denomination") return } let images: string[] = [] if (thumbnail) { const uploadedImgs = await Medusa.uploads .create([thumbnail.nativeFile]) .then(({ data }) => { const uploaded = data.uploads.map(({ url }) => url) return uploaded }) images = uploadedImgs } mutate( { is_giftcard: true, title: data.title, description: data.description, discountable: false, options: [{ title: t("gift-cards-denominations", "Denominations") }], variants: data.denominations.map((d, i) => ({ title: `${i + 1}`, inventory_quantity: 0, manage_inventory: false, prices: [ { amount: d.amount!, currency_code: store?.default_currency_code }, ], options: [{ value: `${d.amount}` }], })), images: images.length ? images : undefined, thumbnail: images.length ? images[0] : undefined, status: ProductStatus.PUBLISHED, }, { onSuccess: () => { notification( t("gift-cards-success", "Success"), t( "gift-cards-successfully-created-gift-card", "Successfully created Gift Card" ), "success" ) refetch() navigate("/a/gift-cards/manage") }, onError: (err) => { notification( t("gift-cards-error", "Error"), getErrorMessage(err), "error" ) }, } ) } return (
console.log(err))}>

{t("gift-cards-create-gift-card", "Create Gift Card")}

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