chore(promotion): toast error messages on promotion errors (#7897)

This commit is contained in:
Riqwan Thamir
2024-07-01 20:52:39 +02:00
committed by GitHub
parent 17e6a83b59
commit b25c6ab54f
2 changed files with 45 additions and 13 deletions

View File

@@ -1137,7 +1137,11 @@
"campaignType": "Select the currency code in the promotion tab to enable it."
},
"errors": {
"requiredField": "Required field"
"requiredField": "Required field",
"promotionTabError": "Fix errors in Promotion Tab before proceeding"
},
"toasts": {
"promotionCreateSuccess": "Promotion ({{code}}) was successfully created."
},
"create": {},
"edit": {

View File

@@ -10,6 +10,7 @@ import {
ProgressTabs,
RadioGroup,
Text,
toast,
} from "@medusajs/ui"
import { useEffect, useMemo, useState } from "react"
import { useForm, useWatch } from "react-hook-form"
@@ -121,21 +122,48 @@ export const CreatePromotionForm = () => {
}))
}
createPromotion({
...promotionData,
rules: buildRulesData(rules),
application_method: {
...applicationMethodData,
...applicationMethodRuleData,
target_rules: buildRulesData(targetRulesData),
buy_rules: buildRulesData(buyRulesData),
createPromotion(
{
...promotionData,
rules: buildRulesData(rules),
application_method: {
...applicationMethodData,
...applicationMethodRuleData,
target_rules: buildRulesData(targetRulesData),
buy_rules: buildRulesData(buyRulesData),
},
is_automatic: is_automatic === "true",
},
is_automatic: is_automatic === "true",
}).then(() => handleSuccess())
{
onSuccess: ({ promotion }) => {
toast.success(t("general.success"), {
description: t("promotions.toasts.promotionCreateSuccess", {
code: promotion.code,
}),
dismissLabel: t("actions.close"),
})
handleSuccess()
},
onError: (e) => {
toast.error(t("general.error"), {
description: e.message,
dismissLabel: t("actions.close"),
})
},
}
)
},
async (error) => {
// TODO: showcase error when something goes wrong
// Wait for alert component and use it here
const { campaign, ...rest } = error || {}
const errorInPromotionTab = !!Object.keys(rest || {}).length
if (errorInPromotionTab) {
toast.error(t("general.error"), {
description: t("promotions.errors.promotionTabError"),
dismissLabel: t("general.close"),
})
}
}
)