safetwitch/src/components/FollowButton.vue

66 lines
1.6 KiB
Vue
Raw Normal View History

2023-03-31 07:50:46 -04:00
<script lang="ts">
import { ref } from 'vue'
export default {
props: {
username: {
type: String,
default() {
return ''
}
}
},
setup() {
return {
isFollowing: ref(false)
}
},
methods: {
followStreamer() {
const username = this.$props.username
const follows = localStorage.getItem('following') || "[]"
2023-03-31 07:50:46 -04:00
let parsedFollows: string[] = JSON.parse(follows)
if (follows?.includes(username)) {
const index = parsedFollows.indexOf(username)
console.log(index)
2023-03-31 07:50:46 -04:00
if (index === -1) return
parsedFollows = parsedFollows.splice(index, 1)
console.log(parsedFollows)
2023-03-31 07:50:46 -04:00
this.isFollowing = false
} else {
if (follows) parsedFollows = JSON.parse(follows)
parsedFollows.push(username)
this.isFollowing = true
}
localStorage.setItem('following', JSON.stringify(parsedFollows))
}
},
mounted() {
let followerData = localStorage.getItem('following')
if (!followerData) return
let following: string[] = JSON.parse(followerData)
const isFollower = following.includes(this.$props.username)
if (isFollower) {
this.isFollowing = true
}
}
}
</script>
<template>
<button
ref="followButton"
@click="followStreamer"
2023-09-24 11:47:48 -04:00
class="text-white text-sm font-bold p-2 py-1 rounded-md bg-purple"
2023-03-31 07:50:46 -04:00
>
<v-icon name="bi-heart-fill" scale="0.85"></v-icon>
2023-07-20 13:57:01 -04:00
<span v-if="isFollowing"> {{ $t('streamer.unfollow') }} </span>
<span v-else> {{ $t('streamer.follow') }} </span>
2023-03-31 07:50:46 -04:00
</button>
</template>