diff --git a/assets/javascripts/discourse/routes/admin-wizards-wizard-show.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-wizard-show.js.es6 index 22b4c0f3..1384a252 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-wizard-show.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards-wizard-show.js.es6 @@ -18,7 +18,7 @@ export default DiscourseRoute.extend({ } }, - setupController(controller, model) { + setupController(controller, model) { const parentModel = this.modelFor('adminWizardsWizard'); const wizard = CustomWizard.create((!model || model.create) ? {} : model); diff --git a/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6 index 5b2c88e3..0b598c92 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6 @@ -6,7 +6,7 @@ import { ajax } from 'discourse/lib/ajax'; export default DiscourseRoute.extend({ model() { - return ajax(`/admin/wizards/wizard`); + return ajax("/admin/wizards/wizard"); }, afterModel(model) { @@ -52,14 +52,20 @@ export default DiscourseRoute.extend({ }); }, + currentWizard() { + const params = this.paramsFor('adminWizardsWizardShow'); + + if (params && params.wizardId) { + return params.wizardId; + } else { + return null; + } + }, + setupController(controller, model) { let props = { - wizardList: model.wizard_list - } - - const params = this.paramsFor('adminWizardsWizardShow'); - if (params && params.wizardId) { - props.wizardId = params.wizardId; + wizardList: model.wizard_list, + wizardId: this.currentWizard() } controller.setProperties(props); diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs index 016100b5..b7711339 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs @@ -80,7 +80,7 @@
- {{input value=field.file_types}} + {{input value=field.file_types class="medium"}}
{{/if}} @@ -156,7 +156,7 @@ {{input name="key" value=field.key - class="small" + class="medium" placeholderKey="admin.wizard.translation_placeholder"}} diff --git a/assets/stylesheets/common/wizard-admin.scss b/assets/stylesheets/common/wizard-admin.scss index e4b25e12..750957a5 100644 --- a/assets/stylesheets/common/wizard-admin.scss +++ b/assets/stylesheets/common/wizard-admin.scss @@ -14,6 +14,12 @@ padding: 10px; display: flex; justify-content: space-between; + + .wizard-message { + .d-icon { + margin-right: 4px; + } + } } .admin-wizard-container { @@ -178,6 +184,10 @@ margin-bottom: 0; } + input[type="number"] { + margin-bottom: 0; + } + input[disabled] { background-color: $primary-low; cursor: not-allowed; diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index b3e729b8..4ce6d571 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -13,7 +13,7 @@ en: name: "Name" name_placeholder: "wizard name" background: "Background" - background_placeholder: "color" + background_placeholder: "#hex" save_submissions: "Save" save_submissions_label: "Save wizard submissions." multiple_submissions: "Multiple" @@ -105,6 +105,7 @@ en: invalid: "{{property}} is invalid" dependent: "{{property}} is dependent on {{dependent}}" conflict: "{{type}} with {{property}} '{{value}}' already exists" + after_time: "After time invalid" step: header: "Steps" diff --git a/controllers/custom_wizard/admin/wizard.rb b/controllers/custom_wizard/admin/wizard.rb index e090314c..d773f35c 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/controllers/custom_wizard/admin/wizard.rb @@ -29,16 +29,14 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController def save opts = {} opts[:create] = params[:create] if params[:create] - + validator = CustomWizard::Validator.new(save_wizard_params.to_h, opts) validation = validator.perform if validation[:error] render json: { error: validation[:error] } - else - params = validation[:wizard] - - if wizard_id = CustomWizard::Wizard.save(params) + else + if wizard_id = CustomWizard::Wizard.save(validation[:wizard]) render json: success_json.merge(wizard_id: wizard_id) else render json: failed_json @@ -116,10 +114,9 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController :code, :skip_redirect, :url, + :post, + :post_template, title: mapped_params, - post: mapped_params, - post_builder: mapped_params, - post_template: mapped_params, category: mapped_params, tags: mapped_params, custom_fields: mapped_params, diff --git a/lib/custom_wizard/builder.rb b/lib/custom_wizard/builder.rb index aca414cf..b8f9b7dd 100644 --- a/lib/custom_wizard/builder.rb +++ b/lib/custom_wizard/builder.rb @@ -69,7 +69,6 @@ class CustomWizard::Builder if (required_data = step_template['required_data']).present? has_required_data = true - pairs = required_data.each do |required| required['pairs'].each do |pair| @@ -122,7 +121,7 @@ class CustomWizard::Builder validate_field(field, updater, step_template) if field['type'] != 'text_only' end end - + next if updater.errors.any? CustomWizard::Builder.step_handlers.each do |handler| @@ -274,36 +273,31 @@ class CustomWizard::Builder def validate_field(field, updater, step_template) value = updater.fields[field['id']] min_length = false - label = field['label'] || I18n.t("#{field['key']}.label") - if field['required'] && !value - updater.errors.add(field['id'].to_s, I18n.t('wizard.field.required', label: label)) - end - - if is_text_type(field) - min_length = field['min_length'] + label = field['label'] || I18n.t("#{field['key']}.label") + type = field['type'] + required = field['required'] + id = field['id'].to_s + min_length = field['min_length'] if is_text_type(field) + + if required && !value + updater.errors.add(id, I18n.t('wizard.field.required', label: label)) end if min_length && value.is_a?(String) && value.strip.length < min_length.to_i - updater.errors.add( - field['id'].to_s, - I18n.t('wizard.field.too_short', label: label, min: min_length.to_i) - ) + updater.errors.add(id, I18n.t('wizard.field.too_short', label: label, min: min_length.to_i)) end - if is_url_type(field) - if !check_if_url(value) - updater.errors.add(field['id'].to_s, I18n.t('wizard.field.not_url', label: label)) - end + if is_url_type(field) && !check_if_url(value) + updater.errors.add(id, I18n.t('wizard.field.not_url', label: label)) end - ## ensure all checkboxes are booleans - if field['type'] === 'checkbox' - updater.fields[field['id']] = standardise_boolean(value) + if type === 'checkbox' + updater.fields[id] = standardise_boolean(value) end CustomWizard::Builder.field_validators.each do |validator| - if field['type'] === validator[:type] + if type === validator[:type] validator[:block].call(field, updater, step_template) end end diff --git a/lib/custom_wizard/step_updater.rb b/lib/custom_wizard/step_updater.rb index b5f9bf6c..aa6523d5 100644 --- a/lib/custom_wizard/step_updater.rb +++ b/lib/custom_wizard/step_updater.rb @@ -18,10 +18,12 @@ class CustomWizard::StepUpdater @step.updater.call(self) if @step.present? && @step.updater.present? if success? - UserHistory.create(action: UserHistory.actions[:custom_wizard_step], - acting_user_id: @current_user.id, - context: @wizard.id, - subject: @step.id) + UserHistory.create( + action: UserHistory.actions[:custom_wizard_step], + acting_user_id: @current_user.id, + context: @wizard.id, + subject: @step.id + ) end end diff --git a/lib/custom_wizard/validator.rb b/lib/custom_wizard/validator.rb index df9f26ca..b14fe22f 100644 --- a/lib/custom_wizard/validator.rb +++ b/lib/custom_wizard/validator.rb @@ -8,17 +8,17 @@ class CustomWizard::Validator def perform params = @params - + check_id(params, :wizard) check_required(params, :wizard) check_depdendent(params, :wizard) after_time = nil - - if !@error && @params[:after_time] - after_time = validate_after_time - end + if !@error && @params[:after_time] + validate_after_time + end + if !@error params[:steps].each do |step| check_required(step, :step) @@ -42,13 +42,11 @@ class CustomWizard::Validator end end end - + if @error { error: @error } else - result = { wizard: params } - result[:after_time] = after_time if after_time - result + { wizard: params } end end @@ -105,39 +103,16 @@ class CustomWizard::Validator end end - def validate_after_time - if !@opts[:create] - wizard = CustomWizard::Wizard.create(params) - end - - new = false - error = nil - scheduled = nil - - if !@params[:after_time_scheduled] && !wizard[:after_time_scheduled] - error = 'after_time_need_time' - else - scheduled = Time.parse(@params[:after_time_scheduled]).utc - new = false - - if wizard[:after_time_scheduled] - new = scheduled != Time.parse(wizard[:after_time_scheduled]).utc - end + def validate_after_time + wizard = CustomWizard::Wizard.create(@params[:id]) if !@opts[:create] + current_time = wizard.present? ? wizard.after_time_scheduled : nil + new_time = @params[:after_time_scheduled] - begin - error = 'after_time_invalid' if new && scheduled < Time.now.utc - rescue ArgumentError - error = 'after_time_invalid' - end - end - - if error - @error = { type: error } - else - { - new: new, - scheduled: scheduled - } + if (new_time.blank? && current_time.blank?) || + (new_time !~ /^([01]?[0-9]|2[0-3])\:[0-5][0-9]$/) || + (Time.parse(new_time).utc < Time.now.utc) + + @error = { type: 'after_time' } end end end \ No newline at end of file diff --git a/lib/custom_wizard/wizard.rb b/lib/custom_wizard/wizard.rb index 049786da..9d2383cf 100644 --- a/lib/custom_wizard/wizard.rb +++ b/lib/custom_wizard/wizard.rb @@ -298,7 +298,8 @@ class CustomWizard::Wizard if wizard[:after_time] Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard[:id]) - Jobs.enqueue_at(wizard[:after_time_scheduled], :set_after_time_wizard, wizard_id: wizard[:id]) + enqueue_at = Time.parse(wizard[:after_time_scheduled]).utc + Jobs.enqueue_at(enqueue_at, :set_after_time_wizard, wizard_id: wizard[:id]) end if existing_wizard && existing_wizard.after_time && !wizard[:after_time] @@ -330,6 +331,8 @@ class CustomWizard::Wizard def self.create(wizard_id, user = nil) if wizard = self.find(wizard_id) CustomWizard::Wizard.new(wizard.to_h, user) + else + false end end