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.

Update edit-rewards-modal.tsx

Tim Wilson2380c675accc
diff --git a/apps/web/ui/partners/design/modals/edit-rewards-modal.tsx b/apps/web/ui/partners/design/modals/edit-rewards-modal.tsx
index 7931d9fd19..3f0021d531 100644
--- a/apps/web/ui/partners/design/modals/edit-rewards-modal.tsx
+++ b/apps/web/ui/partners/design/modals/edit-rewards-modal.tsx
@@ -4,12 +4,14 @@ import useDiscounts from "@/lib/swr/use-discounts";
import useProgram from "@/lib/swr/use-program";
import useRewards from "@/lib/swr/use-rewards";
import {
+ AnimatedSizeContainer,
Button,
CursorRays,
Gift,
InvoiceDollar,
LoadingSpinner,
Modal,
+ TriangleWarning,
useMediaQuery,
UserPlus,
} from "@dub/ui";
@@ -23,7 +25,7 @@ import {
useMemo,
useState,
} from "react";
-import { useForm } from "react-hook-form";
+import { FormProvider, useForm, useFormState, useWatch } from "react-hook-form";
import { formatDiscountDescription } from "../../format-discount-description";
import { formatRewardDescription } from "../../format-reward-description";
import { BrandingFormData, useBrandingFormContext } from "../branding-form";
@@ -74,17 +76,19 @@ function EditRewardsModalInner({
const { getValues: getValuesParent, setValue: setValueParent } =
useBrandingFormContext();
+ const form = useForm<Pick<BrandingFormData, "landerData">>({
+ values: {
+ landerData: getValuesParent("landerData"),
+ },
+ });
+
const {
register,
setValue,
getValues,
handleSubmit,
formState: { isDirty },
- } = useForm<Pick<BrandingFormData, "landerData">>({
- values: {
- landerData: getValuesParent("landerData"),
- },
- });
+ } = form;
// Set default reward selections
useEffect(() => {
@@ -141,99 +145,145 @@ function EditRewardsModalInner({
landing page. These apply only to new partners after approval.
</p>
- <div className="mt-4 flex flex-col gap-6">
- {isLoading ? (
- <div className="flex items-center justify-center">
- <LoadingSpinner className="size-4" />
- </div>
- ) : error ? (
- <p className="text-content-subtle py-4 text-center text-sm">
- Failed to load rewards
- </p>
- ) : (
- <>
- {/* Sale/click/lead rewards */}
- {REWARD_EVENTS.map(({ event, label, icon: Icon }) => {
- const eventRewards = rewards?.filter((r) => r.event === event);
- if (!eventRewards?.length) return null;
-
- return (
- <label key={event}>
- <span className="text-content-emphasis flex items-center gap-1.5 text-sm font-medium">
- <Icon className="size-3.5" />
- {label}
- </span>
- <div className="mt-2 rounded-md shadow-sm">
- <select
- className={cn(
- "block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- )}
- {...register(`landerData.rewards.${event}RewardId`)}
- >
- <option value="none">None</option>
- {eventRewards?.map((reward) => (
- <option value={reward.id} key={reward.id}>
- {reward.name || formatRewardDescription({ reward })}{" "}
- {reward.default && "(Default)"}
- </option>
- ))}
- </select>
- </div>
- </label>
- );
- })}
-
- {/* Discount */}
- {Boolean(discounts?.length) && (
- <label>
- <span className="text-content-emphasis flex items-center gap-1.5 text-sm font-medium">
- <Gift className="size-3.5" />
- Discount
- </span>
- <div className="mt-2 rounded-md shadow-sm">
- <select
- className={cn(
- "block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- )}
- {...register(`landerData.rewards.discountId`)}
- >
- <option value="none">None</option>
- {discounts?.map((discount) => (
- <option value={discount.id} key={discount.id}>
- {formatDiscountDescription({ discount })}{" "}
- {program?.defaultDiscountId === discount.id &&
- "(Default)"}
- </option>
- ))}
- </select>
+ <FormProvider {...form}>
+ <AnimatedSizeContainer
+ height
+ transition={{ ease: "easeInOut", duration: 0.2 }}
+ className="-mx-1"
+ >
+ <div className="px-1 py-4">
+ {isLoading ? (
+ <div className="flex items-center justify-center">
+ <LoadingSpinner className="size-4" />
+ </div>
+ ) : error ? (
+ <p className="text-content-subtle py-4 text-center text-sm">
+ Failed to load rewards
+ </p>
+ ) : (
+ <>
+ <div className="flex flex-col gap-6">
+ {/* Sale/click/lead rewards */}
+ {REWARD_EVENTS.map(({ event, label, icon: Icon }) => {
+ const eventRewards = rewards?.filter(
+ (r) => r.event === event,
+ );
+ if (!eventRewards?.length) return null;
+
+ return (
+ <label key={event}>
+ <span className="text-content-emphasis flex items-center gap-1.5 text-sm font-medium">
+ <Icon className="size-3.5" />
+ {label}
+ </span>
+ <div className="mt-2 rounded-md shadow-sm">
+ <select
+ className={cn(
+ "block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ )}
+ {...register(
+ `landerData.rewards.${event}RewardId`,
+ )}
+ >
+ <option value="none">None</option>
+ {eventRewards?.map((reward) => (
+ <option value={reward.id} key={reward.id}>
+ {reward.name ||
+ formatRewardDescription({ reward })}{" "}
+ {reward.default && "(Default)"}
+ </option>
+ ))}
+ </select>
+ </div>
+ </label>
+ );
+ })}
+
+ {/* Discount */}
+ {Boolean(discounts?.length) && (
+ <label>
+ <span className="text-content-emphasis flex items-center gap-1.5 text-sm font-medium">
+ <Gift className="size-3.5" />
+ Discount
+ </span>
+ <div className="mt-2 rounded-md shadow-sm">
+ <select
+ className={cn(
+ "block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ )}
+ {...register(`landerData.rewards.discountId`)}
+ >
+ <option value="none">None</option>
+ {discounts?.map((discount) => (
+ <option value={discount.id} key={discount.id}>
+ {formatDiscountDescription({ discount })}{" "}
+ {program?.defaultDiscountId === discount.id &&
+ "(Default)"}
+ </option>
+ ))}
+ </select>
+ </div>
+ </label>
+ )}
</div>
- </label>
+
+ <NoneSelectedWarning />
+ </>
)}
- </>
- )}
- </div>
-
- <div className="mt-4 flex items-center justify-end gap-2">
- <Button
- type="button"
- variant="secondary"
- text="Cancel"
- className="h-9 w-fit"
- onClick={() => setShowEditRewardsModal(false)}
- />
- <Button
- type="submit"
- variant="primary"
- text="Save"
- className="h-9 w-fit"
- disabled={!isDirty}
- />
- </div>
+ </div>
+ </AnimatedSizeContainer>
+
+ <div className="mt-4 flex items-center justify-end gap-2">
+ <Button
+ type="button"
+ variant="secondary"
+ text="Cancel"
+ className="h-9 w-fit"
+ onClick={() => setShowEditRewardsModal(false)}
+ />
+ <SaveButton />
+ </div>
+ </FormProvider>
</form>
</>
);
}
+const isAllNone = (rewards: BrandingFormData["landerData"]["rewards"]) =>
+ rewards &&
+ Object.values(rewards).every((value) => !value || value === "none");
+
+function NoneSelectedWarning() {
+ const rewards = useWatch({ name: "landerData.rewards" });
+
+ if (!isAllNone(rewards)) return null;
+
+ return (
+ <div className="mt-5 rounded-lg border border-amber-200 bg-amber-50 p-4">
+ <TriangleWarning className="size-4 text-amber-500" />
+ <div className="mt-3 flex flex-col gap-2 text-sm text-amber-900">
+ <span className="font-semibold">No rewards selected</span>
+ <p>You must select at least one reward to continue.</p>
+ </div>
+ </div>
+ );
+}
+
+function SaveButton() {
+ const { isDirty } = useFormState();
+ const rewards = useWatch({ name: "landerData.rewards" });
+
+ return (
+ <Button
+ type="submit"
+ variant="primary"
+ text="Save"
+ className="h-9 w-fit"
+ disabled={!isDirty || isAllNone(rewards)}
+ />
+ );
+}
+
export function useEditRewardsModal() {
const [showEditRewardsModal, setShowEditRewardsModal] = useState(false);