change and sync wishlist with api
This commit is contained in:
@@ -1,15 +1,38 @@
|
||||
import { create } from "zustand";
|
||||
import * as api from "@/api/wishlist";
|
||||
|
||||
export const useWishlist = create((set) => ({
|
||||
wishlistNotices: [],
|
||||
addNoticeToWishlist: (wishlistNotice) =>
|
||||
set((state) => ({
|
||||
wishlistNotices: [...state.wishlistNotices, wishlistNotice],
|
||||
})),
|
||||
removeNoticeFromWishlist: (noticeId) =>
|
||||
set((state) => ({
|
||||
wishlistNotices: state.wishlistNotices.filter(
|
||||
(item) => item.noticeId !== noticeId
|
||||
),
|
||||
})),
|
||||
toggleNoticeInWishlist: async (noticeId) => {
|
||||
try {
|
||||
await api.toggleNoticeStatus(noticeId);
|
||||
set((state) => {
|
||||
const exists = state.wishlistNotices.some(
|
||||
(item) => item.noticeId == noticeId
|
||||
);
|
||||
return exists
|
||||
? {
|
||||
wishlistNotices: state.wishlistNotices.filter(
|
||||
(item) => item.noticeId != noticeId
|
||||
),
|
||||
}
|
||||
: {
|
||||
wishlistNotices: [
|
||||
...state.wishlistNotices,
|
||||
{ noticeId },
|
||||
],
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error toggling wishlist notice:", error);
|
||||
}
|
||||
},
|
||||
fetchWishlist: async () => {
|
||||
try {
|
||||
const data = await api.getWishlist();
|
||||
set({ wishlistNotices: data });
|
||||
} catch (error) {
|
||||
console.error("Error fetching wishlist:", error);
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user