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.

Set the modifier type to parent type if it's undefined (backward compatibility)

Kiran K1e458c50ed2b
diff --git a/apps/web/lib/api/sales/construct-reward-amount.ts b/apps/web/lib/api/sales/construct-reward-amount.ts
index 1807600ff0..ab35017368 100644
--- a/apps/web/lib/api/sales/construct-reward-amount.ts
+++ b/apps/web/lib/api/sales/construct-reward-amount.ts
@@ -14,11 +14,14 @@ export const constructRewardAmount = (
if (parsedModifiers.success) {
const modifiers = parsedModifiers.data;
+ // if no type or maxDuration, it falls back to the primary reward type and maxDuration
const matchPrimary = modifiers.every((m) => {
- const typeMatches = m.type === undefined || m.type === reward.type;
+ const typeMatches =
+ m.type === undefined ? true : m.type === reward.type;
const durationMatches =
- m.maxDuration === undefined || m.maxDuration === reward.maxDuration;
-
+ m.maxDuration === undefined
+ ? true
+ : m.maxDuration === reward.maxDuration;
return typeMatches && durationMatches;
});
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 8ad086072f..ce05991c2c 100644
--- a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
+++ b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
@@ -102,11 +102,19 @@ function RewardSheetContent({
defaultValuesSource?.type === "flat"
? defaultValuesSource.amount / 100
: defaultValuesSource?.amount,
- modifiers: defaultValuesSource?.modifiers?.map((m) => ({
- ...m,
- amount: m.type === "flat" ? m.amount / 100 : m.amount,
- maxDuration: m.maxDuration === null ? Infinity : m.maxDuration,
- })),
+ modifiers: defaultValuesSource?.modifiers?.map((m) => {
+ const type = m.type === undefined ? defaultValuesSource?.type : m.type;
+ const maxDuration =
+ m.maxDuration === undefined
+ ? defaultValuesSource?.maxDuration
+ : m.maxDuration;
+
+ return {
+ ...m,
+ amount: type === "flat" ? m.amount / 100 : m.amount,
+ maxDuration: m.maxDuration === null ? Infinity : maxDuration,
+ };
+ }),
},
});
@@ -186,11 +194,17 @@ function RewardSheetContent({
if (data.modifiers?.length) {
try {
modifiers = rewardConditionsArraySchema.parse(
- data.modifiers.map((m) => ({
- ...m,
- amount: m.type === "flat" ? m.amount * 100 : m.amount,
- maxDuration: m.maxDuration === Infinity ? null : m.maxDuration,
- })),
+ data.modifiers.map((m) => {
+ const type = m.type === undefined ? data.type : m.type;
+ const maxDuration =
+ m.maxDuration === undefined ? data.maxDuration : m.maxDuration;
+
+ return {
+ ...m,
+ amount: type === "flat" ? m.amount * 100 : m.amount,
+ maxDuration: maxDuration === Infinity ? null : maxDuration,
+ };
+ }),
);
} catch (error) {
setError("root.logic", { message: "Invalid reward condition" });
diff --git a/apps/web/ui/partners/rewards/rewards-logic.tsx b/apps/web/ui/partners/rewards/rewards-logic.tsx
index 7f076a9952..8ec30edf06 100644
--- a/apps/web/ui/partners/rewards/rewards-logic.tsx
+++ b/apps/web/ui/partners/rewards/rewards-logic.tsx
@@ -28,7 +28,7 @@ import { capitalize, cn, COUNTRIES, pluralize, truncate } from "@dub/utils";
import { Command } from "cmdk";
import { motion } from "framer-motion";
import { Package } from "lucide-react";
-import { Fragment, useContext, useState } from "react";
+import { Fragment, useContext, useEffect, useState } from "react";
import { useFieldArray, useWatch } from "react-hook-form";
import { useAddEditRewardForm } from "./add-edit-reward-sheet";
import {
@@ -679,14 +679,24 @@ function ResultTerms({ modifierIndex }: { modifierIndex: number }) {
}
function AmountInput({ modifierKey }: { modifierKey: `modifiers.${number}` }) {
- const { watch, register } = useAddEditRewardForm();
+ const { watch, register, setValue } = useAddEditRewardForm();
+ const { setIsOpen } = useContext(InlineBadgePopoverContext);
+
const type = watch(`${modifierKey}.type`);
+ const parentType = watch("type");
- const { setIsOpen } = useContext(InlineBadgePopoverContext);
+ const displayType = type || parentType;
+
+ // Set the modifier type to parent type if it's undefined (backward compatibility)
+ useEffect(() => {
+ if (type === undefined && parentType) {
+ setValue(`${modifierKey}.type`, parentType, { shouldDirty: true });
+ }
+ }, [type, parentType, setValue, modifierKey]);
return (
<div className="relative rounded-md shadow-sm">
- {type === "flat" && (
+ {displayType === "flat" && (
<span className="absolute inset-y-0 left-0 flex items-center pl-1.5 text-sm text-neutral-400">
$
</span>
@@ -694,13 +704,13 @@ function AmountInput({ modifierKey }: { modifierKey: `modifiers.${number}` }) {
<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",
- type === "flat" ? "pl-4 pr-12" : "pr-7",
+ displayType === "flat" ? "pl-4 pr-12" : "pr-7",
)}
{...register(`${modifierKey}.amount`, {
required: true,
setValueAs: (value: string) => (value === "" ? undefined : +value),
min: 0,
- max: type === "percentage" ? 100 : undefined,
+ max: displayType === "percentage" ? 100 : undefined,
onChange: handleMoneyInputChange,
})}
onKeyDown={(e) => {
@@ -713,7 +723,7 @@ function AmountInput({ modifierKey }: { modifierKey: `modifiers.${number}` }) {
}}
/>
<span className="absolute inset-y-0 right-0 flex items-center pr-1.5 text-sm text-neutral-400">
- {type === "flat" ? "USD" : "%"}
+ {displayType === "flat" ? "USD" : "%"}
</span>
</div>
);