The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refactor campaign editor and transactional logic to streamline trigger condition handling.
Kiran K283079bb9505
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/campaign-editor.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/campaign-editor.tsxindex e6333d71e4..1a8680ee2b 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/campaign-editor.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/campaign-editor.tsx@@ -82,14 +82,7 @@ export function CampaignEditor({ campaign }: { campaign: Campaign }) {await saveDraftCampaign(`/api/campaigns/${campaign.id}`, {method: "PATCH",- body: {- ...changedFields,- triggerCondition: {- attribute: "totalLeads",- operator: "gte",- value: 1,- },- },+ body: changedFields,onSuccess: () => {reset(allFormData, { keepValues: true });},diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsxindex dadcd0ce1a..88f9de133e 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx@@ -1,25 +1,34 @@-import { isCurrencyAttribute } from "@/lib/api/workflows/utils";-import { handleMoneyInputChange, handleMoneyKeyDown } from "@/lib/form-utils";-import { CAMPAIGN_WORKFLOW_ATTRIBUTE_LABELS } from "@/lib/zod/schemas/campaigns";+import {+ ALLOWED_ATTRIBUTE_VALUES_IN_DAYS,+ CAMPAIGN_WORKFLOW_ATTRIBUTE_LABELS,+} from "@/lib/zod/schemas/campaigns";import {InlineBadgePopover,- InlineBadgePopoverContext,InlineBadgePopoverMenu,} from "@/ui/shared/inline-badge-popover";-import { cn, currencyFormatter } from "@dub/utils";-import { useContext } from "react";+import { pluralize } from "@dub/utils";+import { useEffect } from "react";import { Controller } from "react-hook-form";import { useCampaignFormContext } from "./campaign-form-context";export function TransactionalCampaignLogic() {- const { control, watch } = useCampaignFormContext();+ const { control, watch, setValue } = useCampaignFormContext();- const [attribute, operator, value] = watch([+ const [attribute, value] = watch(["triggerCondition.attribute",- "triggerCondition.operator","triggerCondition.value",]);+ useEffect(() => {+ if (!attribute) {+ setValue("triggerCondition.attribute", "partnerEnrolledDays");+ }++ if (value === undefined || value === null) {+ setValue("triggerCondition.value", 1);+ }+ }, [attribute, value, setValue]);+return (<div className="flex w-full items-center"><span className="text-content-emphasis flex gap-1 text-sm font-medium leading-relaxed">@@ -33,7 +42,7 @@ export function TransactionalCampaignLogic() {text={field.value? CAMPAIGN_WORKFLOW_ATTRIBUTE_LABELS[field.value]- : "activity"+ : CAMPAIGN_WORKFLOW_ATTRIBUTE_LABELS.partnerEnrolledDays}invalid={!field.value}>@@ -50,92 +59,38 @@ export function TransactionalCampaignLogic() {</InlineBadgePopover>)}/>-- {/* <Controller- control={control}- name="triggerCondition.attribute"- render={({ field }) => (- <InlineBadgePopover- text={- field.value- ? WORKFLOW_ATTRIBUTE_LABELS[field.value].toLowerCase()- : "activity"+ </div>{" "}+ <Controller+ control={control}+ name="triggerCondition.value"+ render={({ field }) => (+ <InlineBadgePopover+ text={+ field.value !== undefined && field.value !== null+ ? String(field.value)+ : "1"+ }+ invalid={field.value === undefined || field.value === null}+ >+ <InlineBadgePopoverMenu+ selectedValue={+ field.value !== undefined && field.value !== null+ ? String(field.value)+ : "1"}- invalid={!field.value}- >- <InlineBadgePopoverMenu- selectedValue={field.value}- onSelect={field.onChange}- items={WORKFLOW_ATTRIBUTES.map((attribute) => ({- text: WORKFLOW_ATTRIBUTE_LABELS[attribute].toLowerCase(),- value: attribute,- }))}- />- </InlineBadgePopover>- )}- /> */}- </div>- <>- {" "}- is at least{" "}- <InlineBadgePopover- text={- value- ? isCurrencyAttribute(attribute)- ? currencyFormatter(value)- : value- : "amount"- }- invalid={!value}- >- <ValueInput />- </InlineBadgePopover>- </>+ onSelect={(val) => field.onChange(Number(val))}+ items={ALLOWED_ATTRIBUTE_VALUES_IN_DAYS.filter(+ (days) => days !== 0,+ ).map((days) => ({+ text: String(days),+ value: String(days),+ }))}+ />+ </InlineBadgePopover>+ )}+ />{" "}+ {pluralize("day", value || 1)}</span></div>);}--function ValueInput() {- const { watch, register } = useCampaignFormContext();- const { setIsOpen } = useContext(InlineBadgePopoverContext);-- const attribute = watch("triggerCondition.attribute");- const isCurrency = isCurrencyAttribute(attribute);-- return (- <div className="relative rounded-md shadow-sm">- {isCurrency && (- <span className="absolute inset-y-0 left-0 flex items-center pl-1.5 text-sm text-neutral-400">- $- </span>- )}- <input- className={cn(- "block w-full rounded-md border-neutral-300 px-1.5 py-1 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:w-32 sm:text-sm",- isCurrency ? "pl-4 pr-12" : "pr-7",- )}- {...register("triggerCondition.value", {- required: true,- setValueAs: (value: string) => (value === "" ? undefined : +value),- min: 0,- onChange: handleMoneyInputChange,- })}- onKeyDown={(e) => {- if (e.key === "Enter") {- e.preventDefault();- setIsOpen(false);- return;- }-- handleMoneyKeyDown(e);- }}- />- {isCurrency && (- <span className="absolute inset-y-0 right-0 flex items-center pr-1.5 text-sm text-neutral-400">- USD- </span>- )}- </div>- );-}diff --git a/apps/web/lib/api/workflows/utils.ts b/apps/web/lib/api/workflows/utils.tsindex d6e47eac35..ae38fc90eb 100644--- a/apps/web/lib/api/workflows/utils.ts+++ b/apps/web/lib/api/workflows/utils.ts@@ -2,3 +2,6 @@ import { WorkflowConditionAttribute } from "@/lib/types";export const isCurrencyAttribute = (activity: WorkflowConditionAttribute) =>activity === "totalCommissions" || activity === "totalSaleAmount";++export const isDaysAttribute = (activity: WorkflowConditionAttribute) =>+ activity === "partnerEnrolledDays";diff --git a/apps/web/lib/zod/schemas/workflows.ts b/apps/web/lib/zod/schemas/workflows.tsindex f68e812148..762f9ae0d0 100644--- a/apps/web/lib/zod/schemas/workflows.ts+++ b/apps/web/lib/zod/schemas/workflows.ts@@ -62,7 +62,7 @@ export const WORKFLOW_LOGICAL_OPERATORS = ["AND"] as const;// Individual conditionexport const workflowConditionSchema = z.object({attribute: z.enum(WORKFLOW_ATTRIBUTES),- operator: z.enum(WORKFLOW_COMPARISON_OPERATORS),+ operator: z.enum(WORKFLOW_COMPARISON_OPERATORS).default("gte"),value: z.number(),});