Update the store helper and broadcast dialog
This commit is contained in:
@@ -14,10 +14,6 @@ interface BroadcastDialogProps {
|
|||||||
setOpen: (open: boolean) => void;
|
setOpen: (open: boolean) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface BroadcastMessage {
|
|
||||||
message: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function BroadcastDialog({ open, setOpen }: BroadcastDialogProps) {
|
export default function BroadcastDialog({ open, setOpen }: BroadcastDialogProps) {
|
||||||
const [broadcastMessage, setBroadcastMessage] = useState<string>("");
|
const [broadcastMessage, setBroadcastMessage] = useState<string>("");
|
||||||
const [sendingBroadcast, setSendingBroadcast] = useState<boolean>(false);
|
const [sendingBroadcast, setSendingBroadcast] = useState<boolean>(false);
|
||||||
@@ -31,8 +27,15 @@ export default function BroadcastDialog({ open, setOpen }: BroadcastDialogProps)
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
setSendingBroadcast(true);
|
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("");
|
setBroadcastMessage("");
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -49,10 +52,16 @@ export default function BroadcastDialog({ open, setOpen }: BroadcastDialogProps)
|
|||||||
<Send className="h-6 w-6 text-emerald-600" />
|
<Send className="h-6 w-6 text-emerald-600" />
|
||||||
<DialogTitle>Global Broadcast Message</DialogTitle>
|
<DialogTitle>Global Broadcast Message</DialogTitle>
|
||||||
</DialogHeader>
|
</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>
|
<DialogFooter>
|
||||||
<Button onClick={() => setOpen(false)}>Cancel</Button>
|
<Button onClick={() => setOpen(false)} variant="secondary">
|
||||||
<Button onClick={sendBroadcast} disabled={sendingBroadcast}>
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button onClick={sendBroadcast} disabled={sendingBroadcast} className="bg-emerald-600 hover:bg-emerald-700">
|
||||||
{sendingBroadcast ? "Sending..." : "Send Broadcast"}
|
{sendingBroadcast ? "Sending..." : "Send Broadcast"}
|
||||||
</Button>
|
</Button>
|
||||||
</DialogFooter>
|
</DialogFooter>
|
||||||
|
|||||||
@@ -1,36 +1,54 @@
|
|||||||
export const apiRequest = async <T = any>(endpoint: string, method: string = "GET", body?: T | null) => {
|
export const apiRequest = async <T = any>(endpoint: string, method: string = "GET", body?: T | null) => {
|
||||||
try {
|
try {
|
||||||
const authToken = document.cookie.split("; ").find(row => row.startsWith("Authorization="))?.split("=")[1];
|
if (typeof document === "undefined") {
|
||||||
if (!authToken) throw new Error("No authentication token found");
|
throw new Error("API requests must be made from the client side.");
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user