Skip to content

Commit f53f027

Browse files
authored
fix(dashboard): rules form operator change (medusajs#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
1 parent c603540 commit f53f027

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

.changeset/orange-bats-sing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/dashboard": patch
3+
---
4+
5+
fix(dashboard): rules form operator change

packages/admin/dashboard/src/i18n/translations/$schema.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10818,6 +10818,12 @@
1081810818
},
1081910819
"loading": {
1082010820
"type": "string"
10821+
},
10822+
"selectValue": {
10823+
"type": "string"
10824+
},
10825+
"selectValues": {
10826+
"type": "string"
1082110827
}
1082210828
},
1082310829
"required": [
@@ -10830,7 +10836,9 @@
1083010836
"from",
1083110837
"to",
1083210838
"beaware",
10833-
"loading"
10839+
"loading",
10840+
"selectValue",
10841+
"selectValues"
1083410842
],
1083510843
"additionalProperties": false
1083610844
},

packages/admin/dashboard/src/i18n/translations/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2913,7 +2913,9 @@
29132913
"from": "From",
29142914
"to": "To",
29152915
"beaware": "Be aware",
2916-
"loading": "Loading"
2916+
"loading": "Loading",
2917+
"selectValue": "Select Value",
2918+
"selectValues": "Select Values"
29172919
},
29182920
"fields": {
29192921
"amount": "Amount",

packages/admin/dashboard/src/routes/promotions/common/edit-rules/components/rule-value-form-field/rule-value-form-field.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
} from "@medusajs/types"
66
import { Input } from "@medusajs/ui"
77
import { useWatch } from "react-hook-form"
8+
import { useTranslation } from "react-i18next"
9+
import { useEffect } from "react"
10+
811
import { Form } from "../../../../../../components/common/form"
912
import { Combobox } from "../../../../../../components/inputs/combobox"
1013
import { useStore } from "../../../../../../hooks/api"
@@ -51,6 +54,8 @@ export const RuleValueFormField = ({
5154
ruleType,
5255
applicationMethodTargetType,
5356
}: RuleValueFormFieldType) => {
57+
const { t } = useTranslation()
58+
5459
const attribute = attributes?.find(
5560
(attr) => attr.value === fieldRule.attribute
5661
)
@@ -82,6 +87,23 @@ export const RuleValueFormField = ({
8287
name: operator,
8388
})
8489

90+
useEffect(() => {
91+
const hasDirtyRules = Object.keys(form.formState.dirtyFields).length > 0
92+
93+
/**
94+
* Don't reset values if fileds didn't change - this is to prevent reset of form on initial render when editing an existing rule
95+
*/
96+
if (!hasDirtyRules) {
97+
return
98+
}
99+
100+
if (watchOperator === "eq") {
101+
form.setValue(name, "")
102+
} else {
103+
form.setValue(name, [])
104+
}
105+
}, [watchOperator])
106+
85107
return (
86108
<Form.Field
87109
key={`${identifier}.${scope}.${name}-${fieldRule.attribute}`}
@@ -126,11 +148,13 @@ export const RuleValueFormField = ({
126148
<Combobox
127149
{...field}
128150
{...comboboxData}
129-
multiple={watchOperator !== "eq"}
130151
ref={ref}
131152
placeholder={
132-
watchOperator === "eq" ? "Select Value" : "Select Values"
153+
watchOperator === "eq"
154+
? t("labels.selectValue")
155+
: t("labels.selectValues")
133156
}
157+
disabled={!watchOperator}
134158
onChange={onChange}
135159
/>
136160
</Form.Control>

0 commit comments

Comments
 (0)