Piped/src/components/Channel.vue

118 lines
4.1 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-04-23 19:11:51 +00:00
<h1 class="uk-text-center"><img height="48" width="48" v-bind:src="channel.avatarUrl" />{{ channel.name }}</h1>
<img v-if="channel.bannerUrl" v-bind:src="channel.bannerUrl" style="width: 100%" loading="lazy" />
<p style="white-space: pre-wrap"><span v-html="purifyHTML(urlify(channel.description))"></span></p>
2020-11-27 06:46:36 +00:00
<button
v-if="authenticated"
@click="subscribeHandler"
class="uk-button uk-button-small"
style="background: #222"
type="button"
>
{{ subscribed ? "Unsubscribe" : "Subscribe" }}
</button>
2020-11-27 06:46:36 +00:00
<hr />
<div class="uk-grid-xl" uk-grid="parallax: 0">
<div
class="uk-width-1-2 uk-width-1-3@m uk-width-1-4@l uk-width-1-5@xl"
2021-06-16 19:14:46 +00:00
v-bind:key="video.url"
v-for="video in this.channel.relatedStreams"
2020-11-27 06:46:36 +00:00
>
2021-06-16 19:14:46 +00:00
<VideoItem :video="video" height="94" width="168" hideChannel />
2020-11-27 06:46:36 +00:00
</div>
</div>
</div>
</template>
<script>
import ErrorHandler from "@/components/ErrorHandler.vue";
2021-06-16 19:14:46 +00:00
import VideoItem from "@/components/VideoItem.vue";
2020-11-27 06:46:36 +00:00
export default {
data() {
return {
channel: null,
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);
},
deactivated() {
2020-11-27 06:46:36 +00:00
window.removeEventListener("scroll", this.handleScroll);
},
methods: {
async fetchSubscribedStatus() {
this.fetchJson(
this.apiUrl() + "/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";
if (this.authenticated) this.fetchSubscribedStatus();
}
});
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.relatedStreams.concat(json.relatedStreams);
this.channel.nextpage = json.nextpage;
this.loading = false;
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() {
this.fetchJson(this.apiUrl() + (this.subscribed ? "/unsubscribe" : "/subscribe"), null, {
method: "POST",
body: JSON.stringify({
channelId: this.channel.id,
}),
headers: {
Authorization: this.getAuthToken(),
"Content-Type": "application/json",
},
});
this.subscribed = !this.subscribed;
},
},
components: {
ErrorHandler,
2021-06-16 19:14:46 +00:00
VideoItem,
},
2020-11-27 06:46:36 +00:00
};
</script>