|
1 | 1 | import { Button } from "@/components/ui/Button"; |
2 | 2 | import { useDialog } from "@/contexts"; |
3 | | -import { Check, CreditCard, HelpCircle } from "lucide-react"; |
4 | | -import { ReactNode } from "react"; |
| 3 | +import { Check, CreditCard, HelpCircle, X, AlertCircle, RefreshCcw } from "lucide-react"; |
| 4 | +import { ReactNode, useEffect } from "react"; |
| 5 | +import { motion } from "motion/react"; |
| 6 | +import { cn } from "@/lib/utils"; |
5 | 7 |
|
6 | 8 | type Props = { |
7 | 9 | status?: string; |
8 | 10 | transaction_no?: string; |
| 11 | + onRefresh?: () => void; |
9 | 12 | }; |
10 | 13 |
|
11 | | -const statusIconMap: Record<string, ReactNode> = { |
12 | | - paid: <Check className="h-20 w-20 text-green-500" />, |
13 | | - pending: <CreditCard className="h-20 w-20 text-yellow-500" />, |
| 14 | +type StatusConfig = { |
| 15 | + icon: ReactNode; |
| 16 | + title: string; |
| 17 | + description: string; |
| 18 | + bgColor: string; |
| 19 | + textColor: string; |
| 20 | + borderColor: string; |
| 21 | + action: "close" | "refresh" | "none"; |
| 22 | + autoClose?: number; |
14 | 23 | }; |
15 | 24 |
|
16 | | -const EventCheckStatusModal = ({ status = "", transaction_no = "" }: Props) => { |
| 25 | +const statusConfigMap: Record<string, StatusConfig> = { |
| 26 | + SUCCESS: { |
| 27 | + icon: <Check className="h-16 w-16" />, |
| 28 | + title: "Payment Successful!", |
| 29 | + description: "Your payment has been confirmed. You're all set for the event!", |
| 30 | + bgColor: "bg-green-50 dark:bg-green-950/20", |
| 31 | + textColor: "text-green-600 dark:text-green-400", |
| 32 | + borderColor: "border-green-200 dark:border-green-800", |
| 33 | + action: "close", |
| 34 | + autoClose: 3000, |
| 35 | + }, |
| 36 | + PENDING: { |
| 37 | + icon: <CreditCard className="h-16 w-16" />, |
| 38 | + title: "Payment Pending", |
| 39 | + description: "Your payment is still being processed. Please wait a moment and check again.", |
| 40 | + bgColor: "bg-yellow-50 dark:bg-yellow-950/20", |
| 41 | + textColor: "text-yellow-600 dark:text-yellow-500", |
| 42 | + borderColor: "border-yellow-200 dark:border-yellow-800", |
| 43 | + action: "refresh", |
| 44 | + }, |
| 45 | + FAILED: { |
| 46 | + icon: <X className="h-16 w-16" />, |
| 47 | + title: "Payment Failed", |
| 48 | + description: "Your payment could not be processed. Please try again or contact support.", |
| 49 | + bgColor: "bg-red-50 dark:bg-red-950/20", |
| 50 | + textColor: "text-red-600 dark:text-red-400", |
| 51 | + borderColor: "border-red-200 dark:border-red-800", |
| 52 | + action: "close", |
| 53 | + }, |
| 54 | + EXPIRED: { |
| 55 | + icon: <AlertCircle className="h-16 w-16" />, |
| 56 | + title: "Payment Expired", |
| 57 | + description: "Your payment link has expired. Please register again to get a new payment link.", |
| 58 | + bgColor: "bg-orange-50 dark:bg-orange-950/20", |
| 59 | + textColor: "text-orange-600 dark:text-orange-400", |
| 60 | + borderColor: "border-orange-200 dark:border-orange-800", |
| 61 | + action: "close", |
| 62 | + }, |
| 63 | +}; |
| 64 | + |
| 65 | +const EventCheckStatusModal = ({ status = "", transaction_no = "", onRefresh }: Props) => { |
17 | 66 | const { closeDialog } = useDialog(); |
18 | 67 |
|
19 | | - const icon = statusIconMap[status] ?? <HelpCircle className="h-20 w-20 text-gray-400" />; |
| 68 | + const config = |
| 69 | + statusConfigMap[status.toUpperCase()] || |
| 70 | + ({ |
| 71 | + icon: <HelpCircle className="h-16 w-16" />, |
| 72 | + title: "Unknown Status", |
| 73 | + description: "We couldn't determine the payment status. Please try again later.", |
| 74 | + bgColor: "bg-gray-50 dark:bg-gray-950/20", |
| 75 | + textColor: "text-gray-600 dark:text-gray-400", |
| 76 | + borderColor: "border-gray-200 dark:border-gray-800", |
| 77 | + action: "close", |
| 78 | + } as StatusConfig); |
| 79 | + |
| 80 | + useEffect(() => { |
| 81 | + if (config.autoClose && status.toUpperCase() === "SUCCESS") { |
| 82 | + const timer = setTimeout(() => { |
| 83 | + closeDialog(); |
| 84 | + onRefresh?.(); |
| 85 | + }, config.autoClose); |
| 86 | + |
| 87 | + return () => clearTimeout(timer); |
| 88 | + } |
| 89 | + }, [config.autoClose, status, closeDialog, onRefresh]); |
| 90 | + |
| 91 | + const handleAction = () => { |
| 92 | + if (config.action === "refresh" && onRefresh) { |
| 93 | + closeDialog(); |
| 94 | + setTimeout(() => { |
| 95 | + onRefresh(); |
| 96 | + }, 300); |
| 97 | + } else { |
| 98 | + closeDialog(); |
| 99 | + if (status.toUpperCase() === "SUCCESS") { |
| 100 | + onRefresh?.(); |
| 101 | + } |
| 102 | + } |
| 103 | + }; |
20 | 104 |
|
21 | 105 | return ( |
22 | | - <div className="flex flex-col items-center justify-center gap-6 py-8 text-center"> |
23 | | - {icon} |
| 106 | + <div className="flex flex-col items-center justify-center gap-6 py-6 text-center"> |
| 107 | + <motion.div |
| 108 | + initial={{ scale: 0 }} |
| 109 | + animate={{ scale: 1 }} |
| 110 | + transition={{ type: "spring", duration: 0.5, bounce: 0.4 }} |
| 111 | + className={cn( |
| 112 | + "flex items-center justify-center rounded-full p-6", |
| 113 | + config.bgColor, |
| 114 | + config.borderColor, |
| 115 | + "border-2" |
| 116 | + )} |
| 117 | + > |
| 118 | + <motion.div |
| 119 | + initial={{ rotate: 0 }} |
| 120 | + animate={{ rotate: [0, 10, -10, 0] }} |
| 121 | + transition={{ duration: 0.5, delay: 0.2 }} |
| 122 | + className={config.textColor} |
| 123 | + > |
| 124 | + {config.icon} |
| 125 | + </motion.div> |
| 126 | + </motion.div> |
| 127 | + |
| 128 | + <motion.div |
| 129 | + initial={{ opacity: 0, y: 20 }} |
| 130 | + animate={{ opacity: 1, y: 0 }} |
| 131 | + transition={{ duration: 0.3, delay: 0.2 }} |
| 132 | + className="space-y-2" |
| 133 | + > |
| 134 | + <h3 className="text-xl font-bold">{config.title}</h3> |
| 135 | + <p className="text-muted-foreground mx-auto max-w-sm text-sm">{config.description}</p> |
24 | 136 |
|
25 | | - <div className="space-y-1"> |
26 | | - <p className="text-xl font-semibold capitalize">{status || "Unknown"}</p> |
27 | 137 | {transaction_no && ( |
28 | | - <p className="text-muted-foreground text-sm"> |
29 | | - Transaction No: <span className="font-medium">{transaction_no}</span> |
30 | | - </p> |
| 138 | + <div className={cn("mt-4 rounded-lg border p-3", config.borderColor, config.bgColor)}> |
| 139 | + <p className="text-xs text-gray-500 dark:text-gray-400">Transaction Number</p> |
| 140 | + <p className={cn("font-mono text-sm font-semibold", config.textColor)}>{transaction_no}</p> |
| 141 | + </div> |
| 142 | + )} |
| 143 | + </motion.div> |
| 144 | + |
| 145 | + <motion.div |
| 146 | + initial={{ opacity: 0 }} |
| 147 | + animate={{ opacity: 1 }} |
| 148 | + transition={{ duration: 0.3, delay: 0.4 }} |
| 149 | + className="flex w-full gap-3" |
| 150 | + > |
| 151 | + {config.action === "refresh" && ( |
| 152 | + <Button variant="outline" className="flex-1" onClick={handleAction}> |
| 153 | + <RefreshCcw className="mr-2 h-4 w-4" /> |
| 154 | + Check Again |
| 155 | + </Button> |
31 | 156 | )} |
32 | | - </div> |
| 157 | + <Button |
| 158 | + variant={status.toUpperCase() === "SUCCESS" ? "default" : "outline"} |
| 159 | + className={cn("flex-1", status.toUpperCase() === "SUCCESS" && "bg-green-600 hover:bg-green-700")} |
| 160 | + onClick={handleAction} |
| 161 | + > |
| 162 | + {status.toUpperCase() === "SUCCESS" ? "Great!" : "Close"} |
| 163 | + </Button> |
| 164 | + </motion.div> |
33 | 165 |
|
34 | | - <Button variant="outline" onClick={closeDialog}> |
35 | | - Kembali |
36 | | - </Button> |
| 166 | + {status.toUpperCase() === "SUCCESS" && config.autoClose && ( |
| 167 | + <motion.p |
| 168 | + initial={{ opacity: 0 }} |
| 169 | + animate={{ opacity: 1 }} |
| 170 | + transition={{ duration: 0.3, delay: 0.5 }} |
| 171 | + className="text-muted-foreground text-xs" |
| 172 | + > |
| 173 | + This dialog will close automatically in 3 seconds... |
| 174 | + </motion.p> |
| 175 | + )} |
37 | 176 | </div> |
38 | 177 | ); |
39 | 178 | }; |
|
0 commit comments