Skip to content

Commit 2f3d805

Browse files
committed
isCustomerSourceCondition
1 parent c875d18 commit 2f3d805

File tree

2 files changed

+75
-49
lines changed

2 files changed

+75
-49
lines changed

apps/web/lib/zod/schemas/rewards.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export const REWARD_CONDITIONS: Record<
113113
id: "submitted",
114114
label: "qualified lead",
115115
},
116+
{
117+
id: "trial",
118+
label: "free trial",
119+
},
116120
],
117121
},
118122
],
@@ -206,9 +210,6 @@ export const NUMBER_CONDITION_OPERATORS: (typeof CONDITION_OPERATORS)[number][]
206210
"less_than_or_equal",
207211
];
208212

209-
export const SOURCE_CONDITION_OPERATORS: (typeof CONDITION_OPERATORS)[number][] =
210-
["equals_to", "not_equals"];
211-
212213
export const CONDITION_OPERATOR_LABELS = {
213214
equals_to: "is",
214215
not_equals: "is not",
@@ -337,7 +338,7 @@ export const REWARD_EVENT_COLUMN_MAPPING = Object.freeze({
337338
sale: "saleRewardId",
338339
});
339340

340-
export const CUSTOMER_SOURCES = ["tracked", "submitted"] as const;
341+
export const CUSTOMER_SOURCES = ["tracked", "submitted", "trial"] as const;
341342

342343
export const rewardContextSchema = z.object({
343344
customer: z

apps/web/ui/partners/rewards/rewards-logic.tsx

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
NUMBER_CONDITION_OPERATORS,
1212
REWARD_CONDITIONS,
1313
RewardConditionEntityAttribute,
14-
SOURCE_CONDITION_OPERATORS,
1514
STRING_CONDITION_OPERATORS,
1615
} from "@/lib/zod/schemas/rewards";
1716
import { X } from "@/ui/shared/icons";
@@ -303,6 +302,31 @@ function ConditionLogic({
303302

304303
const [displayProductLabel, setDisplayProductLabel] = useState(false);
305304

305+
// Auto-set operator to "equals_to" for customer.source
306+
const isCustomerSourceCondition =
307+
condition.entity === "customer" && condition.attribute === "source";
308+
309+
useEffect(() => {
310+
if (isCustomerSourceCondition && condition.operator !== "equals_to") {
311+
setValue(
312+
conditionKey,
313+
{
314+
...condition,
315+
operator: "equals_to",
316+
},
317+
{
318+
shouldDirty: true,
319+
},
320+
);
321+
}
322+
}, [
323+
isCustomerSourceCondition,
324+
condition.operator,
325+
condition,
326+
conditionKey,
327+
setValue,
328+
]);
329+
306330
return (
307331
<div className="flex w-full flex-col">
308332
<div className="flex items-center justify-between p-2.5">
@@ -369,53 +393,54 @@ function ConditionLogic({
369393
}))}
370394
/>
371395
</InlineBadgePopover>{" "}
372-
<InlineBadgePopover
373-
text={
374-
condition.operator
375-
? CONDITION_OPERATOR_LABELS[condition.operator]
376-
: "Condition"
377-
}
378-
invalid={!condition.operator}
379-
>
380-
<InlineBadgePopoverMenu
381-
selectedValue={condition.operator}
382-
onSelect={(value) =>
383-
setValue(
384-
conditionKey,
385-
{
386-
...condition,
387-
operator:
388-
value as (typeof CONDITION_OPERATORS)[number],
389-
// Update value to array / string / number if needed
390-
...(["in", "not_in"].includes(value)
391-
? !Array.isArray(condition.value)
392-
? { value: [] }
393-
: null
394-
: ["number", "currency"].includes(attributeType)
395-
? typeof condition.value !== "number"
396-
? { value: "" }
397-
: null
398-
: typeof condition.value !== "string"
399-
? { value: "" }
400-
: null),
401-
},
402-
{
403-
shouldDirty: true,
404-
},
405-
)
396+
{isCustomerSourceCondition ? (
397+
<span className="text-content-emphasis font-medium">is </span>
398+
) : (
399+
<InlineBadgePopover
400+
text={
401+
condition.operator
402+
? CONDITION_OPERATOR_LABELS[condition.operator]
403+
: "Condition"
406404
}
407-
items={(condition.entity === "customer" &&
408-
condition.attribute === "source"
409-
? SOURCE_CONDITION_OPERATORS
410-
: ["number", "currency"].includes(attributeType)
405+
invalid={!condition.operator}
406+
>
407+
<InlineBadgePopoverMenu
408+
selectedValue={condition.operator}
409+
onSelect={(value) =>
410+
setValue(
411+
conditionKey,
412+
{
413+
...condition,
414+
operator:
415+
value as (typeof CONDITION_OPERATORS)[number],
416+
// Update value to array / string / number if needed
417+
...(["in", "not_in"].includes(value)
418+
? !Array.isArray(condition.value)
419+
? { value: [] }
420+
: null
421+
: ["number", "currency"].includes(attributeType)
422+
? typeof condition.value !== "number"
423+
? { value: "" }
424+
: null
425+
: typeof condition.value !== "string"
426+
? { value: "" }
427+
: null),
428+
},
429+
{
430+
shouldDirty: true,
431+
},
432+
)
433+
}
434+
items={(["number", "currency"].includes(attributeType)
411435
? NUMBER_CONDITION_OPERATORS
412436
: STRING_CONDITION_OPERATORS
413-
).map((operator) => ({
414-
text: CONDITION_OPERATOR_LABELS[operator],
415-
value: operator,
416-
}))}
417-
/>
418-
</InlineBadgePopover>{" "}
437+
).map((operator) => ({
438+
text: CONDITION_OPERATOR_LABELS[operator],
439+
value: operator,
440+
}))}
441+
/>
442+
</InlineBadgePopover>
443+
)}{" "}
419444
{condition.operator && (
420445
<>
421446
<InlineBadgePopover

0 commit comments

Comments
 (0)