|
| 1 | +import { createClient } from "~/supabase/server"; |
| 2 | + |
| 3 | +import { Badge } from "@theliaison/ui/badge"; |
| 4 | +import { |
| 5 | + Card, |
| 6 | + CardContent, |
| 7 | + CardDescription, |
| 8 | + CardHeader, |
| 9 | + CardTitle, |
| 10 | +} from "@theliaison/ui/card"; |
| 11 | +import { |
| 12 | + Table, |
| 13 | + TableBody, |
| 14 | + TableCell, |
| 15 | + TableHead, |
| 16 | + TableHeader, |
| 17 | + TableRow, |
| 18 | +} from "@theliaison/ui/table"; |
| 19 | +import Link from "next/link"; |
| 20 | + |
| 21 | +export async function GiftsData() { |
| 22 | + const supabase = createClient(); |
| 23 | + const { data, error } = await supabase.from("gifts").select("*"); |
| 24 | + |
| 25 | + if (error) { |
| 26 | + console.log(error); |
| 27 | + return <div>Error</div>; |
| 28 | + } |
| 29 | + |
| 30 | + const status = ({ |
| 31 | + confirmed, |
| 32 | + rejected, |
| 33 | + }: { confirmed: boolean; rejected: boolean }) => { |
| 34 | + return rejected ? "Declined" : confirmed ? "Confirmed" : "Pending"; |
| 35 | + }; |
| 36 | + const statusVariant = ({ |
| 37 | + confirmed, |
| 38 | + rejected, |
| 39 | + }: { confirmed: boolean; rejected: boolean }) => { |
| 40 | + return rejected ? "destructive" : confirmed ? "default" : "secondary"; |
| 41 | + }; |
| 42 | + |
| 43 | + function transformPGDate(createdAtString: string) { |
| 44 | + const createdAtDate = new Date(createdAtString); |
| 45 | + |
| 46 | + const formattedDate = `${createdAtDate.toLocaleDateString()} ${createdAtDate.toLocaleTimeString()}`; |
| 47 | + |
| 48 | + const sanDiegoOffset = createdAtDate.getTimezoneOffset() - 480; |
| 49 | + const sanDiegoDate = new Date( |
| 50 | + createdAtDate.getTime() + sanDiegoOffset * 60 * 1000, |
| 51 | + ); |
| 52 | + const sanDiegoFormattedDate = `${sanDiegoDate.toLocaleDateString()} ${sanDiegoDate.toLocaleTimeString()}`; |
| 53 | + |
| 54 | + const prettySanDiegoFormattedDate = `${sanDiegoDate.toLocaleDateString().replaceAll("/", "-")} (${sanDiegoDate.toLocaleTimeString( |
| 55 | + "en-US", |
| 56 | + { |
| 57 | + hour: "numeric", |
| 58 | + minute: "numeric", |
| 59 | + }, |
| 60 | + )})`; |
| 61 | + |
| 62 | + return { |
| 63 | + formattedDate, |
| 64 | + sanDiegoFormattedDate, |
| 65 | + prettySanDiegoFormattedDate, |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + return ( |
| 70 | + <Card> |
| 71 | + <CardHeader className="px-7"> |
| 72 | + <CardTitle>Orders</CardTitle> |
| 73 | + <CardDescription> |
| 74 | + Recent orders from the Gifting Concierge. |
| 75 | + </CardDescription> |
| 76 | + </CardHeader> |
| 77 | + <CardContent> |
| 78 | + <Table> |
| 79 | + <TableHeader> |
| 80 | + <TableRow> |
| 81 | + <TableHead>Customer</TableHead> |
| 82 | + <TableHead className="hidden sm:table-cell">Recipient</TableHead> |
| 83 | + <TableHead className="hidden sm:table-cell">Status</TableHead> |
| 84 | + <TableHead className="hidden md:table-cell">Date</TableHead> |
| 85 | + <TableHead className="text-right">Amount</TableHead> |
| 86 | + </TableRow> |
| 87 | + </TableHeader> |
| 88 | + <TableBody> |
| 89 | + {data?.map((gift) => ( |
| 90 | + <TableRow key={gift.id}> |
| 91 | + <TableCell> |
| 92 | + <Link href={`/dashboard/users/${gift.sender_name}`}> |
| 93 | + <div className="font-medium">{gift.sender_name}</div> |
| 94 | + <div className="hidden text-sm text-muted-foreground md:inline"> |
| 95 | + ricardo@mail.com |
| 96 | + </div> |
| 97 | + </Link> |
| 98 | + </TableCell> |
| 99 | + <TableCell className="hidden sm:table-cell"> |
| 100 | + <Link href={`/dashboard/gifting-concierge/${gift.id}`}> |
| 101 | + <div className="font-medium">{gift.recipient_name}</div> |
| 102 | + <div className="hidden text-sm text-muted-foreground md:inline"> |
| 103 | + {gift.recipient_social} |
| 104 | + </div> |
| 105 | + </Link> |
| 106 | + </TableCell> |
| 107 | + <TableCell className="hidden sm:table-cell"> |
| 108 | + <Badge |
| 109 | + className="text-xs" |
| 110 | + variant={statusVariant({ |
| 111 | + confirmed: gift.is_confirmed, |
| 112 | + rejected: gift.is_rejected, |
| 113 | + })} |
| 114 | + > |
| 115 | + {status({ |
| 116 | + confirmed: gift.is_confirmed, |
| 117 | + rejected: gift.is_rejected, |
| 118 | + })} |
| 119 | + </Badge> |
| 120 | + </TableCell> |
| 121 | + <TableCell className="hidden md:table-cell"> |
| 122 | + {transformPGDate(gift.created_at).prettySanDiegoFormattedDate} |
| 123 | + </TableCell> |
| 124 | + <TableCell className="text-right"> |
| 125 | + {Intl.NumberFormat("en-US", { |
| 126 | + style: "currency", |
| 127 | + currency: "USD", |
| 128 | + }).format(gift.total_price)} |
| 129 | + </TableCell> |
| 130 | + </TableRow> |
| 131 | + ))} |
| 132 | + </TableBody> |
| 133 | + </Table> |
| 134 | + </CardContent> |
| 135 | + </Card> |
| 136 | + ); |
| 137 | +} |
0 commit comments