diff --git a/src/components/FollowButton.vue b/src/components/FollowButton.vue index 3728a0a..f7b7e65 100644 --- a/src/components/FollowButton.vue +++ b/src/components/FollowButton.vue @@ -16,26 +16,48 @@ export default { } }, methods: { - followStreamer() { + toggleFollow() { const username = this.$props.username - const follows = localStorage.getItem('following') || '[]' - let parsedFollows: string[] = JSON.parse(follows) - - if (follows?.includes(username)) { - const index = parsedFollows.indexOf(username) - console.log(index) - if (index === -1) return - parsedFollows.splice(index, 1) - console.log(parsedFollows) + if (this.isFollowing) { + this.unfollowStreamer(username) this.isFollowing = false } else { - if (follows) parsedFollows = JSON.parse(follows) - parsedFollows.push(username) + this.followStreamer(username) this.isFollowing = true } + }, + /** + * Follows a Streamer. + * @param username + * @returns true if streamer was followed, false if streamer was already followed. + */ + followStreamer(username: string): boolean { + const follows = localStorage.getItem('following') || '[]' + let parsedFollows: string[] = JSON.parse(follows) + if (parsedFollows.includes(username)) return false + + parsedFollows.push(username) localStorage.setItem('following', JSON.stringify(parsedFollows)) + return true + }, + + /** + * Unfollows a streamer. + * @param username + * @returns true if unfollowed, false if not followed. + */ + unfollowStreamer(username: string): boolean { + const follows = localStorage.getItem('following') || '[]' + let parsedFollows: string[] = JSON.parse(follows) + + const index = parsedFollows.indexOf(username) + if (index === -1) return false + parsedFollows.splice(index, 1) + localStorage.setItem('following', JSON.stringify(parsedFollows)) + + return true } }, mounted() { @@ -53,11 +75,8 @@ export default {