2021-05-29 23:54:18 +00:00
|
|
|
<template>
|
2021-07-09 22:33:23 +00:00
|
|
|
<v-tooltip bottom nudge-right="50" :color="buttonStyle ? 'info' : 'secondary'">
|
2021-08-02 03:24:47 +00:00
|
|
|
<template #activator="{ on, attrs }">
|
2021-06-12 06:59:14 +00:00
|
|
|
<v-btn
|
|
|
|
v-if="isFavorite || showAlways"
|
2021-08-02 03:24:47 +00:00
|
|
|
small
|
2021-07-09 22:33:23 +00:00
|
|
|
:color="buttonStyle ? 'info' : 'secondary'"
|
2021-06-12 06:59:14 +00:00
|
|
|
:icon="!buttonStyle"
|
|
|
|
:fab="buttonStyle"
|
|
|
|
v-bind="attrs"
|
2021-08-02 03:24:47 +00:00
|
|
|
@click.prevent="toggleFavorite"
|
2021-06-12 06:59:14 +00:00
|
|
|
v-on="on"
|
|
|
|
>
|
2021-07-09 22:33:23 +00:00
|
|
|
<v-icon :small="!buttonStyle" :color="buttonStyle ? 'white' : 'secondary'">
|
2021-06-17 02:55:32 +00:00
|
|
|
{{ isFavorite ? $globals.icons.heart : $globals.icons.heartOutline }}
|
2021-06-12 06:59:14 +00:00
|
|
|
</v-icon>
|
|
|
|
</v-btn>
|
|
|
|
</template>
|
|
|
|
<span>{{ isFavorite ? $t("recipe.remove-from-favorites") : $t("recipe.add-to-favorites") }}</span>
|
|
|
|
</v-tooltip>
|
2021-05-29 23:54:18 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import { api } from "@/api";
|
2021-08-04 05:38:45 +00:00
|
|
|
import { defineComponent } from "@nuxtjs/composition-api";
|
|
|
|
import { useApiSingleton } from "~/composables/use-api";
|
|
|
|
export default defineComponent({
|
2021-05-29 23:54:18 +00:00
|
|
|
props: {
|
|
|
|
slug: {
|
2021-08-02 03:24:47 +00:00
|
|
|
type: String,
|
2021-05-29 23:54:18 +00:00
|
|
|
default: "",
|
|
|
|
},
|
|
|
|
showAlways: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
buttonStyle: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
},
|
2021-08-04 05:38:45 +00:00
|
|
|
setup() {
|
|
|
|
const api = useApiSingleton();
|
|
|
|
|
|
|
|
return { api };
|
|
|
|
},
|
2021-05-29 23:54:18 +00:00
|
|
|
computed: {
|
|
|
|
user() {
|
2021-08-03 06:15:11 +00:00
|
|
|
return this.$auth.user;
|
2021-05-29 23:54:18 +00:00
|
|
|
},
|
|
|
|
isFavorite() {
|
2021-08-03 06:15:11 +00:00
|
|
|
return this.$auth.user.favoriteRecipes.includes(this.slug);
|
2021-05-29 23:54:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
async toggleFavorite() {
|
|
|
|
if (!this.isFavorite) {
|
2021-08-04 05:38:45 +00:00
|
|
|
await this.api.users.addFavorite(this.$auth.user.id, this.slug);
|
2021-05-29 23:54:18 +00:00
|
|
|
} else {
|
2021-08-04 05:38:45 +00:00
|
|
|
await this.api.users.removeFavorite(this.$auth.user.id, this.slug);
|
2021-05-29 23:54:18 +00:00
|
|
|
}
|
2021-08-04 05:38:45 +00:00
|
|
|
this.$auth.fetchUser();
|
2021-05-29 23:54:18 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-04 05:38:45 +00:00
|
|
|
});
|
2021-05-29 23:54:18 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
</style>
|