import { Discount } from "@medusajs/medusa" import { useAdminDeleteDiscount, useAdminUpdateDiscount } from "medusa-react" import React from "react" import { useNavigate } from "react-router-dom" import { useTranslation } from "react-i18next" import Badge from "../../../../components/fundamentals/badge" import EditIcon from "../../../../components/fundamentals/icons/edit-icon" import TrashIcon from "../../../../components/fundamentals/icons/trash-icon" import { ActionType } from "../../../../components/molecules/actionables" import StatusSelector from "../../../../components/molecules/status-selector" import BodyCard from "../../../../components/organisms/body-card" import useImperativeDialog from "../../../../hooks/use-imperative-dialog" import useNotification from "../../../../hooks/use-notification" import useToggleState from "../../../../hooks/use-toggle-state" import { getErrorMessage } from "../../../../utils/error-messages" import { formatAmountWithSymbol } from "../../../../utils/prices" import EditGeneral from "./edit-general" import { TFunction } from "i18next" type GeneralProps = { discount: Discount } const General: React.FC = ({ discount }) => { const { t } = useTranslation() const dialog = useImperativeDialog() const navigate = useNavigate() const notification = useNotification() const updateDiscount = useAdminUpdateDiscount(discount.id) const deletediscount = useAdminDeleteDiscount(discount.id) const onDelete = async () => { const shouldDelete = await dialog({ heading: t("general-delete-promotion", "Delete Promotion"), text: t( "general-confirm-delete-promotion", "Are you sure you want to delete this promotion?" ), }) if (shouldDelete) { deletediscount.mutate(undefined, { onSuccess: () => { notification( t("general-success", "Success"), t( "general-promotion-deleted-successfully", "Promotion deleted successfully" ), "success" ) navigate("/a/discounts/") }, onError: (err) => { notification( t("general-error", "Error"), getErrorMessage(err), "error" ) }, }) } } const onStatusChange = async () => { updateDiscount.mutate( { is_disabled: !discount.is_disabled, }, { onSuccess: ({ discount: { is_disabled } }) => { notification( t("general-success", "Success"), !is_disabled ? t( "general-discount-published-successfully", "Discount published successfully" ) : t( "general-discount-drafted-successfully", "Discount drafted successfully" ), "success" ) }, onError: (err) => { notification( t("general-error", "Error"), getErrorMessage(err), "error" ) }, } ) } const { state, open, close } = useToggleState() const actionables: ActionType[] = [ { label: t("general-edit-general-information", "Edit general information"), onClick: open, icon: , }, { label: t("general-delete-discount", "Delete discount"), onClick: onDelete, variant: "danger", icon: , }, ] return ( <> {discount.is_dynamic && ( {t("general-template-discount", "Template discount")} )} } >
{getPromotionDescription(discount, t)} {t("general-discount-amount", "Discount Amount")}

{discount.regions.length.toLocaleString("en-US")}

{t("general-valid-regions", "Valid Regions")}

{discount.usage_count.toLocaleString("en-US")}

{t("general-total-redemptions", "Total Redemptions")}
) } const getPromotionDescription = (discount: Discount, t: TFunction) => { switch (discount.rule.type) { case "fixed": return (

{formatAmountWithSymbol({ currency: discount.regions[0].currency_code, amount: discount.rule.value, })}

{discount.regions[0].currency_code.toUpperCase()}
) case "percentage": return (

{discount.rule.value}

%
) case "free_shipping": return (

{t("general-free-shipping", "FREE SHIPPING")}

) default: return t("general-unknown-discount-type", "Unknown discount type") } } export default General