Added store functions to add a favourite
This commit is contained in:
@@ -1,8 +1,40 @@
|
|||||||
import { createStore } from "vuex";
|
import { createStore } from "vuex";
|
||||||
|
|
||||||
export default createStore({
|
export default createStore({
|
||||||
state: {},
|
state: () => ({
|
||||||
mutations: {},
|
user: {
|
||||||
actions: {},
|
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: {}
|
modules: {}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<section class="celestial bg-image">
|
<section class="celestial bg-image">
|
||||||
<div v-if="loaded">
|
<div v-if="celestialLoaded">
|
||||||
<header>
|
<header>
|
||||||
<h1 class="heading-1">{{ celestial.name }}</h1>
|
<h1 class="heading-1">{{ celestial.name }}</h1>
|
||||||
</header>
|
</header>
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
<div v-if="error">Une erreur est survenue : {{ error }}</div>
|
<div v-if="error">Une erreur est survenue : {{ error }}</div>
|
||||||
<div>{{ celestial }}</div>
|
<div>{{ celestial }}</div>
|
||||||
</div>
|
</div>
|
||||||
|
<button @click="toggleFav(celestial.id)">Favourite</button>
|
||||||
</div>
|
</div>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
<nest-loader />
|
<nest-loader />
|
||||||
@@ -18,8 +19,11 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { defineComponent } from "vue";
|
import { defineComponent } from "vue";
|
||||||
|
|
||||||
|
import store from "@/store";
|
||||||
|
|
||||||
// API
|
// API
|
||||||
import { fetchCelestial } from "@/api/le-systeme-solaire";
|
import { fetchCelestial } from "@/api/le-systeme-solaire";
|
||||||
|
// import { fetchWikipediaExcerpt } from "@/api/wikipedia";
|
||||||
|
|
||||||
// Global methods
|
// Global methods
|
||||||
import { addCelestialType } from "@/plugins/methods";
|
import { addCelestialType } from "@/plugins/methods";
|
||||||
@@ -33,8 +37,9 @@ export default defineComponent({
|
|||||||
|
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
celestial: false,
|
celestial: {},
|
||||||
loaded: false,
|
celestialLoaded: false,
|
||||||
|
excerpt: "",
|
||||||
error: ""
|
error: ""
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
@@ -50,15 +55,23 @@ export default defineComponent({
|
|||||||
// Fetches from API...
|
// Fetches from API...
|
||||||
fetchCelestial(this.slug)
|
fetchCelestial(this.slug)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
// Adds type after fake loading (it's just to showcase the spinner tbh)
|
this.celestial = addCelestialType(res);
|
||||||
setTimeout(() => {
|
this.celestialLoaded = true;
|
||||||
this.celestial = addCelestialType(res);
|
return this.celestial;
|
||||||
this.loaded = true;
|
|
||||||
}, 1000);
|
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
this.error = "Impossible de récupérer l'astre demandé.";
|
this.error = "Impossible de récupérer l'astre demandé.";
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
// fetchWikipediaExcerpt(celestial);
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
methods: {
|
||||||
|
toggleFav: (celestialId: string) => {
|
||||||
|
store.dispatch("toggleFav", celestialId);
|
||||||
|
console.log(store.state.user.favourites);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user