mirror of
https://git.sr.ht/~cadence/cloudtube
synced 2024-11-25 08:57:31 +00:00
29 lines
667 B
JavaScript
29 lines
667 B
JavaScript
import {ElemJS} from "/static/js/elemjs/elemjs.js"
|
|
|
|
class SubscribeButton extends ElemJS {
|
|
constructor(element) {
|
|
super(element)
|
|
this.subscribed = this.element.getAttribute("data-subscribed") === "1"
|
|
this.ucid = this.element.getAttribute("data-ucid")
|
|
this.on("click", this.onClick.bind(this))
|
|
this.render()
|
|
}
|
|
|
|
onClick(event) {
|
|
event.preventDefault()
|
|
this.subscribed = !this.subscribed
|
|
const path = this.subscribed ? "subscribe" : "unsubscribe"
|
|
fetch(`/formapi/${path}/${this.ucid}`, {method: "POST"})
|
|
this.render()
|
|
}
|
|
|
|
render() {
|
|
if (!this.subscribed) this.text("Subscribe")
|
|
else this.text("Unsubscribe")
|
|
}
|
|
}
|
|
|
|
export {
|
|
SubscribeButton
|
|
}
|