"use client"; import React, { useState } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { createAlert } from "@/lib/actions/alert.actions"; import { toast } from "sonner"; // Assuming sonner is available or use existing toast interface CreateAlertModalProps { userId: string; symbol: string; currentPrice: number; companyName?: string; // Optional prop for better display onAlertCreated?: () => void; children?: React.ReactNode; // Controlled props open?: boolean; onOpenChange?: (open: boolean) => void; } export default function CreateAlertModal({ userId, symbol, currentPrice, companyName = "", onAlertCreated, children, open: controlledOpen, onOpenChange: setControlledOpen }: CreateAlertModalProps) { const [internalOpen, setInternalOpen] = useState(false); const isControlled = controlledOpen !== undefined; const open = isControlled ? controlledOpen : internalOpen; const setOpen = isControlled ? setControlledOpen : setInternalOpen; const [targetPrice, setTargetPrice] = useState(currentPrice.toString()); const [condition, setCondition] = useState<"ABOVE" | "BELOW">("ABOVE"); const [alertName, setAlertName] = useState(""); const [loading, setLoading] = useState(false); // Update target price when currentPrice changes (e.g. freshly fetched) React.useEffect(() => { setTargetPrice(currentPrice.toString()); }, [currentPrice]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { await createAlert({ userId, symbol, targetPrice: parseFloat(targetPrice), condition, }); toast.success("Alert created successfully"); setOpen?.(false); if (onAlertCreated) onAlertCreated(); } catch (error) { console.error(error); toast.error("Failed to create alert"); } finally { setLoading(false); } }; return ( {children && ( {children} )} Price Alert
{/* Alert Name */}
setAlertName(e.target.value)} placeholder="e.g. Apple at Discount" className="bg-gray-900 border-gray-700 text-white placeholder:text-gray-600 focus:border-yellow-500 focus:ring-yellow-500/20 transition-all rounded-md h-10" />
{/* Stock Identifier */}
{/* Alert Type */}
{/* Condition */}
{/* Threshold Value */}
$ setTargetPrice(e.target.value)} placeholder="eg: 140" className="pl-7 bg-[#1C1C1F] border-gray-800 text-white placeholder:text-gray-600 focus:border-yellow-500 focus:ring-yellow-500/20 transition-all rounded-md h-10 font-mono" />
{/* Expiry Note */}

Alert expires automatically in 90 days

); }