|
1
|
import { PromotionRuleDTO } from "@medusajs/types" |
|
2
|
import { Heading } from "@medusajs/ui" |
|
3
|
import { useTranslation } from "react-i18next" |
|
4
|
import { useParams } from "react-router-dom" |
|
5
|
import { RouteDrawer } from "../../../../components/modals" |
|
6
|
import { usePromotion } from "../../../../hooks/api/promotions" |
|
7
|
import { EditRulesWrapper } from "./components/edit-rules-wrapper" |
|
8
|
|
|
9
|
export enum RuleType { |
|
10
|
RULES = "rules", |
|
11
|
BUY_RULES = "buy-rules", |
|
12
|
TARGET_RULES = "target-rules", |
|
13
|
} |
|
14
|
export type RuleTypeValues = "rules" | "buy-rules" | "target-rules" |
|
15
|
|
|
16
|
export const EditRules = () => { |
|
17
|
const params = useParams() |
|
18
|
const allowedParams: string[] = [ |
|
19
|
RuleType.RULES, |
|
20
|
RuleType.BUY_RULES, |
|
21
|
RuleType.TARGET_RULES, |
|
22
|
] |
|
23
|
|
|
24
|
if (!allowedParams.includes(params.ruleType!)) { |
|
25
|
throw new Error("invalid page") |
|
26
|
} |
|
27
|
|
|
28
|
const { t } = useTranslation() |
|
29
|
const ruleType = params.ruleType as RuleTypeValues |
|
30
|
const id = params.id as string |
|
31
|
const rules: PromotionRuleDTO[] = [] |
|
32
|
const { promotion, isPending: isLoading, isError, error } = usePromotion(id) |
|
33
|
|
|
34
|
if (promotion) { |
|
35
|
if (ruleType === RuleType.RULES) { |
|
36
|
rules.push(...(promotion.rules || [])) |
|
37
|
} else if (ruleType === RuleType.TARGET_RULES) { |
|
38
|
rules.push(...(promotion?.application_method?.target_rules || [])) |
|
39
|
} else if (ruleType === RuleType.BUY_RULES) { |
|
40
|
rules.push(...(promotion.application_method?.buy_rules || [])) |
|
41
|
} |
|
42
|
} |
|
43
|
|
|
44
|
if (isError) { |
|
45
|
throw error |
|
46
|
} |
|
47
|
|
|
48
|
return ( |
|
49
|
<RouteDrawer> |
|
50
|
<RouteDrawer.Header> |
|
51
|
<Heading>{t(`promotions.edit.${ruleType}.title`)}</Heading> |
|
52
|
</RouteDrawer.Header> |
|
53
|
|
|
54
|
{!isLoading && promotion && ( |
|
55
|
<EditRulesWrapper |
|
56
|
promotion={promotion} |
|
57
|
rules={rules} |
|
58
|
ruleType={ruleType} |
|
59
|
/> |
|
60
|
)} |
|
61
|
</RouteDrawer> |
|
62
|
) |
|
63
|
} |