Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

WIP inline condition config

Tim Wilson3a13c2baec4e
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index 2c1c43c6f5..8940308138 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -15,17 +15,28 @@ export const COMMISSION_TYPES = [
},
] as const;
+export const CONDITION_ENTITIES = ["customer", "sale"] as const;
+
+export const CONDITION_CUSTOMER_ATTRIBUTES = ["country"] as const;
+export const CONDITION_SALE_ATTRIBUTES = ["productId"] as const;
+export const CONDITION_ATTRIBUTES = [
+ ...CONDITION_CUSTOMER_ATTRIBUTES,
+ ...CONDITION_SALE_ATTRIBUTES,
+] as const;
+
+export const CONDITION_OPERATORS = [
+ "equals_to",
+ "not_equals",
+ "starts_with",
+ "ends_with",
+ "in",
+ "not_in",
+] as const;
+
export const rewardConditionSchema = z.object({
- entity: z.enum(["customer", "sale"]),
- attribute: z.enum(["country", "productId"]),
- operator: z.enum([
- "equals_to",
- "not_equals",
- "starts_with",
- "ends_with",
- "in",
- "not_in",
- ]),
+ entity: z.enum(CONDITION_ENTITIES),
+ attribute: z.enum(CONDITION_ATTRIBUTES),
+ operator: z.enum(CONDITION_OPERATORS),
value: z.union([
z.string(),
z.number(),
diff --git a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
index 2cbe45ede9..b7ed65a08d 100644
--- a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
+++ b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
@@ -21,7 +21,7 @@ import {
import { X } from "@/ui/shared/icons";
import { EventType } from "@dub/prisma/client";
import { Button, MoneyBills2, Sheet, Users } from "@dub/ui";
-import { cn, pluralize } from "@dub/utils";
+import { capitalize, cn, pluralize } from "@dub/utils";
import { useAction } from "next-safe-action/hooks";
import {
Dispatch,
@@ -256,7 +256,7 @@ function RewardSheetContent({
{selectedEvent === "sale" && (
<>
a{" "}
- <InlineBadgePopover text={type}>
+ <InlineBadgePopover text={capitalize(type)}>
<InlineBadgePopoverMenu
selectedValue={type}
onSelect={(value) =>
diff --git a/apps/web/ui/partners/rewards/inline-badge-popover.tsx b/apps/web/ui/partners/rewards/inline-badge-popover.tsx
index 10ef2d5e80..4e38dfd06a 100644
--- a/apps/web/ui/partners/rewards/inline-badge-popover.tsx
+++ b/apps/web/ui/partners/rewards/inline-badge-popover.tsx
@@ -49,7 +49,7 @@ export function InlineBadgePopover({
className={cn(
"inline-block rounded px-1.5 text-sm font-semibold transition-colors",
invalid
- ? "bg-orange-50 text-orange-700 hover:bg-orange-100 data-[state=open]:bg-orange-100"
+ ? "bg-orange-50 text-orange-500 hover:bg-orange-100 data-[state=open]:bg-orange-100"
: "bg-blue-50 text-blue-700 hover:bg-blue-100 data-[state=open]:bg-blue-100",
)}
>
diff --git a/apps/web/ui/partners/rewards/rewards-logic.tsx b/apps/web/ui/partners/rewards/rewards-logic.tsx
index e15e43b50b..4bf6b87481 100644
--- a/apps/web/ui/partners/rewards/rewards-logic.tsx
+++ b/apps/web/ui/partners/rewards/rewards-logic.tsx
@@ -1,6 +1,11 @@
"use client";
import useWorkspace from "@/lib/swr/use-workspace";
+import {
+ CONDITION_ATTRIBUTES,
+ CONDITION_CUSTOMER_ATTRIBUTES,
+ CONDITION_SALE_ATTRIBUTES,
+} from "@/lib/zod/schemas/rewards";
import { X } from "@/ui/shared/icons";
import {
ArrowTurnRight2,
@@ -18,6 +23,10 @@ import { Command } from "cmdk";
import { Fragment, useState } from "react";
import { useFieldArray, useWatch } from "react-hook-form";
import { useAddEditRewardForm } from "./add-edit-reward-sheet";
+import {
+ InlineBadgePopover,
+ InlineBadgePopoverMenu,
+} from "./inline-badge-popover";
import { RewardIconSquare } from "./reward-icon-square";
export function RewardsLogic({
@@ -123,11 +132,10 @@ function ConditionalGroup({
<div className="border-border-subtle flex items-center justify-between rounded-md border bg-white p-2.5">
<div className="flex items-center gap-1.5">
<RewardIconSquare icon={User} />
- <span>
- {conditionIndex === 0
- ? "If"
- : capitalize(operator.toLowerCase())}
- </span>
+ <ConditionLogic
+ modifierIndex={index}
+ conditionIndex={conditionIndex}
+ />
</div>
<div className="flex items-center gap-1">
{conditions.length > 1 && (
@@ -160,6 +168,88 @@ function ConditionalGroup({
);
}
+const ENTITIES = {
+ customer: {
+ attributes: CONDITION_CUSTOMER_ATTRIBUTES,
+ },
+ sale: {
+ attributes: CONDITION_SALE_ATTRIBUTES,
+ },
+} as const;
+
+function ConditionLogic({
+ modifierIndex,
+ conditionIndex,
+}: {
+ modifierIndex: number;
+ conditionIndex: number;
+}) {
+ const modifierKey = `modifiers.${modifierIndex}` as const;
+ const conditionKey = `${modifierKey}.conditions.${conditionIndex}` as const;
+
+ const { control, setValue } = useAddEditRewardForm();
+ const [condition, operator] = useWatch({
+ control,
+ name: [conditionKey, `${modifierKey}.operator`],
+ });
+
+ // individual condition logic
+ return (
+ <span className="text-content-emphasis font-medium">
+ {conditionIndex === 0 ? "If" : capitalize(operator.toLowerCase())}{" "}
+ <InlineBadgePopover
+ text={capitalize(condition.entity) || "Select item"}
+ invalid={!condition.entity}
+ >
+ <InlineBadgePopoverMenu
+ selectedValue={condition.entity}
+ onSelect={(value) =>
+ setValue(
+ conditionKey,
+ { entity: value as keyof typeof ENTITIES },
+ {
+ shouldDirty: true,
+ },
+ )
+ }
+ items={Object.keys(ENTITIES).map((entity) => ({
+ text: capitalize(entity) || entity,
+ value: entity,
+ }))}
+ />
+ </InlineBadgePopover>{" "}
+ {condition.entity && (
+ <>
+ <InlineBadgePopover
+ text={capitalize(condition.attribute) || "Detail"}
+ invalid={!condition.attribute}
+ >
+ <InlineBadgePopoverMenu
+ selectedValue={condition.attribute}
+ onSelect={(value) =>
+ setValue(
+ conditionKey,
+ {
+ entity: condition.entity,
+ attribute: value as (typeof CONDITION_ATTRIBUTES)[number],
+ },
+ {
+ shouldDirty: true,
+ },
+ )
+ }
+ items={ENTITIES[condition.entity].attributes.map((attribute) => ({
+ text: capitalize(attribute) || attribute,
+ value: attribute,
+ }))}
+ />
+ </InlineBadgePopover>{" "}
+ </>
+ )}
+ </span>
+ );
+}
+
function OperatorDropdown({
modifierIndex,
showNumber,