Skip to content

Commit

Permalink
DEV: Add topic-vote click transformer for theme/plugin extensions (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
janzenisaac authored Aug 14, 2024
1 parent e6dbd1f commit 4f10bf1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
12 changes: 12 additions & 0 deletions assets/javascripts/discourse/pre-initializers/transformers.js
Original file line number Diff line number Diff line change
@@ -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");
});
},
};
39 changes: 21 additions & 18 deletions assets/javascripts/discourse/widgets/vote-button.js
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -71,24 +72,26 @@ 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");
}
});
},

clickOutside() {
Expand Down

0 comments on commit 4f10bf1

Please sign in to comment.