A platform for community discussion. Free, open, simple.
DEV: Update the topic voting UX to modern standards used elsewhere within the app (#35917)
Jordan Vidrine95f7c3cf8d40
diff --git a/app/assets/stylesheets/common/components/dropdown-menu.scss b/app/assets/stylesheets/common/components/dropdown-menu.scssindex 768551a9254..29b8330e638 100644--- a/app/assets/stylesheets/common/components/dropdown-menu.scss+++ b/app/assets/stylesheets/common/components/dropdown-menu.scss@@ -16,6 +16,7 @@:focus-visible {background: var(--danger-low);}+ color: var(--danger);.d-icon {color: var(--danger);diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-box.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-box.gjsindex fb02ef475e8..71d1b1123f5 100644--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-box.gjs+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-box.gjs@@ -2,16 +2,12 @@ import Component from "@glimmer/component";import { tracked } from "@glimmer/tracking";import { action } from "@ember/object";import { service } from "@ember/service";-import { htmlSafe } from "@ember/template";import concatClass from "discourse/helpers/concat-class";import routeAction from "discourse/helpers/route-action";import { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";-import closeOnClickOutside from "discourse/modifiers/close-on-click-outside";-import { i18n } from "discourse-i18n";import VoteButton from "./vote-button";import VoteCount from "./vote-count";-import VoteOptions from "./vote-options";export default class VoteBox extends Component {@service siteSettings;@@ -20,7 +16,6 @@ export default class VoteBox extends Component {@tracked votesAlert;@tracked allowClick = true;@tracked initialVote = false;- @tracked showOptions = false;@actionaddVote() {@@ -38,7 +33,6 @@ export default class VoteBox extends Component {this.currentUser.votes_left = result.votes_left;this.votesAlert = result.alert;this.allowClick = true;- this.showOptions = false;}).catch(popupAjaxError);}@@ -59,21 +53,10 @@ export default class VoteBox extends Component {this.currentUser.votes_exceeded = !result.can_vote;this.currentUser.votes_left = result.votes_left;this.allowClick = true;- this.showOptions = false;}).catch(popupAjaxError);}- @action- showVoteOptions() {- this.showOptions = true;- }-- @action- closeVoteOptions() {- this.showOptions = false;- }-@actioncloseVotesAlert() {this.votesAlert = null;@@ -93,30 +76,9 @@ export default class VoteBox extends Component {@showVoteOptions={{this.showVoteOptions}}@addVote={{this.addVote}}@showLogin={{routeAction "showLogin"}}+ @removeVote={{this.removeVote}}/>- {{#if this.showOptions}}- <VoteOptions- @topic={{@topic}}- @removeVote={{this.removeVote}}- {{closeOnClickOutside this.closeVoteOptions}}- />- {{/if}}-- {{#if this.votesAlert}}- <div- class="voting-popup-menu vote-options popup-menu"- {{closeOnClickOutside this.closeVotesAlert}}- >- {{htmlSafe- (i18n- "topic_voting.votes_left"- count=this.currentUser.votes_left- path="/my/activity/votes"- )- }}- </div>- {{/if}}</div></template>}diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-button.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-button.gjsindex 4398a486ed6..4994675ba2c 100644--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-button.gjs+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-button.gjs@@ -1,101 +1,190 @@import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";import { action } from "@ember/object";import { service } from "@ember/service";+import { and, eq, not } from "truth-helpers";import DButton from "discourse/components/d-button";+import DropdownMenu from "discourse/components/dropdown-menu";+import icon from "discourse/helpers/d-icon";import { applyBehaviorTransformer } from "discourse/lib/transformer";import { i18n } from "discourse-i18n";+import DMenu from "float-kit/components/d-menu";export default class VoteBox extends Component {- @service siteSettings;@service currentUser;- get wrapperClasses() {- const classes = [];- const { topic } = this.args;- if (topic.closed) {- classes.push("voting-closed");- } else {- if (!topic.user_voted) {- classes.push("nonvote");- } else {- if (this.currentUser && this.currentUser.votes_exceeded) {- classes.push("vote-limited nonvote");- } else {- classes.push("vote");- }- }- }- if (this.siteSettings.topic_voting_show_who_voted) {- classes.push("show-pointer");- }- return classes.join(" ");- }+ @tracked hasVoted = false;+ @tracked hasSeenSuccessMenu = false;+ topic = this.args.topic;++ alreadyVoted = this.topic.user_voted;get buttonContent() {- const { topic } = this.args;+ const content = {};if (this.currentUser) {- if (topic.closed) {- return i18n("topic_voting.voting_closed_title");- }-- if (topic.user_voted) {- return i18n("topic_voting.voted_title");- }-- if (this.currentUser.votes_exceeded) {- return i18n("topic_voting.voting_limit");+ if (this.topic.closed) {+ content.label = i18n("topic_voting.voting_closed_title");+ content.title = i18n("topic_voting.voting_closed_title");+ } else if (this.topic.user_voted) {+ content.label = i18n("topic_voting.voted_title");+ content.title = i18n("topic_voting.voted_title");+ } else if (this.currentUser.vote_limit === 0) {+ content.label = i18n("topic_voting.locked");+ content.title = i18n("topic_voting.locked_description");+ } else if (this.currentUser.votes_exceeded) {+ content.label = i18n("topic_voting.voting_limit");+ content.title = i18n("topic_voting.reached_limit");+ } else {+ content.label = i18n("topic_voting.vote_title");+ content.title = i18n("topic_voting.vote_title");}-- return i18n("topic_voting.vote_title");+ } else {+ content.label = i18n("topic_voting.anonymous_button", { count: 1 });+ content.title = i18n("topic_voting.anonymous_button", { count: 1 });}- if (topic.vote_count) {- return i18n("topic_voting.anonymous_button", {- count: topic.vote_count,- });- }+ return content;+ }++ get showVotedMenu() {+ return this.hasVoted && !this.hasSeenSuccessMenu;+ }- return i18n("topic_voting.anonymous_button", { count: 1 });+ get buttonClasses() {+ return this.currentUser.vote_limit === 0+ ? "btn-default vote-button"+ : "btn-primary vote-button";}@action- click() {+ onShowMenu() {applyBehaviorTransformer("topic-vote-button-click", () => {if (!this.currentUser) {return this.args.showLogin();}- const { topic } = this.args;+ if (this.currentUser.vote_limit === 0) {+ return;+ }++ // If user has already voted and seen the success menu, don't do anything+ // The menu will show with the "remove vote" option+ if (this.topic.user_voted && this.hasSeenSuccessMenu) {+ return;+ }+ // If user hasn't voted yet, add vote and show success menuif (- !topic.closed &&- !topic.user_voted &&+ !this.topic.closed &&+ !this.topic.user_voted &&!this.currentUser.votes_exceeded) {this.args.addVote();- }-- if (topic.user_voted || this.currentUser.votes_exceeded) {- this.args.showVoteOptions();+ this.hasVoted = true;+ // Don't set hasSeenSuccessMenu yet - it will be set when menu closes}});}+ @action+ addVote() {+ this.args.addVote();+ this.hasVoted = true;+ }++ @action+ removeVote() {+ this.args.removeVote();+ this.hasVoted = false;+ this.hasSeenSuccessMenu = false;+ this.dMenu.close();+ }++ @action+ onRegisterApi(api) {+ this.dMenu = api;+ }++ @action+ onCloseMenu() {+ if (this.hasVoted && !this.hasSeenSuccessMenu) {+ this.hasSeenSuccessMenu = true;+ }+ }+<template>- <div class={{this.wrapperClasses}}>- <DButton- @translatedTitle={{if- this.currentUser- (i18n- "topic_voting.votes_left_button_title"- count=this.currentUser.votes_left- )- ""- }}- @translatedLabel={{this.buttonContent}}- class="btn-primary vote-button"- @action={{this.click}}- />- </div>+ <DMenu+ @identifier="topic-voting-menu"+ @title={{this.buttonContent.title}}+ @label={{this.buttonContent.label}}+ @onShow={{this.onShowMenu}}+ @onClose={{this.onCloseMenu}}+ class={{this.buttonClasses}}+ @onRegisterApi={{this.onRegisterApi}}+ >+ <:content>+ <DropdownMenu as |dropdown|>+ {{#if this.showVotedMenu}}+ <dropdown.item class="topic-voting-menu__title">+ {{icon "circle-check"}}+ <span>{{i18n "topic_voting.voted_title"}}</span>+ </dropdown.item>+ <dropdown.item class="topic-voting-menu__votes-left">+ <DButton+ @translatedLabel={{i18n+ "topic_voting.see_votes"+ count=this.currentUser.votes_left+ max=this.currentUser.vote_limit+ }}+ @href="/my/activity/votes"+ @icon="check-to-slot"+ class="btn-transparent see-votes topic-voting-menu__row-btn"+ />+ </dropdown.item>+ {{else if (eq this.currentUser.vote_limit 0)}}+ <dropdown.item class="topic-voting-menu__title --locked">+ {{icon "lock"}}+ <span>{{i18n "topic_voting.locked_description"}}</span>+ </dropdown.item>+ {{else if+ (and this.currentUser.votes_exceeded (not this.topic.user_voted))+ }}+ <dropdown.item class="topic-voting-menu__row">+ <DButton+ @translatedLabel={{i18n+ "topic_voting.see_votes"+ count=this.currentUser.votes_left+ max=this.currentUser.vote_limit+ }}+ @href="/my/activity/votes"+ @icon="check-to-slot"+ class="btn-transparent see-votes topic-voting-menu__row-btn"+ />+ </dropdown.item>+ {{else}}+ <dropdown.item class="topic-voting-menu__row">+ <DButton+ @translatedLabel={{i18n+ "topic_voting.see_votes"+ count=this.currentUser.votes_left+ max=this.currentUser.vote_limit+ }}+ @href="/my/activity/votes"+ @icon="check-to-slot"+ class="btn-transparent see-votes topic-voting-menu__row-btn"+ />+ </dropdown.item>+ <dropdown.item class="topic-voting-menu__row">+ <DButton+ @translatedLabel={{i18n "topic_voting.remove_vote"}}+ @action={{this.removeVote}}+ @icon="arrow-rotate-left"+ class="btn-transparent remove-vote topic-voting-menu__row-btn --danger"+ />+ </dropdown.item>+ {{/if}}+ </DropdownMenu>+ </:content>+ </DMenu></template>}diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-options.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-options.gjsdeleted file mode 100644index d634c16396f..00000000000--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-options.gjs+++ /dev/null@@ -1,29 +0,0 @@-import Component from "@glimmer/component";-import { on } from "@ember/modifier";-import { service } from "@ember/service";-import icon from "discourse/helpers/d-icon";-import { i18n } from "discourse-i18n";--export default class VoteBox extends Component {- @service currentUser;-- <template>- <div class="vote-options voting-popup-menu popup-menu" ...attributes>- {{#if @topic.user_voted}}- <div- role="button"- class="remove-vote vote-option"- {{on "click" @removeVote}}- >- {{icon "xmark"}}- {{i18n "topic_voting.remove_vote"}}- </div>- {{else if this.currentUser.votes_exceeded}}- <div>{{i18n "topic_voting.reached_limit"}}</div>- <p>- <a href="/my/activity/votes">{{i18n "topic_voting.list_votes"}}</a>- </p>- {{/if}}- </div>- </template>-}diff --git a/plugins/discourse-topic-voting/assets/stylesheets/common/topic-voting.scss b/plugins/discourse-topic-voting/assets/stylesheets/common/topic-voting.scssindex 8fcef181d97..d32a4cf6f9c 100644--- a/plugins/discourse-topic-voting/assets/stylesheets/common/topic-voting.scss+++ b/plugins/discourse-topic-voting/assets/stylesheets/common/topic-voting.scss@@ -93,3 +93,19 @@right: 80px;}}++.topic-voting-menu__title {+ display: flex;+ align-items: center;+ gap: 0.75em;+ background: var(--tertiary-low);+ color: var(--tertiary);+ padding: 0.65rem 1rem;+ font-weight: bold;+}++.topic-voting-menu__title.--locked {+ background: var(--secondary);+ color: var(--primary-medium);+ font-weight: normal;+}diff --git a/plugins/discourse-topic-voting/config/locales/client.en.yml b/plugins/discourse-topic-voting/config/locales/client.en.ymlindex ac996c9838a..1af4ba0c457 100755--- a/plugins/discourse-topic-voting/config/locales/client.en.yml+++ b/plugins/discourse-topic-voting/config/locales/client.en.yml@@ -15,12 +15,9 @@ en:voted_title: "Voted"voting_closed_title: "Closed"voting_limit: "Limit"- votes_left:- one: "You have %{count} vote left, see <a href='%{path}'>your votes</a>."- other: "You have %{count} votes left, see <a href='%{path}'>your votes</a>."- votes_left_button_title:- one: "You have %{count} vote left."- other: "You have %{count} votes left."+ see_votes: "You have %{count}/%{max} votes left"+ locked: "Locked"+ locked_description: "Increase your trust level to vote"votes:one: "%{count} vote"other: "%{count} votes"diff --git a/plugins/discourse-topic-voting/lib/discourse_topic_voting/user_extension.rb b/plugins/discourse-topic-voting/lib/discourse_topic_voting/user_extension.rbindex c3db1c2d390..7134243cfd7 100644--- a/plugins/discourse-topic-voting/lib/discourse_topic_voting/user_extension.rb+++ b/plugins/discourse-topic-voting/lib/discourse_topic_voting/user_extension.rb@@ -29,5 +29,9 @@ module DiscourseTopicVotingdef vote_limitSiteSetting.public_send("topic_voting_tl#{self.trust_level}_vote_limit")end++ def vote_limit_0?+ vote_limit == 0+ endendenddiff --git a/plugins/discourse-topic-voting/plugin.rb b/plugins/discourse-topic-voting/plugin.rbindex c4cc63c7568..9a318a6fb9c 100755--- a/plugins/discourse-topic-voting/plugin.rb+++ b/plugins/discourse-topic-voting/plugin.rb@@ -11,6 +11,8 @@ register_asset "stylesheets/common/topic-voting.scss"register_asset "stylesheets/desktop/topic-voting.scss", :desktopregister_asset "stylesheets/mobile/topic-voting.scss", :mobile+register_svg_icon "check-to-slot"+enabled_site_setting :topic_voting_enabledDiscourse.top_menu_items.push(:votes)@@ -120,6 +122,7 @@ after_initialize doadd_to_serializer(:current_user, :votes_exceeded) { object.reached_voting_limit? }add_to_serializer(:current_user, :votes_count) { object.vote_count }add_to_serializer(:current_user, :votes_left) { [object.vote_limit - object.vote_count, 0].max }+ add_to_serializer(:current_user, :vote_limit) { object.vote_limit }filter_order_votes = ->(scope, order_direction, _guardian) doscope.joins(:topic_vote_count).order(diff --git a/plugins/discourse-topic-voting/spec/system/page_objects/pages/topic.rb b/plugins/discourse-topic-voting/spec/system/page_objects/pages/topic.rbindex 9113579ed9c..43d4befcb33 100644--- a/plugins/discourse-topic-voting/spec/system/page_objects/pages/topic.rb+++ b/plugins/discourse-topic-voting/spec/system/page_objects/pages/topic.rb@@ -7,19 +7,16 @@ module TopicVotingTopicfind(".voting .vote-count")end- def has_votes_left_popup?(count)- expected_html =- I18n- .t("js.topic_voting.votes_left", count:, path: "/my/activity/votes")- .gsub(/'([^']*)'/) { "\"#{$1}\"" }- selector = ".voting-popup-menu"+ def has_locked_popup?+ expected_html = I18n.t("js.topic_voting.locked_description")+ selector = ".topic-voting-menu__title.--locked"has_css?(selector)actual_html = find(selector)[:innerHTML].stripactual_html.include?(expected_html)enddef vote_popup- find(".voting-popup-menu")+ find(".topic-voting-menu__votes-left")enddef vote@@ -29,12 +26,12 @@ module TopicVotingTopicdef remove_votevote- find(".remove-vote").click+ find("button.remove-vote").clickselfend- def click_vote_popup_activity- find(".voting-popup-menu a").click+ def click_my_votes+ find(".topic-voting-menu__votes-left").clickendenddiff --git a/plugins/discourse-topic-voting/spec/system/voting_spec.rb b/plugins/discourse-topic-voting/spec/system/voting_spec.rbindex a8836bf7a81..2057067fa1f 100644--- a/plugins/discourse-topic-voting/spec/system/voting_spec.rb+++ b/plugins/discourse-topic-voting/spec/system/voting_spec.rb@@ -5,9 +5,14 @@ RSpec.describe "Topic voting", type: :system dofab!(:admin) { Fabricate(:admin, trust_level: TrustLevel[4]) }fab!(:category1, :category)fab!(:category2, :category)+ fab!(:voting_category) { Fabricate(:category, name: "voting category") }fab!(:topic1) { Fabricate(:topic, category: category1) }fab!(:topic2) { Fabricate(:topic, category: category1) }fab!(:topic3) { Fabricate(:topic, category: category2) }+ fab!(:voting_topic1) { Fabricate(:topic, category: voting_category) }+ fab!(:voting_topic2) { Fabricate(:topic, category: voting_category) }+ fab!(:voting_topic3) { Fabricate(:topic, category: voting_category) }+ fab!(:voting_topic4) { Fabricate(:topic, category: voting_category) }fab!(:post1) { Fabricate(:post, topic: topic1) }fab!(:post2) { Fabricate(:post, topic: topic2) }@@ -41,11 +46,13 @@ RSpec.describe "Topic voting", type: :system doexpect(topic_page.vote_count).to have_text("0")topic_page.vote- expect(topic_page.vote_popup).to have_text("You have 9 votes left, see your votes")+ expect(topic_page.vote_popup).to have_text(+ I18n.t("js.topic_voting.see_votes", count: 9, max: 10),+ )expect(topic_page.vote_count).to have_text("1")# visit user activity page- topic_page.click_vote_popup_activity+ topic_page.click_my_votesexpect(user_page.active_user_primary_navigation).to have_text("Activity")expect(user_page.active_user_secondary_navigation).to have_text("Votes")expect(page).to have_css(".topic-list-body tr[data-topic-id=\"#{topic1.id}\"]", text: "1 vote")@@ -66,7 +73,9 @@ RSpec.describe "Topic voting", type: :system docategory_page.visit(category1).select_topic(topic1)topic_page.vote- expect(topic_page).to have_votes_left_popup(0)+ expect(topic_page.vote_popup).to have_text(+ I18n.t("js.topic_voting.see_votes", count: 0, max: 1),+ )endendenddiff --git a/plugins/discourse-topic-voting/spec/voting_spec.rb b/plugins/discourse-topic-voting/spec/voting_spec.rbindex 86cf4da78a7..302f6c4b4e8 100644--- a/plugins/discourse-topic-voting/spec/voting_spec.rb+++ b/plugins/discourse-topic-voting/spec/voting_spec.rb@@ -30,6 +30,14 @@ describe DiscourseTopicVoting doexpect(user0.reached_voting_limit?).to eq(true)end+ it "doesn't allow users to vote if their trust level vote limit is 0" do+ SiteSetting.topic_voting_tl1_vote_limit = 0+ user0.update!(trust_level: 1)++ expect(user0.vote_limit_0?).to eq(true)+ expect(user0.reached_voting_limit?).to eq(true)+ end+context "with two topics" dolet(:users) { [user0, user1, user2, user3, user4, user5] }