Skip to content

Commit 4866213

Browse files
authored
feat(dashboard,core,modules): free shipping promotion in dashboard (medusajs#13263)
* feat(dashboard,core,modules): free shipping promotion in dashboard * self-review * adapt for edit to work * changeset * integration tests * across for each * remove only from tests * remove console log * revert to across * update wording for shipping promotions * modify changeset * suggestion frane * fix i18n schema
1 parent 92e3b2b commit 4866213

File tree

22 files changed

+474
-331
lines changed

22 files changed

+474
-331
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@medusajs/link-modules": patch
3+
"@medusajs/promotion": patch
4+
"@medusajs/dashboard": patch
5+
"@medusajs/core-flows": patch
6+
"@medusajs/js-sdk": patch
7+
"@medusajs/types": patch
8+
---
9+
10+
feat(dashboard,core-flows,js-sdk,link-modules,promotion): free shipping promotion in dashboard

packages/admin/dashboard/src/hooks/api/promotions.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ export const promotionsQueryKeys = {
2121
ruleType: string,
2222
query?: HttpTypes.AdminGetPromotionRuleParams
2323
) => [PROMOTIONS_QUERY_KEY, id, ruleType, query],
24-
listRuleAttributes: (ruleType: string, promotionType?: string) => [
24+
listRuleAttributes: (
25+
ruleType: string,
26+
promotionType?: string,
27+
applicationMethodTargetType?: string
28+
) => [
2529
PROMOTIONS_QUERY_KEY,
2630
ruleType,
2731
promotionType,
32+
applicationMethodTargetType,
2833
],
2934
listRuleValues: (
3035
ruleType: string,
@@ -101,6 +106,7 @@ export const usePromotions = (
101106
export const usePromotionRuleAttributes = (
102107
ruleType: string,
103108
promotionType?: string,
109+
applicationMethodTargetType?: string,
104110
options?: Omit<
105111
UseQueryOptions<
106112
HttpTypes.AdminRuleAttributeOptionsListResponse,
@@ -112,9 +118,17 @@ export const usePromotionRuleAttributes = (
112118
>
113119
) => {
114120
const { data, ...rest } = useQuery({
115-
queryKey: promotionsQueryKeys.listRuleAttributes(ruleType, promotionType),
121+
queryKey: promotionsQueryKeys.listRuleAttributes(
122+
ruleType,
123+
promotionType,
124+
applicationMethodTargetType
125+
),
116126
queryFn: async () =>
117-
sdk.admin.promotion.listRuleAttributes(ruleType, promotionType),
127+
sdk.admin.promotion.listRuleAttributes(
128+
ruleType,
129+
promotionType,
130+
applicationMethodTargetType
131+
),
118132
...options,
119133
})
120134

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

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7516,14 +7516,47 @@
75167516
"target-rules": {
75177517
"type": "object",
75187518
"properties": {
7519-
"title": {
7520-
"type": "string"
7519+
"order": {
7520+
"type": "object",
7521+
"properties": {
7522+
"title": {
7523+
"type": "string"
7524+
},
7525+
"description": {
7526+
"type": "string"
7527+
}
7528+
},
7529+
"required": ["title", "description"],
7530+
"additionalProperties": false
75217531
},
7522-
"description": {
7523-
"type": "string"
7532+
"shipping_methods": {
7533+
"type": "object",
7534+
"properties": {
7535+
"title": {
7536+
"type": "string"
7537+
},
7538+
"description": {
7539+
"type": "string"
7540+
}
7541+
},
7542+
"required": ["title", "description"],
7543+
"additionalProperties": false
7544+
},
7545+
"items": {
7546+
"type": "object",
7547+
"properties": {
7548+
"title": {
7549+
"type": "string"
7550+
},
7551+
"description": {
7552+
"type": "string"
7553+
}
7554+
},
7555+
"required": ["title", "description"],
7556+
"additionalProperties": false
75247557
}
75257558
},
7526-
"required": ["title", "description"],
7559+
"required": ["order", "shipping_methods", "items"],
75277560
"additionalProperties": false
75287561
},
75297562
"buy-rules": {

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,8 +2010,18 @@
20102010
"description": "Which customer is allowed to use the promotion code? Promotion code can be used by all customers if left untouched."
20112011
},
20122012
"target-rules": {
2013-
"title": "What items will the promotion be applied to?",
2014-
"description": "The promotion will be applied to items that match the following conditions."
2013+
"order": {
2014+
"title": "What items will the promotion be applied to?",
2015+
"description": "The promotion will be applied to items that match the following conditions."
2016+
},
2017+
"shipping_methods": {
2018+
"title": "What shipping methods will the promotion be applied to?",
2019+
"description": "The promotion will be applied to shipping methods that match the following conditions."
2020+
},
2021+
"items": {
2022+
"title": "What items will the promotion be applied to?",
2023+
"description": "The promotion will be applied to items that match the following conditions."
2024+
}
20152025
},
20162026
"buy-rules": {
20172027
"title": "What needs to be in the cart to unlock the promotion?",

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ export const EditRulesForm = ({
2828
const [rulesToRemove, setRulesToRemove] = useState([])
2929

3030
const form = useForm<EditRulesType>({
31-
defaultValues: { rules: [], type: promotion.type },
31+
defaultValues: {
32+
rules: [],
33+
type: promotion.type,
34+
application_method: {
35+
target_type: promotion.application_method?.target_type,
36+
},
37+
},
3238
resolver: zodResolver(EditRules),
3339
})
3440

packages/admin/dashboard/src/routes/promotions/common/edit-rules/components/edit-rules-form/form-schema.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export const EditRules = z.object({
2424
field_type: z.string().optional(),
2525
})
2626
),
27+
application_method: z.object({
28+
target_type: z.enum(["order", "shipping_methods", "items"]),
29+
}),
2730
})
2831

2932
export type EditRulesType = z.infer<typeof EditRules>

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { HttpTypes } from "@medusajs/types"
1+
import {
2+
ApplicationMethodTargetTypeValues,
3+
HttpTypes,
4+
RuleTypeValues,
5+
} from "@medusajs/types"
26
import { Input } from "@medusajs/ui"
37
import { useWatch } from "react-hook-form"
48
import { Form } from "../../../../../../components/common/form"
59
import { Combobox } from "../../../../../../components/inputs/combobox"
6-
import { useStore } from "../../../../../../hooks/api/store"
10+
import { useStore } from "../../../../../../hooks/api"
711
import { useComboboxData } from "../../../../../../hooks/use-combobox-data"
812
import { sdk } from "../../../../../../lib/client"
913

@@ -18,7 +22,8 @@ type RuleValueFormFieldType = {
1822
operator: string
1923
fieldRule: any
2024
attributes: HttpTypes.AdminRuleAttributeOption[]
21-
ruleType: "rules" | "target-rules" | "buy-rules"
25+
ruleType: RuleTypeValues
26+
applicationMethodTargetType: ApplicationMethodTargetTypeValues | undefined
2227
}
2328

2429
const buildFilters = (attribute?: string, store?: HttpTypes.AdminStore) => {
@@ -44,6 +49,7 @@ export const RuleValueFormField = ({
4449
fieldRule,
4550
attributes,
4651
ruleType,
52+
applicationMethodTargetType,
4753
}: RuleValueFormFieldType) => {
4854
const attribute = attributes?.find(
4955
(attr) => attr.value === fieldRule.attribute
@@ -59,6 +65,7 @@ export const RuleValueFormField = ({
5965
{
6066
...params,
6167
...buildFilters(attribute?.id, store!),
68+
application_method_target_type: applicationMethodTargetType,
6269
}
6370
)
6471
},

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ export const RulesFormField = ({
4141
}: RulesFormFieldType) => {
4242
const { t } = useTranslation()
4343
const formData = form.getValues()
44-
const { attributes } = usePromotionRuleAttributes(ruleType, formData.type)
44+
const { attributes } = usePromotionRuleAttributes(
45+
ruleType,
46+
formData.type,
47+
formData.application_method?.target_type
48+
)
4549

4650
const { fields, append, remove, update, replace } = useFieldArray({
4751
control: form.control,
@@ -61,10 +65,17 @@ export const RulesFormField = ({
6165
defaultValue: promotion?.application_method?.type,
6266
})
6367

68+
const applicationMethodTargetType = useWatch({
69+
control: form.control,
70+
name: "application_method.target_type",
71+
defaultValue: promotion?.application_method?.target_type,
72+
})
73+
6474
const query: Record<string, string> = promotionType
6575
? {
6676
promotion_type: promotionType,
6777
application_method_type: applicationMethodType,
78+
application_method_target_type: applicationMethodTargetType,
6879
}
6980
: {}
7081

@@ -121,11 +132,19 @@ export const RulesFormField = ({
121132
return (
122133
<div className="flex flex-col">
123134
<Heading level="h2" className="mb-2">
124-
{t(`promotions.fields.conditions.${ruleType}.title`)}
135+
{t(
136+
ruleType === "target-rules"
137+
? `promotions.fields.conditions.${ruleType}.${applicationMethodTargetType}.title`
138+
: `promotions.fields.conditions.${ruleType}.title`
139+
)}
125140
</Heading>
126141

127142
<Text className="text-ui-fg-subtle txt-small mb-6">
128-
{t(`promotions.fields.conditions.${ruleType}.description`)}
143+
{t(
144+
ruleType === "target-rules"
145+
? `promotions.fields.conditions.${ruleType}.${applicationMethodTargetType}.description`
146+
: `promotions.fields.conditions.${ruleType}.description`
147+
)}
129148
</Text>
130149

131150
{fields.map((fieldRule, index) => {
@@ -307,6 +326,7 @@ export const RulesFormField = ({
307326
fieldRule={fieldRule}
308327
attributes={attributes}
309328
ruleType={ruleType}
329+
applicationMethodTargetType={applicationMethodTargetType}
310330
/>
311331
</div>
312332
</div>

0 commit comments

Comments
 (0)