add edit notice and clean code

This commit is contained in:
Patryk
2025-06-11 20:56:39 +02:00
parent b323f02654
commit 3c042d2cfb
7 changed files with 118 additions and 67 deletions

View File

@@ -26,6 +26,34 @@ export const useNoticesStore = create((set, get) => ({
}
},
editNotice: async (noticeId, notice) => {
try {
if (notice.image.length > 0 && typeof notice.image[0] == "string") {
const currentImages = await api.getAllImagesByNoticeId(noticeId);
if (currentImages && currentImages.length > 0) {
for (const image of currentImages) {
const filename = image.uri
? image.uri.split("/").pop()
: image.split("/").pop();
await api.deleteImage(filename);
}
}
}
const updatedNotice = await api.editNotice(noticeId, notice);
set((state) => ({
notices: state.notices.map((n) =>
n.noticeId == noticeId ? updatedNotice : n
),
}));
return updatedNotice;
} catch (error) {
console.error("Error editing notice:", error);
set({ error });
throw error;
}
},
getNoticeById: (noticeId) => {
return get().notices.find(
(notice) => String(notice.noticeId) === String(noticeId)