From 4eb7cbc2c82af25163cd30cc00e97f4dab1d846e Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Mon, 24 Jun 2024 12:08:51 +0200 Subject: [PATCH] Add category scoping field to topics selector --- app/controllers/custom_wizard/admin/wizard.rb | 1 + .../custom_wizard/wizard_field_serializer.rb | 1 + .../custom-wizard-topic-selector.js.es6 | 7 +++++++ .../components/wizard-custom-field.js.es6 | 4 ++++ .../discourse/lib/wizard-schema.js.es6 | 1 + .../components/custom-wizard-field-topic.hbs | 1 + .../components/wizard-custom-field.hbs | 19 +++++++++++++++++++ config/locales/client.en.yml | 3 +++ lib/custom_wizard/builder.rb | 4 ++++ lib/custom_wizard/field.rb | 5 ++++- 10 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/controllers/custom_wizard/admin/wizard.rb b/app/controllers/custom_wizard/admin/wizard.rb index 08e7b6d0..955deb3f 100644 --- a/app/controllers/custom_wizard/admin/wizard.rb +++ b/app/controllers/custom_wizard/admin/wizard.rb @@ -115,6 +115,7 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController :preview_template, :placeholder, :can_create_tag, + :category, prefill: mapped_params, content: mapped_params, condition: mapped_params, diff --git a/app/serializers/custom_wizard/wizard_field_serializer.rb b/app/serializers/custom_wizard/wizard_field_serializer.rb index 56e86cc8..af4514ae 100644 --- a/app/serializers/custom_wizard/wizard_field_serializer.rb +++ b/app/serializers/custom_wizard/wizard_field_serializer.rb @@ -18,6 +18,7 @@ class CustomWizard::FieldSerializer < ::ApplicationSerializer :content, :tag_groups, :can_create_tag, + :category, :validations, :max_length, :char_counter, diff --git a/assets/javascripts/discourse/components/custom-wizard-topic-selector.js.es6 b/assets/javascripts/discourse/components/custom-wizard-topic-selector.js.es6 index 8f3ac98f..b3d9cc57 100644 --- a/assets/javascripts/discourse/components/custom-wizard-topic-selector.js.es6 +++ b/assets/javascripts/discourse/components/custom-wizard-topic-selector.js.es6 @@ -42,6 +42,13 @@ export default MultiSelectComponent.extend({ searchParams.restrictToArchetype = "regular"; searchParams.searchForId = true; + if (this.category) { + searchParams.searchContext = { + type: "category", + id: this.category, + }; + } + return searchForTerm(filter, searchParams).then((results) => { if (results?.posts?.length > 0) { return results.posts.mapBy("topic"); diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index 6990cf9f..5aff7f6e 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -164,5 +164,9 @@ export default Component.extend(UndoChanges, { "field.image_upload_id": null, }); }, + + changeCategory(category) { + this.set("field.category", category?.id); + }, }, }); diff --git a/assets/javascripts/discourse/lib/wizard-schema.js.es6 b/assets/javascripts/discourse/lib/wizard-schema.js.es6 index f3c59785..710a3594 100644 --- a/assets/javascripts/discourse/lib/wizard-schema.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-schema.js.es6 @@ -73,6 +73,7 @@ const field = { type: null, condition: null, tag_groups: null, + category: null, }, types: {}, mapped: ["prefill", "content", "condition", "index"], diff --git a/assets/javascripts/discourse/templates/components/custom-wizard-field-topic.hbs b/assets/javascripts/discourse/templates/components/custom-wizard-field-topic.hbs index e19eb48d..fbaf016b 100644 --- a/assets/javascripts/discourse/templates/components/custom-wizard-field-topic.hbs +++ b/assets/javascripts/discourse/templates/components/custom-wizard-field-topic.hbs @@ -1,5 +1,6 @@ {{custom-wizard-topic-selector topics=topics + category=field.category onChange=(action "setValue") options=(hash maximum=field.limit) }} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs index c4d297ff..33a4400d 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs @@ -245,6 +245,25 @@ {{/if}} +{{#if isTopic}} +
+
+ +
+ +
+ +
+
+{{/if}} + {{#wizard-subscription-container}}
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 56e7f015..71d5eeac 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -282,6 +282,9 @@ en: content: "Content" tag_groups: "Tag Groups" can_create_tag: "Can Create Tag" + category: + label: "Category" + none: "Limit to a category..." date_time_format: label: "Format" instructions: "Moment.js format" diff --git a/lib/custom_wizard/builder.rb b/lib/custom_wizard/builder.rb index cf46f57d..eb5369ec 100644 --- a/lib/custom_wizard/builder.rb +++ b/lib/custom_wizard/builder.rb @@ -143,6 +143,10 @@ class CustomWizard::Builder params[:property] = field_template['property'] end + if field_template['type'] === 'topic' + params[:category] = field_template['category'] + end + if (content_inputs = field_template['content']).present? content = CustomWizard::Mapper.new( inputs: content_inputs, diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index 9d884db8..e8e7bbf9 100644 --- a/lib/custom_wizard/field.rb +++ b/lib/custom_wizard/field.rb @@ -22,6 +22,7 @@ class CustomWizard::Field :property, :content, :tag_groups, + :category, :can_create_tag, :preview_template, :placeholder @@ -53,6 +54,7 @@ class CustomWizard::Field @property = attrs[:property] @content = attrs[:content] @tag_groups = attrs[:tag_groups] + @category = attrs[:category] @can_create_tag = attrs[:can_create_tag] @preview_template = attrs[:preview_template] @placeholder = attrs[:placeholder] @@ -132,7 +134,8 @@ class CustomWizard::Field topic: { limit: 1, prefill: nil, - content: nil + content: nil, + category: nil }, group: { prefill: nil,