|
| 1 | +import { Spinner } from "@/components/ui/Spinner/Spinner"; |
| 2 | +import { |
| 3 | + Table, |
| 4 | + TableBody, |
| 5 | + TableCell, |
| 6 | + TableContainer, |
| 7 | + TableHead, |
| 8 | + TableHeader, |
| 9 | + TableRow, |
| 10 | +} from "@/components/ui/table"; |
| 11 | +import { API_SERVER_URL } from "@/constants/env"; |
| 12 | +import { format, fromUnixTime } from "date-fns"; |
| 13 | +import { |
| 14 | + CalendarIcon, |
| 15 | + CalendarX2Icon, |
| 16 | + ClockIcon, |
| 17 | + InfinityIcon, |
| 18 | +} from "lucide-react"; |
| 19 | +import { Suspense } from "react"; |
| 20 | +import { getAuthToken } from "../../../../../app/api/lib/getAuthToken"; |
| 21 | + |
| 22 | +interface Coupon { |
| 23 | + id: string; |
| 24 | + name: string; |
| 25 | + duration: "forever" | "once" | "repeating"; |
| 26 | + duration_in_months: number | null; |
| 27 | +} |
| 28 | + |
| 29 | +interface CouponData { |
| 30 | + id: string; |
| 31 | + start: number; |
| 32 | + end: number | null; |
| 33 | + coupon: Coupon; |
| 34 | +} |
| 35 | + |
| 36 | +export default function SubscriptionCouponsUI(props: { |
| 37 | + data: CouponData[]; |
| 38 | + status: "pending" | "error" | "success"; |
| 39 | +}) { |
| 40 | + const formatUnixTime = (timestamp: number) => { |
| 41 | + const date = fromUnixTime(timestamp); |
| 42 | + return format(date, "MMM d, yyyy"); |
| 43 | + }; |
| 44 | + |
| 45 | + // Function to render duration information with appropriate icon |
| 46 | + const renderDuration = (coupon: Coupon) => { |
| 47 | + if (coupon.duration === "forever") { |
| 48 | + return ( |
| 49 | + <div className="flex items-center gap-2"> |
| 50 | + <InfinityIcon className="size-4 text-muted-foreground" /> |
| 51 | + <span>Forever</span> |
| 52 | + </div> |
| 53 | + ); |
| 54 | + } else if (coupon.duration === "once") { |
| 55 | + return ( |
| 56 | + <div className="flex items-center gap-2"> |
| 57 | + <ClockIcon className="size-4 text-muted-foreground" /> |
| 58 | + <span>One-time discount</span> |
| 59 | + </div> |
| 60 | + ); |
| 61 | + } else if (coupon.duration === "repeating") { |
| 62 | + return ( |
| 63 | + <div className="flex items-center gap-2"> |
| 64 | + <ClockIcon className="size-4 text-muted-foreground" /> |
| 65 | + <span> |
| 66 | + Repeats every {coupon.duration_in_months} month |
| 67 | + {coupon.duration_in_months !== 1 ? "s" : ""} |
| 68 | + </span> |
| 69 | + </div> |
| 70 | + ); |
| 71 | + } else { |
| 72 | + return null; |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className="overflow-hidden rounded-lg border"> |
| 78 | + <div className="border-b bg-card px-6 py-4"> |
| 79 | + <h3 className="font-semibold text-xl tracking-tight"> |
| 80 | + Subscription Coupons |
| 81 | + </h3> |
| 82 | + </div> |
| 83 | + |
| 84 | + {props.status === "success" && props.data.length > 0 ? ( |
| 85 | + <TableContainer className="rounded-t-none border-none"> |
| 86 | + <Table> |
| 87 | + <TableHeader> |
| 88 | + <TableRow> |
| 89 | + <TableHead>Name</TableHead> |
| 90 | + <TableHead>Starts On</TableHead> |
| 91 | + <TableHead>Ends On</TableHead> |
| 92 | + <TableHead>Duration </TableHead> |
| 93 | + </TableRow> |
| 94 | + </TableHeader> |
| 95 | + <TableBody> |
| 96 | + {props.data.map((item) => ( |
| 97 | + <TableRow key={item.id}> |
| 98 | + {/* Name */} |
| 99 | + <TableCell className="font-medium"> |
| 100 | + {item.coupon.name} |
| 101 | + </TableCell> |
| 102 | + |
| 103 | + {/* Start Date */} |
| 104 | + <TableCell> |
| 105 | + <div className="flex items-center gap-2"> |
| 106 | + <CalendarIcon className="size-4 text-muted-foreground" /> |
| 107 | + {formatUnixTime(item.start)} |
| 108 | + </div> |
| 109 | + </TableCell> |
| 110 | + |
| 111 | + {/* End Date */} |
| 112 | + <TableCell> |
| 113 | + {item.end !== null ? ( |
| 114 | + <div className="flex items-center gap-2"> |
| 115 | + <CalendarIcon className="size-4 text-muted-foreground" /> |
| 116 | + {formatUnixTime(item.end)} |
| 117 | + </div> |
| 118 | + ) : ( |
| 119 | + <div className="flex items-center gap-2"> |
| 120 | + <CalendarX2Icon className="size-4 text-muted-foreground" /> |
| 121 | + <span className="text-foreground">Never </span> |
| 122 | + </div> |
| 123 | + )} |
| 124 | + </TableCell> |
| 125 | + |
| 126 | + {/* Duration */} |
| 127 | + <TableCell>{renderDuration(item.coupon)}</TableCell> |
| 128 | + </TableRow> |
| 129 | + ))} |
| 130 | + </TableBody> |
| 131 | + </Table> |
| 132 | + </TableContainer> |
| 133 | + ) : ( |
| 134 | + <div className="px-6 py-4"> |
| 135 | + <p className="flex min-h-[150px] items-center justify-center text-muted-foreground"> |
| 136 | + {props.status === "pending" ? ( |
| 137 | + <Spinner className="size-8" /> |
| 138 | + ) : props.status === "error" ? ( |
| 139 | + <span className="text-destructive-text"> |
| 140 | + Failed to load coupons |
| 141 | + </span> |
| 142 | + ) : ( |
| 143 | + <span>No coupons</span> |
| 144 | + )} |
| 145 | + </p> |
| 146 | + </div> |
| 147 | + )} |
| 148 | + </div> |
| 149 | + ); |
| 150 | +} |
| 151 | + |
| 152 | +async function AsyncSubscriptionCoupons(props: { |
| 153 | + teamId: string; |
| 154 | +}) { |
| 155 | + const authToken = await getAuthToken(); |
| 156 | + |
| 157 | + const res = await fetch( |
| 158 | + `${API_SERVER_URL}/v1/active-coupons?teamId=${props.teamId}`, |
| 159 | + { |
| 160 | + headers: { |
| 161 | + authorization: `Bearer ${authToken}`, |
| 162 | + }, |
| 163 | + }, |
| 164 | + ); |
| 165 | + |
| 166 | + if (!res.ok) { |
| 167 | + return <SubscriptionCouponsUI status="error" data={[]} />; |
| 168 | + } |
| 169 | + |
| 170 | + const data = (await res.json()) as { |
| 171 | + data: CouponData[]; |
| 172 | + }; |
| 173 | + |
| 174 | + return <SubscriptionCouponsUI status="success" data={data.data} />; |
| 175 | +} |
| 176 | + |
| 177 | +export function SubscriptionCoupons(props: { |
| 178 | + teamId: string; |
| 179 | +}) { |
| 180 | + return ( |
| 181 | + <Suspense fallback={<SubscriptionCouponsUI status="pending" data={[]} />}> |
| 182 | + <AsyncSubscriptionCoupons teamId={props.teamId} /> |
| 183 | + </Suspense> |
| 184 | + ); |
| 185 | +} |
0 commit comments