Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-26 02:50:28 +01:00
add reset flag
Dieser Commit ist enthalten in:
Ursprung
e8ac4f7ca8
Commit
0ea0524dcf
6 geänderte Dateien mit 40 neuen und 9 gelöschten Zeilen
3
assets/javascripts/wizard/controllers/custom.js.es6
Normale Datei
3
assets/javascripts/wizard/controllers/custom.js.es6
Normale Datei
|
@ -0,0 +1,3 @@
|
||||||
|
export default Ember.Controller.extend({
|
||||||
|
queryParams: ['reset']
|
||||||
|
})
|
|
@ -31,8 +31,11 @@ CustomWizard.reopenClass({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export function findCustomWizard(wizardId) {
|
export function findCustomWizard(wizardId, opts = {}) {
|
||||||
return ajax({ url: `/w/${wizardId}`, cache: false, dataType: 'json' }).then(result => {
|
let url = `/w/${wizardId}`;
|
||||||
|
if (opts.reset) url += '?reset=true';
|
||||||
|
|
||||||
|
return ajax({ url, cache: false, dataType: 'json' }).then(result => {
|
||||||
const wizard = result.wizard;
|
const wizard = result.wizard;
|
||||||
|
|
||||||
if (!wizard) return null;
|
if (!wizard) return null;
|
||||||
|
|
|
@ -5,7 +5,9 @@ import { ajax } from 'wizard/lib/ajax';
|
||||||
|
|
||||||
export default Ember.Route.extend({
|
export default Ember.Route.extend({
|
||||||
model(params) {
|
model(params) {
|
||||||
return findCustomWizard(params.wizard_id);
|
let opts = {};
|
||||||
|
if (params.reset == 'true') opts['reset'] = true;
|
||||||
|
return findCustomWizard(params.wizard_id, opts);
|
||||||
},
|
},
|
||||||
|
|
||||||
afterModel() {
|
afterModel() {
|
||||||
|
|
|
@ -23,8 +23,11 @@ class CustomWizard::WizardController < ::ApplicationController
|
||||||
format.json do
|
format.json do
|
||||||
builder = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore)
|
builder = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore)
|
||||||
|
|
||||||
|
builder_opts = {}
|
||||||
|
builder_opts[:reset] = params[:reset] if params[:reset]
|
||||||
|
|
||||||
if builder.wizard.present?
|
if builder.wizard.present?
|
||||||
wizard = builder.build
|
wizard = builder.build(builder_opts)
|
||||||
render_serialized(wizard, WizardSerializer)
|
render_serialized(wizard, WizardSerializer)
|
||||||
else
|
else
|
||||||
render json: { error: I18n.t('wizard.none') }
|
render json: { error: I18n.t('wizard.none') }
|
||||||
|
|
|
@ -52,8 +52,11 @@ class CustomWizard::Builder
|
||||||
result.gsub!(/w\{(.*?)\}/) { |match| data[$1.to_sym] }
|
result.gsub!(/w\{(.*?)\}/) { |match| data[$1.to_sym] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def build
|
def build(build_opts = {})
|
||||||
unless (@wizard.completed? && !@wizard.multiple_submissions && !@wizard.user.admin) || !@steps || !@wizard.permitted?
|
unless (@wizard.completed? && !@wizard.multiple_submissions && !@wizard.user.admin) || !@steps || !@wizard.permitted?
|
||||||
|
|
||||||
|
reset_submissions if build_opts[:reset]
|
||||||
|
|
||||||
@steps.each do |step_template|
|
@steps.each do |step_template|
|
||||||
@wizard.append_step(step_template['id']) do |step|
|
@wizard.append_step(step_template['id']) do |step|
|
||||||
step.title = step_template['title'] if step_template['title']
|
step.title = step_template['title'] if step_template['title']
|
||||||
|
@ -63,7 +66,7 @@ class CustomWizard::Builder
|
||||||
|
|
||||||
if step_template['fields'] && step_template['fields'].length
|
if step_template['fields'] && step_template['fields'].length
|
||||||
step_template['fields'].each do |field_template|
|
step_template['fields'].each do |field_template|
|
||||||
append_field(step, step_template, field_template)
|
append_field(step, step_template, field_template, build_opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -126,7 +129,7 @@ class CustomWizard::Builder
|
||||||
@wizard
|
@wizard
|
||||||
end
|
end
|
||||||
|
|
||||||
def append_field(step, step_template, field_template)
|
def append_field(step, step_template, field_template, build_opts)
|
||||||
params = {
|
params = {
|
||||||
id: field_template['id'],
|
id: field_template['id'],
|
||||||
type: field_template['type'],
|
type: field_template['type'],
|
||||||
|
@ -139,7 +142,7 @@ class CustomWizard::Builder
|
||||||
params[:key] = field_template['key'] if field_template['key']
|
params[:key] = field_template['key'] if field_template['key']
|
||||||
|
|
||||||
## Load previously submitted values
|
## Load previously submitted values
|
||||||
if @submissions.last && !@submissions.last.key?("submitted_at")
|
if !build_opts[:reset] && @submissions.last && !@submissions.last.key?("submitted_at")
|
||||||
submission = @submissions.last
|
submission = @submissions.last
|
||||||
params[:value] = submission[field_template['id']] if submission[field_template['id']]
|
params[:value] = submission[field_template['id']] if submission[field_template['id']]
|
||||||
end
|
end
|
||||||
|
@ -401,4 +404,10 @@ class CustomWizard::Builder
|
||||||
PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions)
|
PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def reset_submissions
|
||||||
|
@submissions.pop(1) if @wizard.unfinished?
|
||||||
|
PluginStore.set("#{@wizard.id}_submissions", @wizard.user.id, @submissions)
|
||||||
|
@wizard.reset
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -82,7 +82,9 @@ class CustomWizard::Wizard
|
||||||
context: @id,
|
context: @id,
|
||||||
).distinct.order('updated_at DESC').first
|
).distinct.order('updated_at DESC').first
|
||||||
|
|
||||||
if most_recent
|
if most_recent.subject == "reset"
|
||||||
|
false
|
||||||
|
elsif most_recent
|
||||||
last_finished_step = most_recent.subject
|
last_finished_step = most_recent.subject
|
||||||
last_step = CustomWizard::Wizard.step_ids(@id).last
|
last_step = CustomWizard::Wizard.step_ids(@id).last
|
||||||
last_finished_step != last_step
|
last_finished_step != last_step
|
||||||
|
@ -113,6 +115,15 @@ class CustomWizard::Wizard
|
||||||
user.staff? || user.trust_level.to_i >= min_trust.to_i
|
user.staff? || user.trust_level.to_i >= min_trust.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def reset
|
||||||
|
::UserHistory.create(
|
||||||
|
action: ::UserHistory.actions[:custom_wizard_step],
|
||||||
|
acting_user_id: @user.id,
|
||||||
|
context: @id,
|
||||||
|
subject: "reset"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
def self.after_signup
|
def self.after_signup
|
||||||
rows = PluginStoreRow.where(plugin_name: 'custom_wizard')
|
rows = PluginStoreRow.where(plugin_name: 'custom_wizard')
|
||||||
wizards = [*rows].select { |r| r.value['after_signup'] }
|
wizards = [*rows].select { |r| r.value['after_signup'] }
|
||||||
|
|
Laden …
In neuem Issue referenzieren