diff --git a/src/store/index.ts b/src/store/index.ts index 187b4cb..765e2f0 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,8 +1,40 @@ import { createStore } from "vuex"; export default createStore({ - state: {}, - mutations: {}, - actions: {}, + state: () => ({ + user: { + favourites: Array<{ id: string }>() + } + }), + getters: { + // Returns the number of favs for the user + favCount: (state): number => { + return state.user.favourites.length; + }, + // Returns true if celestial is fav + isAlreadyFav: state => (celestialId: string) => { + return state.user.favourites.find(fav => fav.id === celestialId); + } + }, + mutations: { + addFav: (state, celestialId: string) => { + state.user.favourites.push({ id: celestialId }); + }, + removeFav: (state, celestialId: string) => { + state.user.favourites = state.user.favourites.filter(fav => fav.id != celestialId); + } + }, + actions: { + toggleFav: ({ commit, getters }, celestialId: string) => { + // If the celestial is not faved + if (!getters.isAlreadyFav(celestialId)) { + // ... favs it + commit("addFav", celestialId); + } else { + // ...else unfavs + commit("removeFav", celestialId); + } + } + }, modules: {} }); diff --git a/src/views/celestials/Celestial.vue b/src/views/celestials/Celestial.vue index 576553e..50cb7cc 100644 --- a/src/views/celestials/Celestial.vue +++ b/src/views/celestials/Celestial.vue @@ -1,6 +1,6 @@