From c058340c84e48e55e0abb27d9b4616731c914f1f Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Sat, 27 Jul 2019 16:10:26 +1000 Subject: [PATCH] Fix tag and category fields --- .../components/wizard-field-category.js.es6 | 19 ++++++++++-- .../templates/components/wizard-field-tag.hbs | 2 +- lib/builder.rb | 31 ++++++++++--------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/assets/javascripts/wizard/components/wizard-field-category.js.es6 b/assets/javascripts/wizard/components/wizard-field-category.js.es6 index 0fa56df7..9df3d0a4 100644 --- a/assets/javascripts/wizard/components/wizard-field-category.js.es6 +++ b/assets/javascripts/wizard/components/wizard-field-category.js.es6 @@ -1,13 +1,28 @@ import { observes } from 'ember-addons/ember-computed-decorators'; +import Category from 'discourse/models/category'; export default Ember.Component.extend({ + didInsertElement() { + const value = this.get('field.value'); + if (value) { + const property = this.get('field.property') || 'id'; + const categories = [...value].map(v => { + return property === 'id' ? + Category.findById(v) : + Category.findBySlug(v); + }); + this.set('categories', categories); + } + }, + @observes('categories') setValue() { const categories = this.get('categories'); if (categories.length) { - const limit = this.get('field.limit'); const property = this.get('field.property') || 'id'; - let value = limit === 1 ? categories[0][property] : categories.map(c => c[property]); + let value = categories.length === 1 ? + categories[0][property] : + categories.map(c => c[property]); this.set('field.value', value); } } diff --git a/assets/javascripts/wizard/templates/components/wizard-field-tag.hbs b/assets/javascripts/wizard/templates/components/wizard-field-tag.hbs index 16243d36..4c163364 100644 --- a/assets/javascripts/wizard/templates/components/wizard-field-tag.hbs +++ b/assets/javascripts/wizard/templates/components/wizard-field-tag.hbs @@ -1 +1 @@ -{{tag-chooser value=field.value maximum=field.limit}} +{{tag-chooser tags=field.value maximum=field.limit}} diff --git a/lib/builder.rb b/lib/builder.rb index a098daaf..221a95e3 100644 --- a/lib/builder.rb +++ b/lib/builder.rb @@ -107,13 +107,13 @@ class CustomWizard::Builder step.on_update do |updater| @updater = updater user = @wizard.user - + if step_template['fields'] && step_template['fields'].length step_template['fields'].each do |field| 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| @@ -131,7 +131,7 @@ class CustomWizard::Builder submission = @submissions.last data = submission.merge(data) end - + if step_template['actions'] && step_template['actions'].length && data step_template['actions'].each do |action| self.send(action['type'].to_sym, user, action, data) @@ -331,7 +331,7 @@ class CustomWizard::Builder else post = data[action['post']] end - + if title params = { title: title, @@ -339,22 +339,23 @@ class CustomWizard::Builder skip_validations: true } - if action['custom_category_enabled'] && - !action['custom_category_wizard_field'] && - action['custom_category_user_field_key'] - - if action['custom_category_user_field_key'].include?('custom_fields') - field = action['custom_category_user_field_key'].split('.').last - category_id = user.custom_fields[field] - else - category_id = user.send(action['custom_category_user_field_key']) + if action['custom_category_enabled'] + if action['custom_category_wizard_field'] + category_id = data[action['category_id']] + elsif action['custom_category_user_field_key'] + if action['custom_category_user_field_key'].include?('custom_fields') + field = action['custom_category_user_field_key'].split('.').last + category_id = user.custom_fields[field] + else + category_id = user.send(action['custom_category_user_field_key']) + end end else category_id = action['category_id'] end params[:category] = category_id - + topic_custom_fields = {} if action['add_fields'] @@ -378,7 +379,7 @@ class CustomWizard::Builder end end else - value = [value] if key === 'tags' + value = [*value] if key === 'tags' params[key.to_sym] = value end end