Skip to content

Commit 56e9f8c

Browse files
committed
Adding Delete option in the Notice Board View for Admins
1 parent 762dec2 commit 56e9f8c

1 file changed

Lines changed: 51 additions & 14 deletions

File tree

src/components/Admin/NoticeBoardView.tsx

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,36 @@ export default function NoticeBoardView(): JSX.Element {
7272
fetchAnnouncements();
7373
}, []);
7474

75+
const handleDelete = async (id: string) => {
76+
const confirmed = window.confirm(
77+
"Are you sure you want to delete this announcement?"
78+
);
79+
80+
if (!confirmed) return;
81+
82+
try {
83+
const response = await fetch(
84+
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/v1/notification/${id}`,
85+
{
86+
method: "DELETE",
87+
}
88+
);
89+
90+
const data = await response.json();
91+
92+
if (!response.ok) {
93+
throw new Error(data.message || "Delete failed");
94+
}
95+
96+
setNotifications((prev) =>
97+
prev.filter((item) => item.id !== id)
98+
);
99+
100+
alert("Announcement deleted successfully.");
101+
} catch (err: any) {
102+
alert(err.message || "Delete failed.");
103+
}
104+
};
75105
if (loading) {
76106
return (
77107
<div className="w-full bg-slate-50 flex items-center justify-center p-6">
@@ -143,20 +173,27 @@ export default function NoticeBoardView(): JSX.Element {
143173
)}
144174
</div>
145175

146-
{item.announcelogo && (
147-
<div className="md:w-32 flex-shrink-0">
148-
<img
149-
src={item.announcelogo}
150-
alt="Announcement logo"
151-
onClick={() =>
152-
setSelectedImage(
153-
item.announcelogo
154-
)
155-
}
156-
className="h-24 w-24 rounded-xl border border-slate-200 object-cover cursor-pointer hover:scale-105 transition"
157-
/>
158-
</div>
159-
)}
176+
<div className="flex flex-col items-end gap-3">
177+
{item.announcelogo && (
178+
<div className="md:w-32 flex-shrink-0">
179+
<img
180+
src={item.announcelogo}
181+
alt="Announcement logo"
182+
onClick={() =>
183+
setSelectedImage(item.announcelogo)
184+
}
185+
className="h-24 w-24 rounded-xl border border-slate-200 object-cover cursor-pointer hover:scale-105 transition"
186+
/>
187+
</div>
188+
)}
189+
190+
<button
191+
onClick={() => handleDelete(item.id!)}
192+
className="rounded-lg bg-red-600 px-4 py-2 text-sm font-semibold text-white hover:bg-red-700 transition"
193+
>
194+
Delete
195+
</button>
196+
</div>
160197
</div>
161198
</div>
162199
))}

0 commit comments

Comments
 (0)