Update the store helper and broadcast dialog

This commit is contained in:
g
2025-02-07 16:28:24 +00:00
parent 6158f232db
commit 2654accf80
2 changed files with 71 additions and 44 deletions

View File

@@ -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,14 +52,20 @@ 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>
</DialogContent>
</Dialog>
);
}
}

View File

@@ -1,36 +1,54 @@
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");
const options: RequestInit = {
method,
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
credentials: "include",
};
if (body) {
options.body = JSON.stringify(body);
}
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}${endpoint}`, options);
if (!res.ok) {
throw new Error(`Failed to ${method} ${endpoint}: ${res.statusText}`);
}
return await res.json();
} catch (error: unknown) {
if (error instanceof Error) {
console.error(`Error in API request: ${error.message}`);
throw new Error(error.message);
}
console.error("An unknown error occurred", error);
throw new Error("An unknown error occurred");
try {
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: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
credentials: "include",
};
if (body) {
options.body = JSON.stringify(body);
}
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) {
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(`🚨 API Request Error: ${error.message}`);
throw new Error(error.message);
}
console.error("❌ An unknown error occurred", error);
throw new Error("An unknown error occurred");
}
};