From 155eabd3775f608fbbe3a108b055da87b0e6210e Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Mon, 9 Nov 2020 21:44:32 +1100 Subject: [PATCH] Isolated custom field removal --- .../components/custom-field-input.js.es6 | 10 ++++--- .../admin-wizards-custom-fields.js.es6 | 14 ++++++---- .../models/custom-wizard-custom-field.js.es6 | 6 +++++ .../components/custom-field-input.hbs | 10 ++++++- assets/stylesheets/common/wizard-admin.scss | 5 ++++ config/routes.rb | 1 + .../custom_wizard/admin/custom_fields.rb | 23 +++++++++++++--- lib/custom_wizard/custom_field.rb | 26 ++++++++++++++++--- 8 files changed, 78 insertions(+), 17 deletions(-) diff --git a/assets/javascripts/discourse/components/custom-field-input.js.es6 b/assets/javascripts/discourse/components/custom-field-input.js.es6 index 4b1be935..81c626f8 100644 --- a/assets/javascripts/discourse/components/custom-field-input.js.es6 +++ b/assets/javascripts/discourse/components/custom-field-input.js.es6 @@ -13,7 +13,8 @@ export default Component.extend({ tagName: 'tr', topicSerializers: ['topic_view', 'topic_list_item'], postSerializers: ['post'], - categorySerializers: ['basic_category', 'topic_view', 'topic_list_item'], + groupSerializers: ['basic_group'], + categorySerializers: ['basic_category'], klassContent: generateContent(['topic', 'post', 'group', 'category'], 'klass'), typeContent: generateContent(['string', 'boolean', 'integer', 'json'], 'type'), showInputs: or('field.new', 'field.edit'), @@ -37,9 +38,12 @@ export default Component.extend({ close() { if (this.field.edit) { this.set('field.edit', false); - } else { - this.removeField(this.field); } + }, + + destroy() { + this.set('removing', true); + this.removeField(this.field); } } }); \ No newline at end of file diff --git a/assets/javascripts/discourse/controllers/admin-wizards-custom-fields.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-custom-fields.js.es6 index 0a7803b3..600a5f9e 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-custom-fields.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-custom-fields.js.es6 @@ -11,14 +11,10 @@ export default Controller.extend({ actions: { addField() { this.get('customFields').pushObject( - CustomWizardCustomField.create() + CustomWizardCustomField.create({ edit: true }) ); }, - removeField(field) { - this.get('customFields').removeObject(field); - }, - saveFields() { this.set('saving', true); CustomWizardCustomField.saveFields(this.customFields) @@ -29,9 +25,17 @@ export default Controller.extend({ this.set('saveIcon', 'times'); } setTimeout(() => this.set('saveIcon', ''), 5000); + this.get('customFields').setEach('edit', false); }).finally(() => { this.set('saving', false); }); + }, + + removeField(field) { + CustomWizardCustomField.removeField(field) + .then(result => { + this.get('customFields').removeObject(field); + }); } } }); \ No newline at end of file diff --git a/assets/javascripts/discourse/models/custom-wizard-custom-field.js.es6 b/assets/javascripts/discourse/models/custom-wizard-custom-field.js.es6 index 01b88f44..068e943b 100644 --- a/assets/javascripts/discourse/models/custom-wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard-custom-field.js.es6 @@ -23,6 +23,12 @@ CustomWizardCustomField.reopenClass({ custom_fields: customFields }) }).catch(popupAjaxError); + }, + + removeField(field) { + return ajax(`${basePath}/${field.name}`, { + type: 'DELETE' + }).catch(popupAjaxError); } }); diff --git a/assets/javascripts/discourse/templates/components/custom-field-input.hbs b/assets/javascripts/discourse/templates/components/custom-field-input.hbs index fdb3d26d..87b5b0ff 100644 --- a/assets/javascripts/discourse/templates/components/custom-field-input.hbs +++ b/assets/javascripts/discourse/templates/components/custom-field-input.hbs @@ -39,6 +39,14 @@ {{d-button action="edit" icon="pencil-alt"}} - {{d-button action="close" icon="times"}} + {{#if field.edit}} + {{d-button action="close" icon="times"}} + {{else}} + {{#if destroying}} + {{loading-spinner size="small"}} + {{else}} + {{d-button action="destroy" icon="trash-alt"}} + {{/if}} + {{/if}} {{/if}} \ No newline at end of file diff --git a/assets/stylesheets/common/wizard-admin.scss b/assets/stylesheets/common/wizard-admin.scss index 79cfb033..03009b0c 100644 --- a/assets/stylesheets/common/wizard-admin.scss +++ b/assets/stylesheets/common/wizard-admin.scss @@ -589,6 +589,11 @@ td { vertical-align: top; + + label { + margin: 0; + line-height: 30px; + } } td:not(:last-of-type) { diff --git a/config/routes.rb b/config/routes.rb index 311de5e6..764c0fdd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,6 +21,7 @@ Discourse::Application.routes.append do get 'admin/wizards/custom-fields' => 'admin_custom_fields#index' put 'admin/wizards/custom-fields' => 'admin_custom_fields#update' + delete 'admin/wizards/custom-fields/:name' => 'admin_custom_fields#destroy' get 'admin/wizards/submissions' => 'admin_submissions#index' get 'admin/wizards/submissions/:wizard_id' => 'admin_submissions#show' diff --git a/controllers/custom_wizard/admin/custom_fields.rb b/controllers/custom_wizard/admin/custom_fields.rb index add147da..62abaae5 100644 --- a/controllers/custom_wizard/admin/custom_fields.rb +++ b/controllers/custom_wizard/admin/custom_fields.rb @@ -12,16 +12,21 @@ class CustomWizard::AdminCustomFieldsController < CustomWizard::AdminController if saved_field = CustomWizard::CustomField.find(field_param[:name]) CustomWizard::CustomField::ATTRS.each do |attr| - field_data[attr] = field_param[attr] || saved_field.send(attr) + field_data[attr] = saved_field.send(attr) end field_id = saved_field.id end - - fields_to_save.push(CustomWizard::CustomField.new(field_id, field_data)) + + CustomWizard::CustomField::ATTRS.each do |attr| + field_data[attr] = field_param[attr] + end + + field = CustomWizard::CustomField.new(field_id, field_data) + fields_to_save.push(field) end PluginStoreRow.transaction do - fields_to_save.each do |field| + fields_to_save.each do |field| unless field.save raise ActiveRecord::Rollback.new, field.errors.any? ? @@ -34,6 +39,16 @@ class CustomWizard::AdminCustomFieldsController < CustomWizard::AdminController render json: success_json end + def destroy + params.require(:name) + + if CustomWizard::CustomField.destroy(params[:name]) + render json: success_json + else + render json: failed_json + end + end + private def custom_field_params diff --git a/lib/custom_wizard/custom_field.rb b/lib/custom_wizard/custom_field.rb index afce1c9e..adaf8115 100644 --- a/lib/custom_wizard/custom_field.rb +++ b/lib/custom_wizard/custom_field.rb @@ -50,7 +50,7 @@ class ::CustomWizard::CustomField end if self.class.save_to_store(id, key, data) - self.class.reset + self.class.invalidate_cache true else false @@ -65,8 +65,10 @@ class ::CustomWizard::CustomField value = send(attr) i18n_key = "wizard.custom_field.error" - if REQUIRED.include?(attr) && value.blank? - I18n.t("#{i18n_key}.required_attribute", attr: attr) + if value.blank? + if REQUIRED.include?(attr) + add_error(I18n.t("#{i18n_key}.required_attribute", attr: attr)) + end next end @@ -157,7 +159,7 @@ class ::CustomWizard::CustomField new(record.id, data) end - def self.save_to_store(id = nil, key, data) + def self.save_to_store(id = nil, key, data) if id record = PluginStoreRow.find_by(id: id, plugin_name: NAMESPACE, key: key) return false if !record @@ -170,4 +172,20 @@ class ::CustomWizard::CustomField record.save end end + + def self.destroy(name) + if exists?(name) + PluginStoreRow.where(plugin_name: NAMESPACE, key: name).destroy_all + invalidate_cache + true + else + false + end + end + + def self.invalidate_cache + self.reset + Discourse.clear_readonly! + Discourse.request_refresh! + end end \ No newline at end of file