Skip to content

A platform for community discussion. Free, open, simple.

UX: Use FormKit for the gamification recalculate scores modal (#44956)

David Battersby8ba30fd88839
diff --git a/plugins/discourse-gamification/assets/javascripts/discourse/components/modal/recalculate-scores-form.gjs b/plugins/discourse-gamification/assets/javascripts/discourse/components/modal/recalculate-scores-form.gjs
index 82eeab5a7ec..a154b20b60f 100644
--- a/plugins/discourse-gamification/assets/javascripts/discourse/components/modal/recalculate-scores-form.gjs
+++ b/plugins/discourse-gamification/assets/javascripts/discourse/components/modal/recalculate-scores-form.gjs
@@ -1,52 +1,41 @@
/* eslint-disable ember/no-tracked-properties-from-args */
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
-import { fn } from "@ember/helper";
import { action } from "@ember/object";
import { service } from "@ember/service";
-import DatePickerPast from "discourse/components/date-picker-past";
+import Form from "discourse/components/form";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { bind } from "discourse/lib/decorators";
-import ComboBox from "discourse/select-kit/components/combo-box";
import { eq } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
import DModal from "discourse/ui-kit/d-modal";
import dIcon from "discourse/ui-kit/helpers/d-icon";
import { i18n } from "discourse-i18n";
+const ALL_TIME_FROM_DATE = "2014-08-26";
+const RANGES = {
+ last_10_days: { count: 10, type: "days" },
+ last_30_days: { count: 30, type: "days" },
+ last_90_days: { count: 90, type: "days" },
+ last_year: { count: 1, type: "year" },
+ all_time: null,
+ custom_date_range: null,
+};
+
export default class RecalculateScoresForm extends Component {
@service messageBus;
- @tracked updateRangeValue = 0;
- @tracked recalculateFromDate = "";
@tracked remaining = this.args.model.recalculate_scores_remaining;
@tracked status = "initial";
- updateRange = [
- {
- name: i18n("gamification.update_range.last_10_days"),
- value: 0,
- calculation: { count: 10, type: "days" },
- },
- {
- name: i18n("gamification.update_range.last_30_days"),
- value: 1,
- calculation: { count: 30, type: "days" },
- },
- {
- name: i18n("gamification.update_range.last_90_days"),
- value: 2,
- calculation: { count: 90, type: "days" },
- },
- {
- name: i18n("gamification.update_range.last_year"),
- value: 3,
- calculation: { count: 1, type: "year" },
- },
- { name: i18n("gamification.update_range.all_time"), value: 4 },
- { name: i18n("gamification.update_range.custom_date_range"), value: 5 },
- ];
+ formData = { range: "last_10_days", from_date: null };
+
+ rangeOptions = Object.keys(RANGES).map((key) => ({
+ value: key,
+ name: i18n(`gamification.update_range.${key}`),
+ }));
+ #formApi;
constructor() {
super(...arguments);
@@ -65,27 +54,18 @@ export default class RecalculateScoresForm extends Component {
}
get applyDisabled() {
- const haveAvailability = this.args.model.recalculate_scores_remaining > 0;
- if (!haveAvailability || this.status !== "initial") {
- return true;
- }
-
return (
- this.updateRangeValue === 5 &&
- this.recalculateFromDate <= moment().locale("en").utc().endOf("day")
+ this.args.model.recalculate_scores_remaining <= 0 ||
+ this.status !== "initial"
);
}
- get dateRange() {
- if (this.updateRangeValue === 4) {
- return;
- }
+ get today() {
+ return moment().locale("en").utc().endOf("day");
+ }
- let today = moment().locale("en").utc().endOf("day");
- let pastDate = this.dateRangeToDate(this.updateRangeValue);
- return `${pastDate} - ${today.format(
- i18n("dates.long_with_year_no_time")
- )}`;
+ get maxFromDate() {
+ return this.today.format("YYYY-MM-DD");
}
@bind
@@ -98,37 +78,58 @@ export default class RecalculateScoresForm extends Component {
}
@bind
- dateRangeToDate(updateRangeValue) {
- if (updateRangeValue === 4) {
- return "2014-8-26";
+ dateRangeText(range) {
+ const calculation = RANGES[range];
+ if (!calculation) {
+ return;
}
- if (updateRangeValue === 5) {
- return this.recalculateFromDate;
+ const format = i18n("dates.long_with_year_no_time");
+ const pastDate = this.today
+ .clone()
+ .subtract(calculation.count, calculation.type);
+ return `${pastDate.format(format)} - ${this.today.format(format)}`;
+ }
+
+ fromDate(data) {
+ if (data.range === "all_time") {
+ return ALL_TIME_FROM_DATE;
}
- let today = moment().locale("en").utc().endOf("day");
- let updateRange = this.updateRange.find((obj) => {
- return obj.value === updateRangeValue;
- });
- let pastDate = today
+ if (data.range === "custom_date_range") {
+ return data.from_date;
+ }
+
+ const calculation = RANGES[data.range];
+ return this.today
.clone()
- .subtract(updateRange.calculation.count, updateRange.calculation.type);
+ .subtract(calculation.count, calculation.type)
+ .format("YYYY-MM-DD");
+ }
+
+ @action
+ registerFormApi(api) {
+ this.#formApi = api;
+ }
- return pastDate.format(i18n("dates.long_with_year_no_time"));
+ @action
+ submit() {
+ this.#formApi.submit();
}
@action
- apply() {
+ async recalculate(data) {
this.status = "loading";
- const data = {
- from_date: this.dateRangeToDate(this.updateRangeValue),
- };
-
- return ajax(`/admin/plugins/gamification/recalculate-scores.json`, {
- data,
- type: "PUT",
- }).catch(popupAjaxError);
+
+ try {
+ await ajax(`/admin/plugins/gamification/recalculate-scores.json`, {
+ data: { from_date: this.fromDate(data) },
+ type: "PUT",
+ });
+ } catch (error) {
+ this.status = "initial";
+ popupAjaxError(error);
+ }
}
<template>
@@ -147,52 +148,57 @@ export default class RecalculateScoresForm extends Component {
{{i18n "gamification.completed"}}
</div>
{{else}}
- <form class="form-horizontal">
- <div class="input-group">
- <label>{{i18n "gamification.update_scores_help"}}</label>
- <ComboBox
- @content={{this.updateRange}}
- @id="update-range"
- @onChange={{fn (mut this.updateRangeValue)}}
- @value={{this.updateRangeValue}}
- @valueProperty="value"
- />
-
- {{#if (eq this.updateRangeValue 5)}}
- <div class="input-group -custom-range">
- <label>{{i18n "gamification.custom_range_from"}}</label>
- <DatePickerPast
- class="date-input"
- @id="custom-from-date"
- @onSelect={{fn (mut this.recalculateFromDate)}}
- @placeholder="yyyy-mm-dd"
- @value={{this.recalculateFromDate}}
- />
- </div>
- {{else}}
- <div class="recalculate-modal__date-range">
- {{this.dateRange}}
- </div>
- {{/if}}
- </div>
- </form>
+ <Form
+ class="recalculate-scores-form"
+ @data={{this.formData}}
+ @onRegisterApi={{this.registerFormApi}}
+ @onSubmit={{this.recalculate}}
+ as |form transientData|
+ >
+ <form.Field
+ @description={{this.dateRangeText transientData.range}}
+ @format="full"
+ @name="range"
+ @title={{i18n "gamification.update_scores_help"}}
+ @type="select"
+ @validation="required"
+ as |field|
+ >
+ <field.Control as |select|>
+ {{#each this.rangeOptions as |option|}}
+ <select.Option @value={{option.value}}>
+ {{option.name}}
+ </select.Option>
+ {{/each}}
+ </field.Control>
+ </form.Field>
+
+ {{#if (eq transientData.range "custom_date_range")}}
+ <form.Field
+ @format="full"
+ @name="from_date"
+ @title={{i18n "gamification.custom_range_from"}}
+ @type="input-date"
+ @validation="required"
+ as |field|
+ >
+ <field.Control max={{this.maxFromDate}} />
+ </form.Field>
+ {{/if}}
+ </Form>
{{/if}}
</:body>
<:footer>
<DButton
- class="btn-primary"
- id="apply-section"
- @action={{this.apply}}
- @ariaLabel="gamification.apply"
+ class="btn-primary recalculate-modal__submit"
+ @action={{this.submit}}
@disabled={{this.applyDisabled}}
- @label="gamification.apply"
+ @label="gamification.recalculate"
/>
<DButton
class="btn-default"
- id="cancel-section"
@action={{@closeModal}}
- @ariaLabel="gamification.cancel"
@label={{if
(eq this.status "complete")
"gamification.close"
diff --git a/plugins/discourse-gamification/assets/stylesheets/common/leaderboard.scss b/plugins/discourse-gamification/assets/stylesheets/common/leaderboard.scss
index 213f2ae414b..ed94b66cd1f 100644
--- a/plugins/discourse-gamification/assets/stylesheets/common/leaderboard.scss
+++ b/plugins/discourse-gamification/assets/stylesheets/common/leaderboard.scss
@@ -270,35 +270,12 @@
}
.recalculate-scores-form-modal {
- .-custom-range {
- display: flex;
- align-items: baseline;
- margin-top: 0.5rem;
-
- > * {
- flex: 1 1 0;
- }
-
- .date-picker-wrapper {
- margin-left: auto;
- }
-
- .date-picker {
- max-width: 400px;
- }
- }
-
.recalculate-modal__status.is-success .d-icon-check {
color: $success;
}
}
.recalculate-modal {
- &__date-range {
- margin-top: 0.5rem;
- color: var(--primary-high);
- }
-
&__footer-text {
margin-left: auto;
color: var(--primary-high);
diff --git a/plugins/discourse-gamification/config/locales/client.en.yml b/plugins/discourse-gamification/config/locales/client.en.yml
index ebeafa82a5c..9edcbd66cf2 100644
--- a/plugins/discourse-gamification/config/locales/client.en.yml
+++ b/plugins/discourse-gamification/config/locales/client.en.yml
@@ -71,7 +71,7 @@ en:
chat_reaction_given: "Chat reaction given"
chat_reaction_received: "Chat reaction received"
period:
- all_time: "All Time"
+ all_time: "All time"
yearly: "Yearly"
quarterly: "Quarterly"
monthly: "Monthly"
@@ -85,7 +85,6 @@ en:
edit: "Edit"
back_to_leaderboards: "Back to leaderboards"
save: "Save"
- apply: "Apply"
recalculate: "Recalculate scores"
recalculating: "Recalculating scores..."
completed: "Done! The scores have been successfully recalculated."
@@ -95,7 +94,7 @@ en:
last_30_days: "Last 30 days"
last_90_days: "Last 90 days"
last_year: "Last year"
- all_time: "All Time"
+ all_time: "All time"
custom_date_range: "Custom range"
custom_range_from: "From"
daily_update_scores_availability:
diff --git a/plugins/discourse-gamification/spec/system/page_objects/modals/recalculate_scores_form.rb b/plugins/discourse-gamification/spec/system/page_objects/modals/recalculate_scores_form.rb
index 0d6295ef267..52f405c66bd 100644
--- a/plugins/discourse-gamification/spec/system/page_objects/modals/recalculate_scores_form.rb
+++ b/plugins/discourse-gamification/spec/system/page_objects/modals/recalculate_scores_form.rb
@@ -3,25 +3,32 @@
module PageObjects
module Modals
class RecalculateScoresForm < PageObjects::Modals::Base
- def update_range_dropdown
- PageObjects::Components::SelectKit.new("#update-range")
+ def form
+ PageObjects::Components::FormKit.new(".recalculate-scores-form")
end
- def select_update_range(value: nil)
- update_range_dropdown.expand
- update_range_dropdown.select_row_by_value(value)
+ def select_update_range(range)
+ form.field("range").select(range)
+ end
+
+ def date_range
+ form.field("range").component.find(".form-kit__container-description")
+ end
+
+ def has_no_date_range?
+ form.field("range").component.has_no_css?(".form-kit__container-description")
end
def fill_since_date(since)
- find("#custom-from-date").fill_in(with: since)
+ form.field("from_date").fill_in(since)
end
- def date_range
- find(".recalculate-modal__date-range")
+ def has_since_date_field?
+ form.has_field_with_name?("from_date")
end
- def custom_since_date
- find("#custom-from-date input")
+ def has_no_since_date_field?
+ form.has_no_field_with_name?("from_date")
end
def status
@@ -32,8 +39,12 @@ module PageObjects
find(".recalculate-modal__footer-text")
end
- def apply
- find("#apply-section")
+ def submit_button
+ find(".recalculate-modal__submit")
+ end
+
+ def has_disabled_submit_button?
+ has_css?(".recalculate-modal__submit[disabled]")
end
end
end
diff --git a/plugins/discourse-gamification/spec/system/recalculate_scores_form_spec.rb b/plugins/discourse-gamification/spec/system/recalculate_scores_form_spec.rb
index 144c8f20565..7f2f835c79c 100644
--- a/plugins/discourse-gamification/spec/system/recalculate_scores_form_spec.rb
+++ b/plugins/discourse-gamification/spec/system/recalculate_scores_form_spec.rb
@@ -17,7 +17,7 @@ describe "Recalculate Scores Form" do
date.midnight.strftime("%b %-d, %Y")
end
- it "has date options that are valid and can be applied" do
+ it "shows the date range for each preset" do
freeze_time
visit("/admin/plugins/gamification")
@@ -25,26 +25,20 @@ describe "Recalculate Scores Form" do
today = format_date(Time.now)
- recalculate_scores_modal.select_update_range(value: 0)
expect(recalculate_scores_modal.date_range.text).to eq("#{format_date(10.days.ago)} - #{today}")
- recalculate_scores_modal.select_update_range(value: 1)
+ recalculate_scores_modal.select_update_range("last_30_days")
expect(recalculate_scores_modal.date_range.text).to eq("#{format_date(30.days.ago)} - #{today}")
- recalculate_scores_modal.select_update_range(value: 2)
+ recalculate_scores_modal.select_update_range("last_90_days")
expect(recalculate_scores_modal.date_range.text).to eq("#{format_date(90.days.ago)} - #{today}")
- recalculate_scores_modal.select_update_range(value: 3)
+ recalculate_scores_modal.select_update_range("last_year")
expect(recalculate_scores_modal.date_range.text).to eq("#{format_date(1.year.ago)} - #{today}")
- recalculate_scores_modal.select_update_range(value: 4)
- expect(recalculate_scores_modal.date_range.text).to eq("")
-
- recalculate_scores_modal.select_update_range(value: 5)
- expect(recalculate_scores_modal.custom_since_date).to be_visible
-
- recalculate_scores_modal.fill_since_date(today)
- expect(recalculate_scores_modal.custom_since_date.value).to eq(today)
+ recalculate_scores_modal.select_update_range("all_time")
+ expect(recalculate_scores_modal).to have_no_date_range
+ expect(recalculate_scores_modal).to have_no_since_date_field
end
context "when admin has daily recalculation remaining" do
@@ -52,25 +46,47 @@ describe "Recalculate Scores Form" do
visit("/admin/plugins/gamification")
find(".leaderboard-admin__btn-recalculate").click
- recalculate_scores_modal.apply.click
+ recalculate_scores_modal.submit_button.click
expect(recalculate_scores_modal.status).to have_content(
I18n.t("js.gamification.recalculating"),
)
- expect(recalculate_scores_modal).to have_button("apply-section", disabled: true)
+ expect(recalculate_scores_modal).to have_disabled_submit_button
+
+ expect(Jobs::RecalculateScores.jobs.count).to eq(1)
+ end
+
+ it "requires a date for a custom range" do
+ visit("/admin/plugins/gamification")
+ find(".leaderboard-admin__btn-recalculate").click
+ recalculate_scores_modal.select_update_range("custom_date_range")
+ expect(recalculate_scores_modal).to have_since_date_field
+
+ recalculate_scores_modal.submit_button.click
+ expect(recalculate_scores_modal.form.field("from_date")).to have_errors(
+ I18n.t("js.form_kit.errors.required"),
+ )
+ expect(Jobs::RecalculateScores.jobs.count).to eq(0)
+
+ recalculate_scores_modal.fill_since_date(5.days.ago.to_date)
+ recalculate_scores_modal.submit_button.click
+
+ expect(recalculate_scores_modal.status).to have_content(
+ I18n.t("js.gamification.recalculating"),
+ )
expect(Jobs::RecalculateScores.jobs.count).to eq(1)
end
end
context "when admin does not have daily recalculation remaining" do
- it "disables the 'apply' button" do
+ it "disables the submit button" do
5.times { DiscourseGamification::RecalculateScoresRateLimiter.perform! }
visit("/admin/plugins/gamification")
find(".leaderboard-admin__btn-recalculate").click
- expect(recalculate_scores_modal).to have_button("apply-section", disabled: true)
+ expect(recalculate_scores_modal).to have_disabled_submit_button
end
end
end