0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 18:50:27 +01:00
Dieser Commit ist enthalten in:
Keegan George 2021-09-01 12:38:09 -07:00
Commit 622ab1b2ce
17 geänderte Dateien mit 261 neuen und 80 gelöschten Zeilen

Datei anzeigen

@ -1,15 +1,10 @@
import { import discourseComputed from "discourse-common/utils/decorators";
default as discourseComputed,
observes,
on,
} from "discourse-common/utils/decorators";
import { generateName } from "../lib/wizard"; import { generateName } from "../lib/wizard";
import { import {
setWizardDefaults, setWizardDefaults,
default as wizardSchema, default as wizardSchema,
} from "../lib/wizard-schema"; } from "../lib/wizard-schema";
import { notEmpty } from "@ember/object/computed"; import { notEmpty } from "@ember/object/computed";
import { scheduleOnce } from "@ember/runloop";
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import Component from "@ember/component"; import Component from "@ember/component";
import { A } from "@ember/array"; import { A } from "@ember/array";
@ -19,28 +14,12 @@ export default Component.extend({
items: A(), items: A(),
anyLinks: notEmpty("links"), anyLinks: notEmpty("links"),
@on("didInsertElement")
@observes("links.[]")
setupSortable() {
scheduleOnce("afterRender", () => this.applySortable());
},
applySortable() {
$(this.element)
.find(".link-list")
.sortable({ tolerance: "pointer" })
.on("sortupdate", (e, ui) => {
this.updateItemOrder(ui.item.data("id"), ui.item.index());
});
},
updateItemOrder(itemId, newIndex) { updateItemOrder(itemId, newIndex) {
const items = this.items; const items = this.items;
const item = items.findBy("id", itemId); const item = items.findBy("id", itemId);
items.removeObject(item); items.removeObject(item);
item.set("index", newIndex); item.set("index", newIndex);
items.insertAt(newIndex, item); items.insertAt(newIndex, item);
scheduleOnce("afterRender", this, () => this.applySortable());
}, },
@discourseComputed("itemType") @discourseComputed("itemType")
@ -58,7 +37,7 @@ export default Component.extend({
return; return;
} }
return items.map((item) => { return items.map((item, index) => {
if (item) { if (item) {
let link = { let link = {
id: item.id, id: item.id,
@ -77,6 +56,15 @@ export default Component.extend({
} }
link.classes = classes; link.classes = classes;
link.index = index;
if (index === 0) {
link.first = true;
}
if (index === items.length - 1) {
link.last = true;
}
return link; return link;
} }
@ -118,6 +106,14 @@ export default Component.extend({
this.set("current", newItem); this.set("current", newItem);
}, },
back(item) {
this.updateItemOrder(item.id, item.index - 1);
},
forward(item) {
this.updateItemOrder(item.id, item.index + 1);
},
change(itemId) { change(itemId) {
this.set("current", this.items.findBy("id", itemId)); this.set("current", this.items.findBy("id", itemId));
}, },

Datei anzeigen

@ -7,6 +7,10 @@
{{/if}} {{/if}}
{{nav-item route="adminWizardsLogs" label="admin.wizard.log.nav_label"}} {{nav-item route="adminWizardsLogs" label="admin.wizard.log.nav_label"}}
{{nav-item route="adminWizardsManager" label="admin.wizard.manager.nav_label"}} {{nav-item route="adminWizardsManager" label="admin.wizard.manager.nav_label"}}
<div class="admin-actions">
<a target="_blank" class="btn btn-pavilion-pro" rel="noreferrer noopener" href="https://thepavilion.io/w/support" title={{i18n "admin.wizard.pro_support_button.title"}}>{{d-icon "far-life-ring"}}{{i18n "admin.wizard.pro_support_button.label"}}</a>
</div>
{{/admin-nav}} {{/admin-nav}}
<div class="admin-container"> <div class="admin-container">

Datei anzeigen

@ -2,10 +2,16 @@
<div class="link-list"> <div class="link-list">
{{#if anyLinks}} {{#if anyLinks}}
{{#each links as |l|}} {{#each links as |link|}}
<div data-id={{l.id}}> <div data-id={{link.id}}>
{{d-button action="change" actionParam=l.id translatedLabel=l.label class=l.classes}} {{d-button action="change" actionParam=link.id translatedLabel=link.label class=link.classes}}
{{d-button action="remove" actionParam=l.id icon="times" class="remove"}} {{#unless link.first}}
{{d-button action="back" actionParam=link icon="arrow-left" class="back"}}
{{/unless}}
{{#unless link.last}}
{{d-button action="forward" actionParam=link icon="arrow-right" class="forward"}}
{{/unless}}
{{d-button action="remove" actionParam=link.id icon="times" class="remove"}}
</div> </div>
{{/each}} {{/each}}
{{/if}} {{/if}}

Datei anzeigen

@ -2,6 +2,7 @@
//= require discourse/app/mixins/singleton //= require discourse/app/mixins/singleton
//= require discourse/app/mixins/upload //= require discourse/app/mixins/upload
//= require discourse/app/mixins/composer-upload
//= require discourse/app/adapters/rest //= require discourse/app/adapters/rest

Datei anzeigen

@ -2,6 +2,7 @@
@import "wizard-manager"; @import "wizard-manager";
@import "wizard-api"; @import "wizard-api";
@import "common/components/buttons"; @import "common/components/buttons";
@import "wizard-variables";
.admin-wizard-controls { .admin-wizard-controls {
display: flex; display: flex;
@ -766,3 +767,21 @@
vertical-align: middle; vertical-align: middle;
} }
.btn.btn-pavilion-pro {
background: var(--pavilion-primary);
color: var(--pavilion-secondary);
.d-icon {
color: var(--pavilion-secondary);
}
&:hover,
&:focus {
background: darken($pavilionPrimary, 5%);
&[href],
svg.d-icon {
color: darken($pavilionSecondary, 10%);
}
}
}

Datei anzeigen

@ -0,0 +1,7 @@
$pavilionPrimary: #3c1c8c;
$pavilionSecondary: #ffffff;
:root {
--pavilion-primary: #3c1c8c;
--pavilion-secondary: #ffffff;
}

Datei anzeigen

@ -58,6 +58,9 @@ en:
select_type: "Select a type" select_type: "Select a type"
condition: "Condition" condition: "Condition"
index: "Index" index: "Index"
pro_support_button:
title: "Request Pro Support"
label: "Pro Support"
message: message:
wizard: wizard:

Datei anzeigen

@ -68,7 +68,8 @@ class CustomWizard::WizardController < ::ApplicationController
result.merge!(redirect_to: submission.redirect_to) result.merge!(redirect_to: submission.redirect_to)
end end
wizard.final_cleanup! submission.remove if submission.present?
wizard.reset
end end
render json: result render json: result

Datei anzeigen

@ -392,7 +392,7 @@ class CustomWizard::Action
user_ids.each { |user_id| group.group_users.build(user_id: user_id) } user_ids.each { |user_id| group.group_users.build(user_id: user_id) }
end end
GroupActionLogger.new(user, group, skip_guardian: true).log_change_group_settings GroupActionLogger.new(user, group).log_change_group_settings
log_success("Group created", group.name) log_success("Group created", group.name)
result.output = group.name result.output = group.name

Datei anzeigen

@ -75,6 +75,7 @@ class CustomWizard::Builder
end end
@wizard.update! @wizard.update!
CustomWizard::Submission.cleanup_incomplete_submissions(@wizard)
@wizard @wizard
end end

Datei anzeigen

@ -4,7 +4,7 @@ class CustomWizard::Submission
PAGE_LIMIT = 50 PAGE_LIMIT = 50
KEY ||= "submissions" KEY ||= "submissions"
META ||= %w(submitted_at route_to redirect_on_complete redirect_to) META ||= %w(updated_at submitted_at route_to redirect_on_complete redirect_to)
attr_reader :id, attr_reader :id,
:user, :user,
@ -46,6 +46,7 @@ class CustomWizard::Submission
submission_list = self.class.list(wizard, user_id: user.id) submission_list = self.class.list(wizard, user_id: user.id)
submissions = submission_list.submissions.select { |submission| submission.id != self.id } submissions = submission_list.submissions.select { |submission| submission.id != self.id }
self.updated_at = Time.now.iso8601
submissions.push(self) submissions.push(self)
submission_data = submissions.map { |submission| data_to_save(submission) } submission_data = submissions.map { |submission| data_to_save(submission) }
@ -97,7 +98,41 @@ class CustomWizard::Submission
new(wizard, data, user_id) new(wizard, data, user_id)
end end
def self.list(wizard, user_id: nil, page: nil) def remove
if present?
user_id = @user.id
wizard_id = @wizard.id
submission_id = @id
data = PluginStore.get("#{wizard_id}_#{KEY}", user_id)
data.delete_if { |sub| sub["id"] == submission_id }
PluginStore.set("#{wizard_id}_#{KEY}", user_id, data)
end
end
def self.cleanup_incomplete_submissions(wizard)
user_id = wizard.user.id
all_submissions = list(wizard, user_id: user_id)
sorted_submissions = all_submissions.submissions.sort_by do |submission|
zero_epoch_time = DateTime.strptime("0", '%s')
[
submission.submitted_at ? Time.iso8601(submission.submitted_at) : zero_epoch_time,
submission.updated_at ? Time.iso8601(submission.updated_at) : zero_epoch_time
]
end.reverse
has_incomplete = false
valid_submissions = sorted_submissions.select do |submission|
to_be_included = submission.submitted_at || !has_incomplete
has_incomplete = true if !submission.submitted_at
to_be_included
end
valid_data = valid_submissions.map { |submission| submission.data_to_save(submission) }
PluginStore.set("#{wizard.id}_#{KEY}", user_id, valid_data)
end
def self.list(wizard, user_id: nil, order_by: nil, page: nil)
params = { plugin_name: "#{wizard.id}_#{KEY}" } params = { plugin_name: "#{wizard.id}_#{KEY}" }
params[:key] = user_id if user_id.present? params[:key] = user_id if user_id.present?

Datei anzeigen

@ -32,13 +32,15 @@ class ::CustomWizard::UpdateValidator
@updater.errors.add(field_id, I18n.t('wizard.field.required', label: label)) @updater.errors.add(field_id, I18n.t('wizard.field.required', label: label))
end end
if min_length.present? && value.is_a?(String) && value.strip.length < min_length.to_i if value.is_a?(String) && (stripped_length = value.strip.length) > 0
if min_length.present? && stripped_length < min_length.to_i
@updater.errors.add(field_id, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i)) @updater.errors.add(field_id, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i))
end end
if max_length.present? && value.is_a?(String) && value.strip.length > max_length.to_i if max_length.present? && stripped_length > max_length.to_i
@updater.errors.add(field_id, I18n.t('wizard.field.too_long', label: label, max: max_length.to_i)) @updater.errors.add(field_id, I18n.t('wizard.field.too_long', label: label, max: max_length.to_i))
end end
end
if is_url_type(field) && value.present? && !check_if_url(value) if is_url_type(field) && value.present? && !check_if_url(value)
@updater.errors.add(field_id, I18n.t('wizard.field.not_url', label: label)) @updater.errors.add(field_id, I18n.t('wizard.field.not_url', label: label))

Datei anzeigen

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
# name: discourse-custom-wizard # name: discourse-custom-wizard
# about: Create custom wizards # about: Create custom wizards
# version: 0.8.0 # version: 0.8.1
# authors: Angus McLeod # authors: Angus McLeod
# url: https://github.com/paviliondev/discourse-custom-wizard # url: https://github.com/paviliondev/discourse-custom-wizard
# contact emails: angus@thepavilion.io # contact emails: angus@thepavilion.io
@ -40,6 +40,8 @@ if respond_to?(:register_svg_icon)
register_svg_icon "clock" register_svg_icon "clock"
register_svg_icon "link" register_svg_icon "link"
register_svg_icon "comment-alt" register_svg_icon "comment-alt"
register_svg_icon "far-life-ring"
register_svg_icon "arrow-right"
end end
class ::Sprockets::DirectiveProcessor class ::Sprockets::DirectiveProcessor

Datei anzeigen

@ -13,12 +13,22 @@ describe CustomWizard::Submission do
before do before do
CustomWizard::Template.save(template_json, skip_jobs: true) CustomWizard::Template.save(template_json, skip_jobs: true)
@wizard = CustomWizard::Wizard.create(template_json["id"], user)
described_class.new(@wizard, step_1_field_1: "I am user submission").save
end
it "saves a user's submission" do
expect(
described_class.get(@wizard, user.id).fields["step_1_field_1"]
).to eq("I am user submission")
end
context "#list" do
before do
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)
@wizard = CustomWizard::Wizard.create(template_json["id"], user)
@wizard2 = CustomWizard::Wizard.create(template_json["id"], user2) @wizard2 = CustomWizard::Wizard.create(template_json["id"], user2)
@wizard3 = CustomWizard::Wizard.create(template_json_2["id"], user) @wizard3 = CustomWizard::Wizard.create(template_json_2["id"], user)
@count = CustomWizard::Submission::PAGE_LIMIT + 20 @count = CustomWizard::Submission::PAGE_LIMIT + 20
@ -30,21 +40,62 @@ describe CustomWizard::Submission do
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
end end
it "saves a user's submission" do
expect(
described_class.get(@wizard, user.id).fields["step_1_field_1"]
).to eq("I am user submission #{@count}")
end
it "list submissions by wizard" do it "list submissions by wizard" do
expect(described_class.list(@wizard).total).to eq(@count + 1) expect(described_class.list(@wizard).total).to eq(@count + 2)
end end
it "list submissions by wizard and user" do it "list submissions by wizard and user" do
expect(described_class.list(@wizard, user_id: user.id).total).to eq(@count) expect(described_class.list(@wizard, user_id: user.id).total).to eq(@count + 1)
end end
it "paginates submission lists" do it "paginates submission lists" do
expect(described_class.list(@wizard, page: 1).submissions.size).to eq((@count + 1) - CustomWizard::Submission::PAGE_LIMIT) expect(described_class.list(@wizard, page: 1).submissions.size).to eq((@count + 2) - CustomWizard::Submission::PAGE_LIMIT)
end
end
context "#cleanup_incomplete_submissions" do
it "cleans up redundant incomplete submissions on each build" do
freeze_time Time.now + 1
described_class.new(@wizard, step_1_field_1: "I am the second submission").save
builder = CustomWizard::Builder.new(@wizard.id, @wizard.user)
builder.build
submissions = described_class.list(@wizard, user_id: @wizard.user.id).submissions
expect(submissions.length).to eq(1)
expect(submissions.first.fields["step_1_field_1"]).to eq("I am the second submission")
end
it "handles submissions without 'updated_at' field correctly" do
described_class.new(@wizard, step_1_field_1: "I am the second submission").save
described_class.new(@wizard, step_1_field_1: "I am the third submission").save
sub_data = PluginStore.get("#{@wizard.id}_submissions", @wizard.user.id)
sub_data.each do |sub|
sub['updated_at'] = nil
end
PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, sub_data)
builder = CustomWizard::Builder.new(@wizard.id, @wizard.user)
builder.build
submissions = described_class.list(@wizard, user_id: @wizard.user.id).submissions
expect(submissions.length).to eq(1)
expect(submissions.first.fields["step_1_field_1"]).to eq("I am the third submission")
end
it "handles submissions with and without 'updated_at' field correctly" do
freeze_time Time.now + 1
described_class.new(@wizard, step_1_field_1: "I am the second submission").save
freeze_time Time.now + 2
described_class.new(@wizard, step_1_field_1: "I am the third submission").save
sub_data = PluginStore.get("#{@wizard.id}_submissions", @wizard.user.id)
sub_data[0]['updated_at'] = nil
PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, sub_data)
builder = CustomWizard::Builder.new(@wizard.id, @wizard.user)
builder.build
submissions = described_class.list(@wizard, user_id: @wizard.user.id).submissions
expect(submissions.length).to eq(1)
expect(submissions.first.fields["step_1_field_1"]).to eq("I am the third submission")
end
end end
end end

Datei anzeigen

@ -97,6 +97,31 @@ describe CustomWizard::UpdateValidator do
).to eq(nil) ).to eq(nil)
end end
it "applies min length only if the input is non-empty" do
min_length = 3
@template[:steps][0][:fields][0][:min_length] = min_length
CustomWizard::Template.save(@template)
updater = perform_validation('step_1', step_1_field_1: '')
expect(
updater.errors.messages[:step_1_field_1].first
).to eq(nil)
end
it "applies max length only if the input is non-empty" do
max_length = 100
@template[:steps][0][:fields][0][:max_length] = max_length
CustomWizard::Template.save(@template)
updater = perform_validation('step_1', step_1_field_1: "")
expect(
updater.errors.messages[:step_1_field_1].first
).to eq(nil)
end
it 'standardises boolean entries' do it 'standardises boolean entries' do
updater = perform_validation('step_2', step_2_field_5: 'false') updater = perform_validation('step_2', step_2_field_5: 'false')
expect(updater.submission['step_2_field_5']).to eq(false) expect(updater.submission['step_2_field_5']).to eq(false)

Datei anzeigen

@ -11,4 +11,7 @@ if ENV['SIMPLECOV']
end end
end end
require 'oj'
Oj.default_options = Oj.default_options.merge(cache_str: -1)
require 'rails_helper' require 'rails_helper'

Datei anzeigen

@ -50,6 +50,8 @@ describe CustomWizard::WizardController do
expect(response.parsed_body["error"]).to eq("We couldn't find a wizard at that address.") expect(response.parsed_body["error"]).to eq("We couldn't find a wizard at that address.")
end end
context 'when user skips the wizard' do
it 'skips a wizard if user is allowed to skip' do it 'skips a wizard if user is allowed to skip' do
put '/w/super-mega-fun-wizard/skip.json' put '/w/super-mega-fun-wizard/skip.json'
expect(response.status).to eq(200) expect(response.status).to eq(200)
@ -76,4 +78,27 @@ describe CustomWizard::WizardController do
put '/w/super-mega-fun-wizard/skip.json' put '/w/super-mega-fun-wizard/skip.json'
expect(response.parsed_body['redirect_to']).to eq('/t/2') expect(response.parsed_body['redirect_to']).to eq('/t/2')
end end
it "deletes the submission if user has filled up some data" do
@wizard = CustomWizard::Wizard.create(@template["id"], user)
CustomWizard::Submission.new(@wizard, step_1_field_1: "Hello World").save
current_submission = @wizard.current_submission
put '/w/super-mega-fun-wizard/skip.json'
submissions = CustomWizard::Submission.list(@wizard).submissions
expect(submissions.any? { |submission| submission.id == current_submission.id }).to eq(false)
end
it "starts from the first step if user visits after skipping the wizard" do
put '/w/super-mega-fun-wizard/steps/step_1.json', params: {
fields: {
step_1_field_1: "Text input"
}
}
put '/w/super-mega-fun-wizard/skip.json'
get '/w/super-mega-fun-wizard.json'
expect(response.parsed_body["start"]).to eq('step_1')
end
end
end end