Add, list, getbyid
working via zustand now + adding image from camera
This commit is contained in:
41
ArtisanConnect/store/noticesStore.jsx
Normal file
41
ArtisanConnect/store/noticesStore.jsx
Normal file
@@ -0,0 +1,41 @@
|
||||
import {create} from "zustand";
|
||||
import * as api from "@/api/notices";
|
||||
|
||||
export const useNoticesStore = create((set, get) => ({
|
||||
notices: [],
|
||||
fetchNotices: async () => {
|
||||
set({error: null});
|
||||
try {
|
||||
const data = await api.listNotices();
|
||||
set({notices: data});
|
||||
} catch (error) {
|
||||
set(error);
|
||||
}
|
||||
},
|
||||
|
||||
addNotice: async (notice) => {
|
||||
try {
|
||||
const newNotice = await api.createNotice(notice);
|
||||
set((state) => ({
|
||||
notices: [...state.notices, newNotice],
|
||||
}));
|
||||
return newNotice;
|
||||
} catch (error) {
|
||||
set({ error });
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
getNoticeById: (noticeId) => {
|
||||
return get().notices.find((notice) => String(notice.noticeId) === String(noticeId));
|
||||
},
|
||||
|
||||
getAllImagesByNoticeId: async (noticeId) => {
|
||||
try {
|
||||
return await api.getAllImagesByNoticeId(noticeId);
|
||||
} catch (error) {
|
||||
console.error("Error while getting images:", error);
|
||||
return ["https://http.cat/404.jpg"];
|
||||
}
|
||||
}
|
||||
}));
|
||||
@@ -9,7 +9,7 @@ export const useWishlist = create((set) => ({
|
||||
removeNoticeFromWishlist: (noticeId) =>
|
||||
set((state) => ({
|
||||
wishlistNotices: state.wishlistNotices.filter(
|
||||
(item) => item.noticeId != noticeId
|
||||
(item) => item.noticeId !== noticeId
|
||||
),
|
||||
})),
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user