From efcd1ff3cc277c440d3c6523637d07dfd919ffda Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 4 Oct 2022 11:45:14 +0200 Subject: Like a model --- pages/models/_id/index.vue | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'pages/models/_id/index.vue') diff --git a/pages/models/_id/index.vue b/pages/models/_id/index.vue index ab28187..abc9a34 100644 --- a/pages/models/_id/index.vue +++ b/pages/models/_id/index.vue @@ -86,6 +86,15 @@ p {{ model.description }} .mb-5.text-right + button.inline-flex.leading-6.justify-center.rounded-md.border.border-transparent.bg-white.py-2.px-4.mr-2.text-sm.font-medium.text-black.shadow-sm( + class="hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-gray-300 focus:ring-offset-2" + @click="toggleLike" + ) + svg(v-if="!likedByUser()" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6 mr-1") + + svg(v-else xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6 mr-1") + + | {{ model.likes ? model.likes.length : 0 }} button.inline-flex.leading-6.justify-center.rounded-md.border.border-transparent.bg-yellow-600.py-2.px-4.mr-2.text-sm.font-medium.text-white.shadow-sm( class="hover:bg-yellow-700 focus:outline-none focus:ring-2 focus:ring-yellow-500 focus:ring-offset-2" @click="boxReport = true" @@ -282,6 +291,54 @@ export default { } }); }, + likedByUser() { + if (!this.model.likes || !this.me) { + return false; + } + + return this.model.likes.filter((x) => x.user_id == this.me.id).length > 0; + }, + toggleLike() { + if (this.likedByUser()) { + this.$store + .dispatch("models/removeLike", this.model.id) + .then((response) => { + if (response.status == 204) { + this.$toast.success("Like removed!"); + this.$store + .dispatch("models/findModel", this.id) + .then((response) => { + if (response.status != 200) { + window.location.href = "/models"; + } else { + this.model = response.data; + } + }); + } else { + this.$toast.error(response.data.error); + } + }); + } else { + this.$store + .dispatch("models/addLike", this.model.id) + .then((response) => { + if (response.status == 201) { + this.$toast.success("Like added!"); + this.$store + .dispatch("models/findModel", this.id) + .then((response) => { + if (response.status != 200) { + window.location.href = "/models"; + } else { + this.model = response.data; + } + }); + } else { + this.$toast.error(response.data.error); + } + }); + } + }, }, }; -- cgit v1.2.3-18-g5258