Added store favourites

This commit is contained in:
Alexis
2021-03-19 23:03:54 +01:00
parent bb62d38e6b
commit e4c6861c06
3 changed files with 72 additions and 19 deletions

View File

@@ -42,6 +42,15 @@
</p> </p>
</div> </div>
<div class="card-info">
<router-link
:to="{ name: 'CelesteSingle', params: { slug: celestial.id } }"
class="no-style"
>
<span class="material-icons-round">info</span>
</router-link>
</div>
<div class="card-content"> <div class="card-content">
<div v-if="celestial.aroundPlanet"> <div v-if="celestial.aroundPlanet">
<p> <p>
@@ -75,12 +84,13 @@
</div> </div>
<div class="card-actions"> <div class="card-actions">
<router-link <button
:to="{ name: 'CelesteSingle', params: { slug: celestial.id } }" @click="toggleFav(celestial.id)"
class="btn btn-primary" class="favourite no-style"
:class="isFav ? 'active' : ''"
> >
Détails <span class="icon material-icons-round">favorite</span>
</router-link> </button>
</div> </div>
</div> </div>
</div> </div>
@@ -89,6 +99,8 @@
<script lang="ts"> <script lang="ts">
import { defineComponent } from "vue"; import { defineComponent } from "vue";
import store from "@/store";
export default defineComponent({ export default defineComponent({
name: "celestial-card", name: "celestial-card",
data() { data() {
@@ -96,8 +108,22 @@ export default defineComponent({
type: "unknown" type: "unknown"
}; };
}, },
computed: {
isFav() {
if (store.getters.isFav(this.celestial?.id)) {
return true;
}
return false;
}
},
props: { props: {
celestial: Object celestial: Object
},
methods: {
toggleFav: (celestialId: string) => {
store.dispatch("toggleFav", celestialId);
console.log(store.state.user.favourites);
}
} }
}); });
</script> </script>
@@ -121,8 +147,18 @@ export default defineComponent({
.card-icon { .card-icon {
display: block; display: block;
position: absolute; position: absolute;
top: 0; top: 20px;
bottom: 20px;
left: 0;
right: 0; right: 0;
user-select: none;
pointer-events: none;
opacity: 4%;
img {
height: 100%;
width: 100%;
z-index: -1;
}
} }
> * { > * {
@@ -135,6 +171,30 @@ export default defineComponent({
flex: 1 1 auto; flex: 1 1 auto;
min-height: 30px; min-height: 30px;
} }
.card-info {
position: absolute;
top: 0;
right: 0;
}
.card-actions {
color: #afafaf;
.favourite {
.icon {
color: #afafaf;
}
&.active {
.icon {
position: relative;
display: inline-block;
will-change: font-size;
animation: toggleFavHeart 0.6s cubic-bezier(0.17, 0.89, 0.32, 1.49);
animation-fill-mode: forwards;
}
}
}
}
} }
} }
</style> </style>

View File

@@ -3,6 +3,7 @@ import { createStore } from "vuex";
export default createStore({ export default createStore({
state: () => ({ state: () => ({
user: { user: {
avatarUrl: "",
favourites: Array<{ id: string }>() favourites: Array<{ id: string }>()
} }
}), }),
@@ -12,7 +13,7 @@ export default createStore({
return state.user.favourites.length; return state.user.favourites.length;
}, },
// Returns true if celestial is fav // Returns true if celestial is fav
isAlreadyFav: state => (celestialId: string) => { isFav: state => (celestialId: string) => {
return state.user.favourites.find(fav => fav.id === celestialId); return state.user.favourites.find(fav => fav.id === celestialId);
} }
}, },
@@ -21,13 +22,15 @@ export default createStore({
state.user.favourites.push({ id: celestialId }); state.user.favourites.push({ id: celestialId });
}, },
removeFav: (state, celestialId: string) => { removeFav: (state, celestialId: string) => {
state.user.favourites = state.user.favourites.filter(fav => fav.id != celestialId); state.user.favourites = state.user.favourites.filter(
fav => fav.id != celestialId
);
} }
}, },
actions: { actions: {
toggleFav: ({ commit, getters }, celestialId: string) => { toggleFav: ({ commit, getters }, celestialId: string) => {
// If the celestial is not faved // If the celestial is not faved
if (!getters.isAlreadyFav(celestialId)) { if (!getters.isFav(celestialId)) {
// ... favs it // ... favs it
commit("addFav", celestialId); commit("addFav", celestialId);
} else { } else {

View File

@@ -8,7 +8,6 @@
<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 />
@@ -19,8 +18,6 @@
<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"; // import { fetchWikipediaExcerpt } from "@/api/wikipedia";
@@ -65,13 +62,6 @@ export default defineComponent({
.then(() => { .then(() => {
// fetchWikipediaExcerpt(celestial); // fetchWikipediaExcerpt(celestial);
}); });
},
methods: {
toggleFav: (celestialId: string) => {
store.dispatch("toggleFav", celestialId);
console.log(store.state.user.favourites);
},
} }
}); });
</script> </script>