DEV: Add topic-vote behavior transformer

This commit is contained in:
Isaac Janzen 2024-08-14 12:08:44 -05:00
parent e6cb596c82
commit ac5e359fe4
No known key found for this signature in database
GPG Key ID: D75AF9C21FD8EBCD
2 changed files with 37 additions and 18 deletions

View File

@ -0,0 +1,12 @@
import { withPluginApi } from "discourse/lib/plugin-api";
export default {
name: "discourse-topic-voting-transformers",
before: "freeze-valid-transformers",
initialize() {
withPluginApi("1.35.0", (api) => {
api.addBehaviorTransformerName("topic-vote-button-click");
});
},
};

View File

@ -1,5 +1,6 @@
import { h } from "virtual-dom";
import cookie from "discourse/lib/cookie";
import { applyBehaviorTransformer } from "discourse/lib/transformer";
import { createWidget } from "discourse/widgets/widget";
import I18n from "I18n";
@ -71,24 +72,30 @@ export default createWidget("vote-button", {
},
click() {
if (!this.currentUser) {
this.sendWidgetAction("showLogin");
cookie("destination_url", window.location.href, { path: "/" });
return;
}
if (
!this.attrs.closed &&
this.parentWidget.state.allowClick &&
!this.attrs.user_voted &&
!this.currentUser.votes_exceeded
) {
this.parentWidget.state.allowClick = false;
this.parentWidget.state.initialVote = true;
this.sendWidgetAction("addVote");
}
if (this.attrs.user_voted || this.currentUser.votes_exceeded) {
document.querySelector(".vote-options").classList.toggle("hidden");
}
applyBehaviorTransformer(
"topic-vote-button-click",
() => {
if (!this.currentUser) {
this.sendWidgetAction("showLogin");
cookie("destination_url", window.location.href, { path: "/" });
return;
}
if (
!this.attrs.closed &&
this.parentWidget.state.allowClick &&
!this.attrs.user_voted &&
!this.currentUser.votes_exceeded
) {
this.parentWidget.state.allowClick = false;
this.parentWidget.state.initialVote = true;
this.sendWidgetAction("addVote");
}
if (this.attrs.user_voted || this.currentUser.votes_exceeded) {
document.querySelector(".vote-options").classList.toggle("hidden");
}
},
{ currentUser: this.currentUser, attrs: this.attrs }
);
},
clickOutside() {