Update the store helper and broadcast dialog
This commit is contained in:
@@ -14,10 +14,6 @@ interface BroadcastDialogProps {
|
||||
setOpen: (open: boolean) => void;
|
||||
}
|
||||
|
||||
interface BroadcastMessage {
|
||||
message: string;
|
||||
}
|
||||
|
||||
export default function BroadcastDialog({ open, setOpen }: BroadcastDialogProps) {
|
||||
const [broadcastMessage, setBroadcastMessage] = useState<string>("");
|
||||
const [sendingBroadcast, setSendingBroadcast] = useState<boolean>(false);
|
||||
@@ -31,8 +27,15 @@ export default function BroadcastDialog({ open, setOpen }: BroadcastDialogProps)
|
||||
|
||||
try {
|
||||
setSendingBroadcast(true);
|
||||
await apiRequest("/storefront/broadcast", "POST", { message: broadcastMessage });
|
||||
toast.success("Broadcast message sent successfully!");
|
||||
|
||||
// ✅ API Call
|
||||
const response = await apiRequest("/storefront/broadcast", "POST", { message: broadcastMessage });
|
||||
|
||||
if (response.error) {
|
||||
throw new Error(response.error);
|
||||
}
|
||||
|
||||
toast.success(`Broadcast sent to ${response.totalUsers} users!`);
|
||||
setBroadcastMessage("");
|
||||
setOpen(false);
|
||||
} catch (error) {
|
||||
@@ -49,10 +52,16 @@ export default function BroadcastDialog({ open, setOpen }: BroadcastDialogProps)
|
||||
<Send className="h-6 w-6 text-emerald-600" />
|
||||
<DialogTitle>Global Broadcast Message</DialogTitle>
|
||||
</DialogHeader>
|
||||
<Textarea value={broadcastMessage} onChange={(e) => setBroadcastMessage(e.target.value)} />
|
||||
<Textarea
|
||||
placeholder="Type your message here..."
|
||||
value={broadcastMessage}
|
||||
onChange={(e) => setBroadcastMessage(e.target.value)}
|
||||
/>
|
||||
<DialogFooter>
|
||||
<Button onClick={() => setOpen(false)}>Cancel</Button>
|
||||
<Button onClick={sendBroadcast} disabled={sendingBroadcast}>
|
||||
<Button onClick={() => setOpen(false)} variant="secondary">
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={sendBroadcast} disabled={sendingBroadcast} className="bg-emerald-600 hover:bg-emerald-700">
|
||||
{sendingBroadcast ? "Sending..." : "Send Broadcast"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
|
||||
@@ -1,8 +1,21 @@
|
||||
export const apiRequest = async <T = any>(endpoint: string, method: string = "GET", body?: T | null) => {
|
||||
try {
|
||||
const authToken = document.cookie.split("; ").find(row => row.startsWith("Authorization="))?.split("=")[1];
|
||||
if (!authToken) throw new Error("No authentication token found");
|
||||
if (typeof document === "undefined") {
|
||||
throw new Error("API requests must be made from the client side.");
|
||||
}
|
||||
|
||||
const authToken = document.cookie
|
||||
.split("; ")
|
||||
.find((row) => row.startsWith("Authorization="))
|
||||
?.split("=")[1];
|
||||
|
||||
if (!authToken){
|
||||
// go to /login
|
||||
document.location.href = "/login";
|
||||
throw new Error("No authentication token found");
|
||||
}
|
||||
|
||||
// ✅ API Request Options
|
||||
const options: RequestInit = {
|
||||
method,
|
||||
headers: {
|
||||
@@ -16,21 +29,26 @@ export const apiRequest = async <T = any>(endpoint: string, method: string = "GE
|
||||
options.body = JSON.stringify(body);
|
||||
}
|
||||
|
||||
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}${endpoint}`, options);
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL;
|
||||
if (!API_URL) throw new Error("NEXT_PUBLIC_API_URL is not set in environment variables");
|
||||
|
||||
const res = await fetch(`${API_URL}${endpoint}`, options);
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to ${method} ${endpoint}: ${res.statusText}`);
|
||||
const errorResponse = await res.json().catch(() => null);
|
||||
const errorMessage = errorResponse?.error || res.statusText || "Unknown error";
|
||||
throw new Error(`Failed to ${method} ${endpoint}: ${errorMessage}`);
|
||||
}
|
||||
|
||||
// ✅ Return JSON response
|
||||
return await res.json();
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`Error in API request: ${error.message}`);
|
||||
console.error(`🚨 API Request Error: ${error.message}`);
|
||||
throw new Error(error.message);
|
||||
}
|
||||
|
||||
console.error("An unknown error occurred", error);
|
||||
console.error("❌ An unknown error occurred", error);
|
||||
throw new Error("An unknown error occurred");
|
||||
}
|
||||
};
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user