Piped/src/components/ChannelPage.vue

140 lines
4.8 KiB
Vue
Raw Normal View History

2020-11-27 06:46:36 +00:00
<template>
<ErrorHandler v-if="channel && channel.error" :message="channel.message" :error="channel.error" />
<div v-if="channel" v-show="!channel.error">
2021-12-27 14:46:30 +00:00
<div class="flex justify-center place-items-center">
<img height="48" width="48" class="rounded-full m-1" :src="channel.avatarUrl" />
2021-12-27 16:29:25 +00:00
<h1 v-text="channel.name" />
<font-awesome-icon class="ml-1.5 !text-3xl" v-if="channel.verified" icon="check" />
2021-12-27 14:46:30 +00:00
</div>
<img v-if="channel.bannerUrl" :src="channel.bannerUrl" class="w-full pb-1.5" loading="lazy" />
<!-- eslint-disable-next-line vue/no-v-html -->
<p class="whitespace-pre-wrap">
2021-12-27 22:33:55 +00:00
<span v-html="purifyHTML(urlify(channel.description))" />
</p>
2020-11-27 06:46:36 +00:00
2021-12-27 16:29:25 +00:00
<button
class="btn"
@click="subscribeHandler"
v-t="{
path: `actions.${subscribed ? 'unsubscribe' : 'subscribe'}`,
args: { count: numberFormat(channel.subscriberCount) },
}"
></button>
2020-11-27 06:46:36 +00:00
<hr />
2021-12-27 14:46:22 +00:00
<div class="video-grid">
<VideoItem
v-for="video in channel.relatedStreams"
:key="video.url"
:video="video"
height="94"
width="168"
hide-channel
/>
2020-11-27 06:46:36 +00:00
</div>
</div>
</template>
<script>
2022-04-08 15:46:49 +00:00
import ErrorHandler from "./ErrorHandler.vue";
import VideoItem from "./VideoItem.vue";
2020-11-27 06:46:36 +00:00
export default {
components: {
ErrorHandler,
VideoItem,
},
2020-11-27 06:46:36 +00:00
data() {
return {
channel: null,
2022-08-02 08:41:35 +00:00
subscribed: false,
2020-11-27 06:46:36 +00:00
};
},
mounted() {
this.getChannelData();
},
activated() {
if (this.channel && !this.channel.error) document.title = this.channel.name + " - Piped";
2020-11-27 06:46:36 +00:00
window.addEventListener("scroll", this.handleScroll);
if (this.channel && !this.channel.error) this.updateWatched(this.channel.relatedStreams);
2020-11-27 06:46:36 +00:00
},
deactivated() {
2020-11-27 06:46:36 +00:00
window.removeEventListener("scroll", this.handleScroll);
},
unmounted() {
window.removeEventListener("scroll", this.handleScroll);
},
2020-11-27 06:46:36 +00:00
methods: {
async fetchSubscribedStatus() {
2022-08-02 08:41:35 +00:00
if (!this.channel.id) return;
if (!this.authenticated) {
this.subscribed = this.isSubscribedLocally(this.channel.id);
return;
}
this.fetchJson(
this.authApiUrl() + "/subscribed",
{
channelId: this.channel.id,
},
{
headers: {
Authorization: this.getAuthToken(),
},
},
).then(json => {
this.subscribed = json.subscribed;
});
},
2020-11-27 06:46:36 +00:00
async fetchChannel() {
const url = this.apiUrl() + "/" + this.$route.params.path + "/" + this.$route.params.channelId;
2021-05-28 18:40:54 +00:00
return await this.fetchJson(url);
2020-11-27 06:46:36 +00:00
},
async getChannelData() {
this.fetchChannel()
.then(data => (this.channel = data))
.then(() => {
if (!this.channel.error) {
document.title = this.channel.name + " - Piped";
2022-08-02 08:41:35 +00:00
this.fetchSubscribedStatus();
this.updateWatched(this.channel.relatedStreams);
}
});
2020-11-27 06:46:36 +00:00
},
handleScroll() {
if (this.loading || !this.channel || !this.channel.nextpage) return;
if (window.innerHeight + window.scrollY >= document.body.offsetHeight - window.innerHeight) {
2020-11-27 06:46:36 +00:00
this.loading = true;
this.fetchJson(this.apiUrl() + "/nextpage/channel/" + this.channel.id, {
nextpage: this.channel.nextpage,
}).then(json => {
2021-02-24 09:35:41 +00:00
this.channel.nextpage = json.nextpage;
this.loading = false;
this.updateWatched(json.relatedStreams);
json.relatedStreams.map(stream => this.channel.relatedStreams.push(stream));
2021-02-24 09:35:41 +00:00
});
2020-11-27 06:46:36 +00:00
}
},
subscribeHandler() {
if (this.authenticated) {
this.fetchJson(this.authApiUrl() + (this.subscribed ? "/unsubscribe" : "/subscribe"), null, {
method: "POST",
body: JSON.stringify({
channelId: this.channel.id,
}),
headers: {
Authorization: this.getAuthToken(),
"Content-Type": "application/json",
},
});
} else {
this.handleLocalSubscriptions(this.channel.id);
}
this.subscribed = !this.subscribed;
},
},
2020-11-27 06:46:36 +00:00
};
</script>