import { Product } from "@medusajs/medusa" import { useEffect } from "react" import { useForm } from "react-hook-form" import { useTranslation } from "react-i18next" import useEditProductActions from "../../../hooks/use-edit-product-actions" import { countries } from "../../../utils/countries" import { nestedForm } from "../../../utils/nested-form" import CustomsForm, { CustomsFormType } from "../../forms/product/customs-form" import DimensionsForm, { DimensionsFormType, } from "../../forms/product/dimensions-form" import Button from "../../fundamentals/button" import Modal from "../../molecules/modal" type Props = { product: Product open: boolean onClose: () => void } type AttributesForm = { dimensions: DimensionsFormType customs: CustomsFormType } const AttributeModal = ({ product, open, onClose }: Props) => { const { t } = useTranslation() const { onUpdate, updating } = useEditProductActions(product.id) const form = useForm({ defaultValues: getDefaultValues(product), }) const { formState: { isDirty }, handleSubmit, reset, } = form useEffect(() => { reset(getDefaultValues(product)) }, [product]) const onReset = () => { reset(getDefaultValues(product)) onClose() } const onSubmit = handleSubmit((data) => { onUpdate( { // @ts-ignore weight: data.dimensions.weight, // @ts-ignore width: data.dimensions.width, // @ts-ignore height: data.dimensions.height, // @ts-ignore length: data.dimensions.length, // @ts-ignore mid_code: data.customs.mid_code, // @ts-ignore hs_code: data.customs.hs_code, origin_country: data.customs.origin_country?.value, }, onReset ) }) return (

{t("product-attributes-section-edit-attributes", "Edit Attributes")}

{t("product-attributes-section-dimensions", "Dimensions")}

{t( "product-attributes-section-configure-to-calculate-the-most-accurate-shipping-rates", "Configure to calculate the most accurate shipping rates" )}

{t("product-attributes-section-customs", "Customs")}

{t( "product-attributes-section-configure-to-calculate-the-most-accurate-shipping-rates", "Configure to calculate the most accurate shipping rates" )}

) } const getDefaultValues = (product: Product): AttributesForm => { const country = countries.find( (country) => country.alpha2 === product.origin_country ) const countryOption = country ? { label: country.name, value: country.alpha2 } : null return { dimensions: { weight: product.weight, width: product.width, height: product.height, length: product.length, }, customs: { mid_code: product.mid_code, hs_code: product.hs_code, origin_country: countryOption, }, } } export default AttributeModal