add edit notice and clean code
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user