0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-22 09:20:29 +01:00

FIX: pagination and submitted_at ordering of submissions

Dieser Commit ist enthalten in:
Angus McLeod 2022-10-05 17:14:43 +02:00
Ursprung 60abb6981e
Commit c2e759b1c5
8 geänderte Dateien mit 63 neuen und 46 gelöschten Zeilen

Datei anzeigen

@ -25,9 +25,9 @@ export default Component.extend({
textState: "text-collapsed", textState: "text-collapsed",
toggleText: I18n.t("admin.wizard.expand_text"), toggleText: I18n.t("admin.wizard.expand_text"),
@discourseComputed("value", "isUser") @discourseComputed("value", "isUser", "isSubmittedAt")
hasValue(value, isUser) { hasValue(value, isUser, isSubmittedAt) {
if (isUser) { if (isUser || isSubmittedAt) {
return value; return value;
} }
return value && value.value; return value && value.value;

Datei anzeigen

@ -3,7 +3,8 @@ import { empty } from "@ember/object/computed";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
import { fmt } from "discourse/lib/computed"; import { fmt } from "discourse/lib/computed";
import showModal from "discourse/lib/show-modal"; 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({ export default Controller.extend({
downloadUrl: fmt("wizard.id", "/admin/wizards/submissions/%@/download"), downloadUrl: fmt("wizard.id", "/admin/wizards/submissions/%@/download"),
@ -16,10 +17,12 @@ export default Controller.extend({
const wizardId = this.get("wizard.id"); const wizardId = this.get("wizard.id");
this.set("loadingMore", true); this.set("loadingMore", true);
CustomWizard.submissions(wizardId, page) CustomWizardAdmin.submissions(wizardId, page)
.then((result) => { .then((result) => {
if (result.submissions) { if (result.submissions) {
this.get("submissions").pushObjects(result.submissions); const { fields, submissions } = formatModel(result);
this.get("submissions").pushObjects(submissions);
} }
}) })
.finally(() => { .finally(() => {
@ -27,7 +30,7 @@ export default Controller.extend({
}); });
}, },
@discourseComputed("submissions", "fields.@each.enabled") @discourseComputed("submissions.[]", "fields.@each.enabled")
displaySubmissions(submissions, fields) { displaySubmissions(submissions, fields) {
let result = []; let result = [];

Datei anzeigen

@ -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
}

Datei anzeigen

@ -211,9 +211,12 @@ CustomWizardAdmin.reopenClass({
.catch(popupAjaxError); .catch(popupAjaxError);
}, },
submissions(wizardId) { submissions(wizardId, page = 0) {
return ajax(`/admin/wizards/submissions/${wizardId}`, { return ajax(`/admin/wizards/submissions/${wizardId}`, {
type: "GET", type: "GET",
data: {
page
}
}).catch(popupAjaxError); }).catch(popupAjaxError);
}, },

Datei anzeigen

@ -1,7 +1,7 @@
import { A } from "@ember/array"; import { A } from "@ember/array";
import EmberObject from "@ember/object";
import CustomWizardAdmin from "../models/custom-wizard-admin"; import CustomWizardAdmin from "../models/custom-wizard-admin";
import DiscourseRoute from "discourse/routes/discourse"; import DiscourseRoute from "discourse/routes/discourse";
import { formatModel } from "../lib/wizard-submission";
export default DiscourseRoute.extend({ export default DiscourseRoute.extend({
model(params) { model(params) {
@ -9,40 +9,7 @@ export default DiscourseRoute.extend({
}, },
setupController(controller, model) { setupController(controller, model) {
let fields = [ const { fields, submissions } = formatModel(model);
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));
controller.setProperties({ controller.setProperties({
wizard: model.wizard, wizard: model.wizard,

Datei anzeigen

@ -152,8 +152,8 @@
{{/if}} {{/if}}
{{#if isSubmittedAt}} {{#if isSubmittedAt}}
<span class="date" title={{value.value}}> <span class="date" title={{value}}>
{{d-icon "clock"}}{{format-date value format="tiny"}} {{raw-date value}}
</span> </span>
{{/if}} {{/if}}
{{else}} {{else}}

Datei anzeigen

@ -148,6 +148,7 @@ class CustomWizard::Submission
end end
result.total = result.submissions.size result.total = result.submissions.size
result.submissions.sort_by! { |h| [h.submitted_at ? 1 : 0, h.submitted_at] }.reverse!
if !page.nil? if !page.nil?
start = page * PAGE_LIMIT start = page * PAGE_LIMIT

Datei anzeigen

@ -19,6 +19,8 @@ describe CustomWizard::Submission do
describe "#list" do describe "#list" do
before do before do
freeze_time Time.now
template_json_2 = template_json.dup template_json_2 = template_json.dup
template_json_2["id"] = "super_mega_fun_wizard_2" template_json_2["id"] = "super_mega_fun_wizard_2"
CustomWizard::Template.save(template_json_2, skip_jobs: true) CustomWizard::Template.save(template_json_2, skip_jobs: true)
@ -28,7 +30,7 @@ describe CustomWizard::Submission do
@count = CustomWizard::Submission::PAGE_LIMIT + 20 @count = CustomWizard::Submission::PAGE_LIMIT + 20
@count.times do |index| @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 end
described_class.new(@wizard2, step_1_field_1: "I am another user's submission").save 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 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 it "paginates submission lists" do
expect(described_class.list(@wizard, page: 1).submissions.size).to eq((@count + 2) - CustomWizard::Submission::PAGE_LIMIT) expect(described_class.list(@wizard, page: 1).submissions.size).to eq((@count + 2) - CustomWizard::Submission::PAGE_LIMIT)
end 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 end
describe "#cleanup_incomplete_submissions" do describe "#cleanup_incomplete_submissions" do