Files
mohadese namavar ec84dfd222 init git
2024-06-16 00:22:14 +04:30

25 lines
787 B
JavaScript

export const useFavoritesStore = defineStore('favorites', {
state: () => {
return {
items: []
}
},
getters: {
find: (state) => (id) => state.items.find((item) => item.id == id)
},
actions: {
async update(id) {
const item = this.find(id)
let stat = item ? 'delete' : 'add'
const body = { favoriteProducts: id, stat }
const { status } = await useFetch(`/api/users/fave`, {
headers: { Authorization: useCookie('token') }, method: 'put', body
})
if (status.value == 'success') {
if (item) this.items = this.items.filter((item) => item.id != id)
else this.items.push({ id })
}
}
}
})