Skip to content

Commit 8d233de

Browse files
committed
fixed name of announcement toggle
fixed toggle and shuffle moved data transformation to custom hook
1 parent dc339a4 commit 8d233de

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

client/src/components/Admin/Announcements.jsx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,7 @@ const Announcements = () => {
4747
} = useAnnouncements();
4848

4949
useEffect(() => {
50-
if (announcementsData) {
51-
const newRows = announcementsData.map((announcement) => ({
52-
announcementId: announcement.id,
53-
title: announcement.title,
54-
description: announcement.description,
55-
is_enabled: announcement.is_enabled,
56-
}));
57-
setRows(newRows);
58-
}
50+
setRows(announcementsData || []);
5951
}, [announcementsData]);
6052

6153
const handleModalClose = () => {
@@ -73,12 +65,17 @@ const Announcements = () => {
7365

7466
const handleIsEnabled = async (announcementId, isEnabled) => {
7567
try {
76-
await announcementService.update(announcementId, { is_enabled: isEnabled });
77-
setRows((prevRows) =>
78-
prevRows.map((row) =>
79-
row.announcementId === announcementId ? { ...row, is_enabled: isEnabled } : row
80-
)
68+
const announcement = rows.find(
69+
(row) => row.announcementId === announcementId
8170
);
71+
if (!announcement) throw new Error("Announcement not found");
72+
73+
await announcementService.update(announcementId, {
74+
title: announcement.title,
75+
description: announcement.description,
76+
is_enabled: isEnabled,
77+
});
78+
await announcementRefetch();
8279
} catch (error) {
8380
console.error("Error updating announcement:", error);
8481
}
@@ -174,7 +171,7 @@ const Announcements = () => {
174171
<TableCell align="left"> Announcement ID </TableCell>
175172
<TableCell align="left">Announcement Title</TableCell>
176173
<TableCell align="left">Announcement Description</TableCell>
177-
<TableCell align="left">Is Enabled?</TableCell>
174+
<TableCell align="left">Enabled</TableCell>
178175
<TableCell />
179176
</TableRow>
180177
</TableHead>
@@ -311,7 +308,7 @@ const Announcements = () => {
311308
onChange={announcementFormik.handleChange}
312309
/>
313310
}
314-
label="Globally Enable"
311+
label="Enabled"
315312
/>
316313
</Box>
317314
<Box mt={3} display="flex" justifyContent="space-between">
@@ -381,7 +378,7 @@ const Announcements = () => {
381378
onChange={editFormik.handleChange}
382379
/>
383380
}
384-
label="Globally Enable"
381+
label="Enabled"
385382
/>
386383
</Box>
387384
<Box mt={3} display="flex" justifyContent="space-between">

client/src/hooks/useAnnouncements.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@ export const useAnnouncements = () => {
1111
setLoading(true);
1212
try {
1313
const announcements = await announcementService.getAllAnnouncements();
14-
setData(announcements);
14+
// Transform and sort here
15+
const processed = announcements
16+
.slice()
17+
.sort((a, b) => a.id - b.id)
18+
.map((announcement) => ({
19+
announcementId: announcement.id,
20+
title: announcement.title,
21+
description: announcement.description,
22+
is_enabled: announcement.is_enabled,
23+
}));
24+
setData(processed);
1525
setLoading(false);
1626
} catch (err) {
1727
setError(err);

0 commit comments

Comments
 (0)