The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Refactor bounty sheet and add AmountInput component
Kiran Kec8ae41503cf
diff --git a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts b/apps/web/app/(ee)/api/bounties/[bountyId]/route.tsindex a142bab755..93053cf2c6 100644--- a/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts+++ b/apps/web/app/(ee)/api/bounties/[bountyId]/route.ts@@ -1,17 +1,31 @@+import { getBountyOrThrow } from "@/lib/api/bounty/get-bounty-or-throw";import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";import { withWorkspace } from "@/lib/auth";+import { BountySchema } from "@/lib/zod/schemas/bounties";import { NextResponse } from "next/server";// PATCH /api/bounties/[bountyId] - update a bounty-export const PATCH = withWorkspace(async ({ workspace, searchParams }) => {+export const PATCH = withWorkspace(async ({ workspace, params }) => {+ const { bountyId } = params;const programId = getDefaultProgramIdOrThrow(workspace);- return NextResponse.json({});+ const bounty = await getBountyOrThrow({+ bountyId,+ programId,+ });++ return NextResponse.json(BountySchema.parse(bounty));});// DELETE /api/bounties/[bountyId] - delete a bounty-export const DELETE = withWorkspace(async ({ workspace, searchParams }) => {+export const DELETE = withWorkspace(async ({ workspace, params }) => {+ const { bountyId } = params;const programId = getDefaultProgramIdOrThrow(workspace);- return NextResponse.json({});+ const bounty = await getBountyOrThrow({+ bountyId,+ programId,+ });++ return NextResponse.json({ id: bounty.id });});diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/create-bounty-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty-sheet.tsxsimilarity index 83%rename from apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/create-bounty-sheet.tsxrename to apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty-sheet.tsxindex 85af9f910f..9c88e71425 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/create-bounty-sheet.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/add-edit-bounty-sheet.tsx@@ -1,4 +1,3 @@-import { handleMoneyKeyDown } from "@/lib/form-utils";import useWorkspace from "@/lib/swr/use-workspace";import { createBountySchema } from "@/lib/zod/schemas/bounties";import {@@ -7,6 +6,7 @@ import {ProgramSheetAccordionItem,ProgramSheetAccordionTrigger,} from "@/ui/partners/program-sheet-accordion";+import { AmountInput } from "@/ui/shared/amount-input";import { X } from "@/ui/shared/icons";import {AnimatedSizeContainer,@@ -17,13 +17,12 @@ import {SmartDateTimePicker,Switch,} from "@dub/ui";-import { cn } from "@dub/utils";import { Dispatch, SetStateAction, useEffect, useMemo, useState } from "react";import { Controller, useForm } from "react-hook-form";import { toast } from "sonner";import { z } from "zod";-interface CreateBountySheetProps {+interface BountySheetProps {setIsOpen: Dispatch<SetStateAction<boolean>>;partnerId?: string;}@@ -43,14 +42,12 @@ const BOUNTY_TYPES: CardSelectorOption[] = [},];-function CreateBountySheetContent({ setIsOpen }: CreateBountySheetProps) {- const { id: workspaceId, defaultProgramId } = useWorkspace();+const ACCORDION_ITEMS = ["bounty-type", "bounty-details"];+function BountySheetContent({ setIsOpen }: BountySheetProps) {const [hasEndDate, setHasEndDate] = useState(false);- const [openAccordions, setOpenAccordions] = useState([- "partner-and-type",- "bounty-details",- ]);+ const { id: workspaceId, defaultProgramId } = useWorkspace();+ const [openAccordions, setOpenAccordions] = useState(ACCORDION_ITEMS);const {handleSubmit,@@ -112,7 +109,7 @@ function CreateBountySheetContent({ setIsOpen }: CreateBountySheetProps) {onValueChange={setOpenAccordions}className="space-y-6">- <ProgramSheetAccordionItem value="partner-and-type">+ <ProgramSheetAccordionItem value="bounty-type"><ProgramSheetAccordionTrigger>Bounty type</ProgramSheetAccordionTrigger>@@ -200,10 +197,7 @@ function CreateBountySheetContent({ setIsOpen }: CreateBountySheetProps) {>Reward</label>- <div className="relative mt-2 rounded-md shadow-sm">- <span className="absolute inset-y-0 left-0 flex items-center pl-3 text-sm text-neutral-400">- $- </span>+ <div className="mt-2"><Controllername="rewardAmount"control={control}@@ -212,14 +206,12 @@ function CreateBountySheetContent({ setIsOpen }: CreateBountySheetProps) {min: 0,}}render={({ field }) => (- <input+ <AmountInput{...field}- type="number"- className={cn(- "block w-full rounded-md border-neutral-300 pl-6 pr-12 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",- errors.rewardAmount &&- "border-red-600 focus:border-red-500 focus:ring-red-600",- )}+ id="rewardAmount"+ amountType="flat"+ placeholder="200"+ error={errors.rewardAmount?.message}value={field.value == null || isNaN(field.value)? ""@@ -227,18 +219,14 @@ function CreateBountySheetContent({ setIsOpen }: CreateBountySheetProps) {}onChange={(e) => {const val = e.target.value;+field.onChange(val === "" ? null : parseFloat(val),);}}- onKeyDown={handleMoneyKeyDown}- placeholder="200"/>)}/>- <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-sm text-neutral-400">- USD- </span></div></div></div>@@ -273,29 +261,29 @@ function CreateBountySheetContent({ setIsOpen }: CreateBountySheetProps) {);}-export function CreateBountySheet({+export function BountySheet({isOpen,nested,...rest-}: CreateBountySheetProps & {+}: BountySheetProps & {isOpen: boolean;nested?: boolean;}) {return (<Sheet open={isOpen} onOpenChange={rest.setIsOpen} nested={nested}>- <CreateBountySheetContent {...rest} />+ <BountySheetContent {...rest} /></Sheet>);}-export function useCreateBountySheet(- props: { nested?: boolean } & Omit<CreateBountySheetProps, "setIsOpen">,+export function useBountySheet(+ props: { nested?: boolean } & Omit<BountySheetProps, "setIsOpen">,) {const [isOpen, setIsOpen] = useState(false);return {- createBountySheet: (- <CreateBountySheet setIsOpen={setIsOpen} isOpen={isOpen} {...props} />+ BountySheet: (+ <BountySheet setIsOpen={setIsOpen} isOpen={isOpen} {...props} />),setIsOpen,};diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/create-bounty-button.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/create-bounty-button.tsxindex ffcb467f6d..c644456d49 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/create-bounty-button.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/create-bounty-button.tsx@@ -1,20 +1,19 @@"use client";import { Button, useKeyboardShortcut } from "@dub/ui";-import { useCreateBountySheet } from "./create-bounty-sheet";+import { useBountySheet } from "./add-edit-bounty-sheet";export function CreateBountyButton() {- const { createBountySheet, setIsOpen: setShowCreateBountySheet } =- useCreateBountySheet({- nested: false,- partnerId: "",- });+ const { BountySheet, setIsOpen: setShowCreateBountySheet } = useBountySheet({+ nested: false,+ partnerId: "",+ });useKeyboardShortcut("c", () => setShowCreateBountySheet(true));return (<>- {createBountySheet}+ {BountySheet}<Buttontype="button"onClick={() => setShowCreateBountySheet(true)}diff --git a/apps/web/lib/api/bounty/get-bounty-or-throw.ts b/apps/web/lib/api/bounty/get-bounty-or-throw.tsnew file mode 100644index 0000000000..9d8bedc6a1--- /dev/null+++ b/apps/web/lib/api/bounty/get-bounty-or-throw.ts@@ -0,0 +1,32 @@+import { prisma } from "@dub/prisma";+import { DubApiError } from "../errors";++export const getBountyOrThrow = async ({+ bountyId,+ programId,+}: {+ bountyId: string;+ programId: string;+}) => {+ const bounty = await prisma.bounty.findUnique({+ where: {+ id: bountyId,+ },+ });++ if (!bounty) {+ throw new DubApiError({+ code: "not_found",+ message: `Bounty ${bountyId} not found.`,+ });+ }++ if (bounty.programId !== programId) {+ throw new DubApiError({+ code: "forbidden",+ message: `Bounty ${bountyId} does not belong to the program.`,+ });+ }++ return bounty;+};diff --git a/apps/web/ui/shared/amount-input.tsx b/apps/web/ui/shared/amount-input.tsxnew file mode 100644index 0000000000..1dc010436f--- /dev/null+++ b/apps/web/ui/shared/amount-input.tsx@@ -0,0 +1,88 @@+import { handleMoneyInputChange, handleMoneyKeyDown } from "@/lib/form-utils";+import { cn } from "@dub/utils";+import { forwardRef, InputHTMLAttributes } from "react";++export type AmountType = "flat" | "percentage";++interface AmountInputProps+ extends Omit<InputHTMLAttributes<HTMLInputElement>, "type"> {+ amountType?: AmountType;+ error?: string;+ currency?: string;+ prefix?: string;+ suffix?: string;+ containerClassName?: string;+ onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void;+ onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;+}++export const AmountInput = forwardRef<HTMLInputElement, AmountInputProps>(+ (+ {+ error,+ currency = "USD",+ amountType = "flat",+ prefix,+ suffix,+ containerClassName,+ className,+ id,+ onKeyDown,+ onChange,+ ...props+ },+ ref,+ ) => {+ const inputId = id || "amount";+ const isFlat = amountType === "flat";+ const displayPrefix = prefix ?? (isFlat ? "$" : "");+ const displaySuffix = suffix ?? (isFlat ? currency : "%");++ return (+ <div className={containerClassName}>+ <div className="relative rounded-md shadow-sm">+ {displayPrefix && (+ <span className="absolute inset-y-0 left-0 flex items-center pl-3 text-sm text-neutral-400">+ {displayPrefix}+ </span>+ )}++ <input+ ref={ref}+ id={inputId}+ type="number"+ className={cn(+ "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",+ displayPrefix && "pl-6",+ displaySuffix && "pr-12",+ error && "border-red-600 focus:border-red-500 focus:ring-red-600",+ className,+ )}+ min="0"+ step={isFlat ? "0.01" : "1"}+ max={!isFlat ? 100 : undefined}+ onKeyDown={(e) => {+ handleMoneyKeyDown(e);+ onKeyDown?.(e);+ }}+ onChange={(e) => {+ handleMoneyInputChange(e);+ onChange?.(e);+ }}+ {...props}+ />++ {displaySuffix && (+ <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-sm text-neutral-400">+ {displaySuffix}+ </span>+ )}+ </div>++ {error && <p className="mt-1 text-xs text-red-600">{error}</p>}+ </div>+ );+ },+);++AmountInput.displayName = "AmountInput";