From f53f027ce68b0518968482dccafeadd68cc55433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Fri, 29 Aug 2025 17:53:00 +0200 Subject: [PATCH] fix(dashboard): rules form operator change (#13324) **What** - fix condition rules form sometimes rendering multi and single selects unrelated to the attribute operator selected - reset rule value when operator is changed - disable selecting rule values untill operator is selected - translate labels --- .changeset/orange-bats-sing.md | 5 ++++ .../src/i18n/translations/$schema.json | 10 ++++++- .../dashboard/src/i18n/translations/en.json | 4 ++- .../rule-value-form-field.tsx | 28 +++++++++++++++++-- 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 .changeset/orange-bats-sing.md diff --git a/.changeset/orange-bats-sing.md b/.changeset/orange-bats-sing.md new file mode 100644 index 0000000000..ee4c575f96 --- /dev/null +++ b/.changeset/orange-bats-sing.md @@ -0,0 +1,5 @@ +--- +"@medusajs/dashboard": patch +--- + +fix(dashboard): rules form operator change diff --git a/packages/admin/dashboard/src/i18n/translations/$schema.json b/packages/admin/dashboard/src/i18n/translations/$schema.json index cf25cca9b3..3b8d241fa5 100644 --- a/packages/admin/dashboard/src/i18n/translations/$schema.json +++ b/packages/admin/dashboard/src/i18n/translations/$schema.json @@ -10818,6 +10818,12 @@ }, "loading": { "type": "string" + }, + "selectValue": { + "type": "string" + }, + "selectValues": { + "type": "string" } }, "required": [ @@ -10830,7 +10836,9 @@ "from", "to", "beaware", - "loading" + "loading", + "selectValue", + "selectValues" ], "additionalProperties": false }, diff --git a/packages/admin/dashboard/src/i18n/translations/en.json b/packages/admin/dashboard/src/i18n/translations/en.json index 07f7791bff..2793d92a2c 100644 --- a/packages/admin/dashboard/src/i18n/translations/en.json +++ b/packages/admin/dashboard/src/i18n/translations/en.json @@ -2913,7 +2913,9 @@ "from": "From", "to": "To", "beaware": "Be aware", - "loading": "Loading" + "loading": "Loading", + "selectValue": "Select Value", + "selectValues": "Select Values" }, "fields": { "amount": "Amount", diff --git a/packages/admin/dashboard/src/routes/promotions/common/edit-rules/components/rule-value-form-field/rule-value-form-field.tsx b/packages/admin/dashboard/src/routes/promotions/common/edit-rules/components/rule-value-form-field/rule-value-form-field.tsx index 806ca68071..a0fcc43db1 100644 --- a/packages/admin/dashboard/src/routes/promotions/common/edit-rules/components/rule-value-form-field/rule-value-form-field.tsx +++ b/packages/admin/dashboard/src/routes/promotions/common/edit-rules/components/rule-value-form-field/rule-value-form-field.tsx @@ -5,6 +5,9 @@ import { } from "@medusajs/types" import { Input } from "@medusajs/ui" import { useWatch } from "react-hook-form" +import { useTranslation } from "react-i18next" +import { useEffect } from "react" + import { Form } from "../../../../../../components/common/form" import { Combobox } from "../../../../../../components/inputs/combobox" import { useStore } from "../../../../../../hooks/api" @@ -51,6 +54,8 @@ export const RuleValueFormField = ({ ruleType, applicationMethodTargetType, }: RuleValueFormFieldType) => { + const { t } = useTranslation() + const attribute = attributes?.find( (attr) => attr.value === fieldRule.attribute ) @@ -82,6 +87,23 @@ export const RuleValueFormField = ({ name: operator, }) + useEffect(() => { + const hasDirtyRules = Object.keys(form.formState.dirtyFields).length > 0 + + /** + * Don't reset values if fileds didn't change - this is to prevent reset of form on initial render when editing an existing rule + */ + if (!hasDirtyRules) { + return + } + + if (watchOperator === "eq") { + form.setValue(name, "") + } else { + form.setValue(name, []) + } + }, [watchOperator]) + return (