From c2e759b1c5f9dec0b31c0111f2d0413aea7c9e47 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 5 Oct 2022 17:14:43 +0200 Subject: [PATCH] FIX: pagination and submitted_at ordering of submissions --- .../components/wizard-table-field.js.es6 | 6 +-- .../admin-wizards-submissions-show.js.es6 | 11 ++++-- .../discourse/lib/wizard-submission.js.es6 | 37 +++++++++++++++++++ .../models/custom-wizard-admin.js.es6 | 5 ++- .../admin-wizards-submissions-show.js.es6 | 37 +------------------ .../components/wizard-table-field.hbs | 4 +- lib/custom_wizard/submission.rb | 1 + .../custom_wizard/submission_spec.rb | 8 +++- 8 files changed, 63 insertions(+), 46 deletions(-) create mode 100644 assets/javascripts/discourse/lib/wizard-submission.js.es6 diff --git a/assets/javascripts/discourse/components/wizard-table-field.js.es6 b/assets/javascripts/discourse/components/wizard-table-field.js.es6 index 049b4d40..93859f1f 100644 --- a/assets/javascripts/discourse/components/wizard-table-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-table-field.js.es6 @@ -25,9 +25,9 @@ export default Component.extend({ textState: "text-collapsed", toggleText: I18n.t("admin.wizard.expand_text"), - @discourseComputed("value", "isUser") - hasValue(value, isUser) { - if (isUser) { + @discourseComputed("value", "isUser", "isSubmittedAt") + hasValue(value, isUser, isSubmittedAt) { + if (isUser || isSubmittedAt) { return value; } return value && value.value; diff --git a/assets/javascripts/discourse/controllers/admin-wizards-submissions-show.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-submissions-show.js.es6 index 10621cd3..bddc1cfb 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-submissions-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-submissions-show.js.es6 @@ -3,7 +3,8 @@ import { empty } from "@ember/object/computed"; import discourseComputed from "discourse-common/utils/decorators"; import { fmt } from "discourse/lib/computed"; import showModal from "discourse/lib/show-modal"; -import CustomWizard from "../models/custom-wizard"; +import CustomWizardAdmin from "../models/custom-wizard-admin"; +import { formatModel } from "../lib/wizard-submission"; export default Controller.extend({ downloadUrl: fmt("wizard.id", "/admin/wizards/submissions/%@/download"), @@ -16,10 +17,12 @@ export default Controller.extend({ const wizardId = this.get("wizard.id"); this.set("loadingMore", true); - CustomWizard.submissions(wizardId, page) + CustomWizardAdmin.submissions(wizardId, page) .then((result) => { if (result.submissions) { - this.get("submissions").pushObjects(result.submissions); + const { fields, submissions } = formatModel(result); + + this.get("submissions").pushObjects(submissions); } }) .finally(() => { @@ -27,7 +30,7 @@ export default Controller.extend({ }); }, - @discourseComputed("submissions", "fields.@each.enabled") + @discourseComputed("submissions.[]", "fields.@each.enabled") displaySubmissions(submissions, fields) { let result = []; diff --git a/assets/javascripts/discourse/lib/wizard-submission.js.es6 b/assets/javascripts/discourse/lib/wizard-submission.js.es6 new file mode 100644 index 00000000..62de96c1 --- /dev/null +++ b/assets/javascripts/discourse/lib/wizard-submission.js.es6 @@ -0,0 +1,37 @@ +import EmberObject from "@ember/object"; + +function formatModel(model) { + let fields = [ + EmberObject.create({ id: "submitted_at", label: "Submitted At", enabled: true }), + EmberObject.create({ id: "username", label: "User", enabled: true }), + ]; + let submissions = []; + + model.submissions.forEach((s) => { + let submission = { + submitted_at: s.submitted_at, + username: s.user, + }; + + Object.keys(s.fields).forEach((fieldId) => { + if (!fields.some((field) => field.id === fieldId)) { + fields.push( + EmberObject.create({ + id: fieldId, + label: s.fields[fieldId].label, + enabled: true, + }) + ); + } + submission[fieldId] = s.fields[fieldId]; + }); + + submissions.push(EmberObject.create(submission)); + }); + + return { fields, submissions }; +} + +export { + formatModel +} diff --git a/assets/javascripts/discourse/models/custom-wizard-admin.js.es6 b/assets/javascripts/discourse/models/custom-wizard-admin.js.es6 index bb4f7001..de4bd986 100644 --- a/assets/javascripts/discourse/models/custom-wizard-admin.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard-admin.js.es6 @@ -211,9 +211,12 @@ CustomWizardAdmin.reopenClass({ .catch(popupAjaxError); }, - submissions(wizardId) { + submissions(wizardId, page = 0) { return ajax(`/admin/wizards/submissions/${wizardId}`, { type: "GET", + data: { + page + } }).catch(popupAjaxError); }, diff --git a/assets/javascripts/discourse/routes/admin-wizards-submissions-show.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-submissions-show.js.es6 index 59f4eec4..750e0d9f 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-submissions-show.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards-submissions-show.js.es6 @@ -1,7 +1,7 @@ import { A } from "@ember/array"; -import EmberObject from "@ember/object"; import CustomWizardAdmin from "../models/custom-wizard-admin"; import DiscourseRoute from "discourse/routes/discourse"; +import { formatModel } from "../lib/wizard-submission"; export default DiscourseRoute.extend({ model(params) { @@ -9,40 +9,7 @@ export default DiscourseRoute.extend({ }, setupController(controller, model) { - let fields = [ - EmberObject.create({ id: "username", label: "User", enabled: true }), - ]; - let submissions = []; - - model.submissions.forEach((s) => { - let submission = { - username: s.user, - }; - - Object.keys(s.fields).forEach((fieldId) => { - if (!fields.some((field) => field.id === fieldId)) { - fields.push( - EmberObject.create({ - id: fieldId, - label: s.fields[fieldId].label, - enabled: true, - }) - ); - } - submission[fieldId] = s.fields[fieldId]; - }); - - submission["submitted_at"] = s.submitted_at; - submissions.push(EmberObject.create(submission)); - }); - - let submittedAt = { - id: "submitted_at", - label: "Submitted At", - enabled: true, - }; - - fields.push(EmberObject.create(submittedAt)); + const { fields, submissions } = formatModel(model); controller.setProperties({ wizard: model.wizard, diff --git a/assets/javascripts/discourse/templates/components/wizard-table-field.hbs b/assets/javascripts/discourse/templates/components/wizard-table-field.hbs index bd7da5c4..f660a61c 100644 --- a/assets/javascripts/discourse/templates/components/wizard-table-field.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-table-field.hbs @@ -152,8 +152,8 @@ {{/if}} {{#if isSubmittedAt}} - - {{d-icon "clock"}}{{format-date value format="tiny"}} + + {{raw-date value}} {{/if}} {{else}} diff --git a/lib/custom_wizard/submission.rb b/lib/custom_wizard/submission.rb index 89de50bf..a52172e3 100644 --- a/lib/custom_wizard/submission.rb +++ b/lib/custom_wizard/submission.rb @@ -148,6 +148,7 @@ class CustomWizard::Submission end result.total = result.submissions.size + result.submissions.sort_by! { |h| [h.submitted_at ? 1 : 0, h.submitted_at] }.reverse! if !page.nil? start = page * PAGE_LIMIT diff --git a/spec/components/custom_wizard/submission_spec.rb b/spec/components/custom_wizard/submission_spec.rb index 48459a08..4011f32c 100644 --- a/spec/components/custom_wizard/submission_spec.rb +++ b/spec/components/custom_wizard/submission_spec.rb @@ -19,6 +19,8 @@ describe CustomWizard::Submission do describe "#list" do before do + freeze_time Time.now + template_json_2 = template_json.dup template_json_2["id"] = "super_mega_fun_wizard_2" CustomWizard::Template.save(template_json_2, skip_jobs: true) @@ -28,7 +30,7 @@ describe CustomWizard::Submission do @count = CustomWizard::Submission::PAGE_LIMIT + 20 @count.times do |index| - described_class.new(@wizard, step_1_field_1: "I am user submission #{index + 1}").save + described_class.new(@wizard, step_1_field_1: "I am user submission #{index + 1}", submitted_at: Time.now + (index + 1).minutes).save end described_class.new(@wizard2, step_1_field_1: "I am another user's submission").save described_class.new(@wizard3, step_1_field_1: "I am a user submission on another wizard").save @@ -45,6 +47,10 @@ describe CustomWizard::Submission do it "paginates submission lists" do expect(described_class.list(@wizard, page: 1).submissions.size).to eq((@count + 2) - CustomWizard::Submission::PAGE_LIMIT) end + + it "orders submissions by submitted_at" do + expect(described_class.list(@wizard).submissions.first.submitted_at.to_datetime.change(usec: 0)).to eq((Time.now + @count.minutes).change(usec: 0)) + end end describe "#cleanup_incomplete_submissions" do