import { Controller, useForm } from "react-hook-form" import { adminStoreKeys, useAdminUpdateCurrency } from "medusa-react" import { useTranslation } from "react-i18next" import CoinsIcon from "../../../../components/fundamentals/icons/coins-icon" import { Currency } from "@medusajs/medusa" import FeatureToggle from "../../../../components/fundamentals/feature-toggle" import Switch from "../../../../components/atoms/switch" import { getErrorMessage } from "../../../../utils/error-messages" import { useEffect } from "react" import useNotification from "../../../../hooks/use-notification" import { useQueryClient } from "@tanstack/react-query" type CurrencyTaxSettingFormType = { includes_tax: boolean } type Props = { currency: Currency isDefault: boolean } const CurrencyTaxSetting = ({ currency, isDefault }: Props) => { const { t } = useTranslation() const queryClient = useQueryClient() const { mutate } = useAdminUpdateCurrency(currency.code) const { handleSubmit, control, reset } = useForm({ defaultValues: { includes_tax: currency.includes_tax, }, }) const notification = useNotification() useEffect(() => { reset({ includes_tax: currency.includes_tax, }) }, [currency]) const onSubmit = handleSubmit((data: CurrencyTaxSettingFormType) => { mutate(data, { onSuccess: () => { notification( t("components-success", "Success"), t( "components-successfully-updated-currency", "Successfully updated currency" ), "success" ) // When we update a currency, we need to invalidate the store in order for this change to be reflected across admin queryClient.invalidateQueries(adminStoreKeys.all) }, onError: (error) => { notification( t("components-error", "Error"), getErrorMessage(error), "error" ) reset({ includes_tax: currency.includes_tax, }) }, }) }) return (

{currency.code.toUpperCase()}

{currency.name}

{isDefault && (

{t("components-default", "Default")}

)}
{ return ( { onChange(data) onSubmit() }} /> ) }} />
) } export default CurrencyTaxSetting