From 7018e264b2c1c395b8a73b3d5ea851e0b50f8c89 Mon Sep 17 00:00:00 2001 From: dragongoose Date: Sun, 2 Jun 2024 10:48:35 -0400 Subject: [PATCH] Clean up follow button --- src/components/FollowButton.vue | 53 ++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 17 deletions(-) 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 {