I have a dilemma with my component that allows users to add or remove an item from their favorites. While the functionality works smoothly, there is no visual indication for the user to know whether the item has been added to favorites or not. Currently, I display a filled star icon if the item is favorited and an empty one if it is not. How can I make this interaction reactive? I want the icon to change its appearance when clicked on by the user.
Below is a snippet of my code:
Component:
<template>
<span :class="isFavorite
? 'mdi mdi-star favorite'
: 'mdi mdi-star-outline'"
class="favorite-item-icon"
@click="isFavorite ? deleteFromFavoriteItems(itemId) : addToFavoriteItems(itemId)">
</span>
</template>
<script>
export default {
import { mapGetters, mapActions } from 'vuex';
props: ['itemId', 'isFavorite'],
methods: {
...mapActions(['addToFavoriteItems', 'deleteFromFavoriteItems']),
},
};
</script>
The component within a v-for loop list in the parent component:
...
<favorite-label :itemId="item.id" :isFavorite="item.is_favourite" />
...
In my Vuex store:
addToFavoriteItems({ commit }, itemId) {
http
.post(`${config.api}api/v1/items/favourite-save`, {
item_id: itemId,
});
deleteFromFavoriteItems({ commit }, itemId) {
http
.post(`${config.api}api/v1/items/favourite-delete`, {
item_id: itemId,
});