From a9fa327c9bafd8ba9952d6080a84a71ecd215a12 Mon Sep 17 00:00:00 2001 From: ChunkyProgrammer <78101139+ChunkyProgrammer@users.noreply.github.com> Date: Sun, 7 May 2023 08:06:23 -0400 Subject: [PATCH] Create Subscribe Button reusable component (#3468) * Create Subscribe Button component Co-Authored-By: Minh Hang Nguyen (Jenny) <55411709+minhhang107@users.noreply.github.com> * use defineComponent, remove unused prop Co-Authored-By: absidue <48293849+absidue@users.noreply.github.com> * readd unsubscribe method * add comment * Fix typo Co-authored-by: PikachuEXE * remove unused code * move json serialization --------- Co-authored-by: Minh Hang Nguyen (Jenny) <55411709+minhhang107@users.noreply.github.com> Co-authored-by: absidue <48293849+absidue@users.noreply.github.com> Co-authored-by: PikachuEXE --- .../ft-subscribe-button.js | 146 ++++++++++++++++++ .../ft-subscribe-button.scss | 6 + .../ft-subscribe-button.vue | 12 ++ .../watch-video-info/watch-video-info.js | 106 +------------ .../watch-video-info/watch-video-info.vue | 11 +- src/renderer/views/Channel/Channel.css | 7 - src/renderer/views/Channel/Channel.js | 86 +---------- src/renderer/views/Channel/Channel.vue | 10 +- .../SubscribedChannels/SubscribedChannels.css | 2 +- .../SubscribedChannels/SubscribedChannels.js | 62 +------- .../SubscribedChannels/SubscribedChannels.vue | 17 +- 11 files changed, 188 insertions(+), 277 deletions(-) create mode 100644 src/renderer/components/ft-subscribe-button/ft-subscribe-button.js create mode 100644 src/renderer/components/ft-subscribe-button/ft-subscribe-button.scss create mode 100644 src/renderer/components/ft-subscribe-button/ft-subscribe-button.vue diff --git a/src/renderer/components/ft-subscribe-button/ft-subscribe-button.js b/src/renderer/components/ft-subscribe-button/ft-subscribe-button.js new file mode 100644 index 000000000..15a6147c9 --- /dev/null +++ b/src/renderer/components/ft-subscribe-button/ft-subscribe-button.js @@ -0,0 +1,146 @@ +import { defineComponent } from 'vue' + +import { mapActions } from 'vuex' + +import FtButton from '../../components/ft-button/ft-button.vue' + +import { MAIN_PROFILE_ID } from '../../../constants' +import { showToast } from '../../helpers/utils' + +export default defineComponent({ + name: 'FtSubscribeButton', + components: { + 'ft-button': FtButton + }, + props: { + channelId: { + type: String, + required: true + }, + channelName: { + type: String, + required: true + }, + channelThumbnail: { + type: String, + required: true + }, + subscriptionCountText: { + default: '', + type: String, + required: false + } + }, + computed: { + profileList: function () { + return this.$store.getters.getProfileList + }, + + activeProfile: function () { + return this.$store.getters.getActiveProfile + }, + + subscriptionInfo: function () { + return this.activeProfile.subscriptions.find((channel) => { + return channel.id === this.channelId + }) ?? null + }, + + isSubscribed: function () { + return this.subscriptionInfo !== null + }, + + subscribedText: function () { + let subscribedValue = (this.isSubscribed ? this.$t('Channel.Unsubscribe') : this.$t('Channel.Subscribe')).toUpperCase() + if (this.subscriptionCountText !== '') { + subscribedValue += ' ' + this.subscriptionCountText + } + return subscribedValue + } + + }, + methods: { + handleSubscription: function () { + if (this.channelId === '') { + return + } + + const currentProfile = JSON.parse(JSON.stringify(this.activeProfile)) + + if (this.isSubscribed) { + currentProfile.subscriptions = currentProfile.subscriptions.filter((channel) => { + return channel.id !== this.channelId + }) + + this.updateProfile(currentProfile) + showToast(this.$t('Channel.Channel has been removed from your subscriptions')) + + if (this.activeProfile._id === MAIN_PROFILE_ID) { + // Check if a subscription exists in a different profile. + // Remove from there as well. + let duplicateSubscriptions = 0 + + this.profileList.forEach((profile) => { + if (profile._id === MAIN_PROFILE_ID) { + return + } + duplicateSubscriptions += this.unsubscribe(profile, this.channelId) + }) + + if (duplicateSubscriptions > 0) { + const message = this.$t('Channel.Removed subscription from {count} other channel(s)', { count: duplicateSubscriptions }) + showToast(message) + } + } + } else { + const subscription = { + id: this.channelId, + name: this.channelName, + thumbnail: this.channelThumbnail + } + currentProfile.subscriptions.push(subscription) + + this.updateProfile(currentProfile) + showToast(this.$t('Channel.Added channel to your subscriptions')) + + if (this.activeProfile._id !== MAIN_PROFILE_ID) { + const primaryProfile = JSON.parse(JSON.stringify(this.profileList.find(prof => { + return prof._id === MAIN_PROFILE_ID + }))) + + const index = primaryProfile.subscriptions.findIndex((channel) => { + return channel.id === this.channelId + }) + + if (index === -1) { + primaryProfile.subscriptions.push(subscription) + this.updateProfile(primaryProfile) + } + } + } + }, + + unsubscribe: function(profile, channelId) { + const parsedProfile = JSON.parse(JSON.stringify(profile)) + const index = parsedProfile.subscriptions.findIndex((channel) => { + return channel.id === channelId + }) + + if (index !== -1) { + // use filter instead of splice in case the subscription appears multiple times + // https://github.com/FreeTubeApp/FreeTube/pull/3468#discussion_r1179290877 + parsedProfile.subscriptions = parsedProfile.subscriptions.filter((x) => { + return x.id !== channelId + }) + + this.updateProfile(parsedProfile) + return 1 + } + return 0 + }, + + ...mapActions([ + 'updateProfile' + ]) + } +}) diff --git a/src/renderer/components/ft-subscribe-button/ft-subscribe-button.scss b/src/renderer/components/ft-subscribe-button/ft-subscribe-button.scss new file mode 100644 index 000000000..7aff23304 --- /dev/null +++ b/src/renderer/components/ft-subscribe-button/ft-subscribe-button.scss @@ -0,0 +1,6 @@ +.subscribeButton { + align-self: center; + height: 50%; + margin-bottom: 10px; + min-width: 150px; +} diff --git a/src/renderer/components/ft-subscribe-button/ft-subscribe-button.vue b/src/renderer/components/ft-subscribe-button/ft-subscribe-button.vue new file mode 100644 index 000000000..bf6d90d97 --- /dev/null +++ b/src/renderer/components/ft-subscribe-button/ft-subscribe-button.vue @@ -0,0 +1,12 @@ + + +