From 5d749fe42651c1d2abd3e5e8eab15fb01bb87839 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Wed, 27 Jan 2021 10:38:26 +0530 Subject: [PATCH] some changes --- .../realtime-validation-settings.js.es6 | 5 +++++ .../components/wizard-custom-field.js.es6 | 13 +++++++++++++ .../discourse/lib/wizard-schema.js.es6 | 4 ++++ .../discourse/routes/admin-wizards-wizard.js.es6 | 5 +++-- .../components/realtime-validation-settings.hbs | 15 +++++++++++++++ .../templates/components/wizard-custom-field.hbs | 4 ++++ .../wizard/components/alpha-validator.js.es6 | 13 +++++++++++++ config/locales/client.en.yml | 3 +++ controllers/custom_wizard/admin/wizard.rb | 1 + lib/custom_wizard/realtime_validation.rb | 7 +++++++ plugin.rb | 1 + 11 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 assets/javascripts/discourse/components/realtime-validation-settings.js.es6 create mode 100644 assets/javascripts/discourse/templates/components/realtime-validation-settings.hbs create mode 100644 assets/javascripts/wizard/components/alpha-validator.js.es6 create mode 100644 lib/custom_wizard/realtime_validation.rb diff --git a/assets/javascripts/discourse/components/realtime-validation-settings.js.es6 b/assets/javascripts/discourse/components/realtime-validation-settings.js.es6 new file mode 100644 index 00000000..c9862978 --- /dev/null +++ b/assets/javascripts/discourse/components/realtime-validation-settings.js.es6 @@ -0,0 +1,5 @@ +import Component from "@ember/component"; + +Component.extend({ + +}); \ No newline at end of file diff --git a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 index 97f003df..87b27a89 100644 --- a/assets/javascripts/discourse/components/wizard-custom-field.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-field.js.es6 @@ -4,6 +4,7 @@ import { computed } from "@ember/object"; import { selectKitContent } from '../lib/wizard'; import UndoChanges from '../mixins/undo-changes'; import Component from "@ember/component"; +import wizardSchema from '../lib/wizard-schema'; export default Component.extend(UndoChanges, { componentType: 'field', @@ -26,6 +27,18 @@ export default Component.extend(UndoChanges, { showAdvanced: alias('field.type'), messageUrl: 'https://thepavilion.io/t/2809', + @discourseComputed('field.type') + validations(type) { + const applicableToField = []; + for(let validation in wizardSchema.field.validations) { + if (wizardSchema.field.validations[validation].includes(type)) { + applicableToField.push(validation); + } + } + + return applicableToField; + }, + @discourseComputed('field.type') isDateTime(type) { return ['date_time', 'date', 'time'].indexOf(type) > -1; diff --git a/assets/javascripts/discourse/lib/wizard-schema.js.es6 b/assets/javascripts/discourse/lib/wizard-schema.js.es6 index 7d34a79d..36e1bb70 100644 --- a/assets/javascripts/discourse/lib/wizard-schema.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-schema.js.es6 @@ -244,6 +244,10 @@ export function buildFieldTypes(types) { wizardSchema.field.types = types; } +export function buildFieldValidations(validations) { + wizardSchema.field.validations = validations; +} + if (Discourse.SiteSettings.wizard_apis_enabled) { wizardSchema.action.types.send_to_api = { api: null, diff --git a/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6 index 588936f8..e92e9d60 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards-wizard.js.es6 @@ -1,5 +1,5 @@ import DiscourseRoute from "discourse/routes/discourse"; -import { buildFieldTypes } from '../lib/wizard-schema'; +import { buildFieldTypes, buildFieldValidations } from '../lib/wizard-schema'; import EmberObject, { set } from "@ember/object"; import { A } from "@ember/array"; import { all } from "rsvp"; @@ -12,7 +12,8 @@ export default DiscourseRoute.extend({ afterModel(model) { buildFieldTypes(model.field_types); - + buildFieldValidations(model.realtime_validations); + return all([ this._getThemes(model), this._getApis(model), diff --git a/assets/javascripts/discourse/templates/components/realtime-validation-settings.hbs b/assets/javascripts/discourse/templates/components/realtime-validation-settings.hbs new file mode 100644 index 00000000..2e6ee32e --- /dev/null +++ b/assets/javascripts/discourse/templates/components/realtime-validation-settings.hbs @@ -0,0 +1,15 @@ +
+ +
+ +{{#each validations as |validation|}} + + {{i18n (concat 'admin.wizard.field.validations.' validation)}} {{input type="checkbox" }} + +
+ {{input type="radio"}} :before +
+
+ {{input type="radio"}} :after +
+{{/each}} \ 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 563ab716..20b3b602 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-field.hbs @@ -221,4 +221,8 @@ {{/if}} + + {{#if validations}} + {{realtime-validation-settings field=field validations=validations}} + {{/if}} {{/if}} diff --git a/assets/javascripts/wizard/components/alpha-validator.js.es6 b/assets/javascripts/wizard/components/alpha-validator.js.es6 new file mode 100644 index 00000000..dfa16406 --- /dev/null +++ b/assets/javascripts/wizard/components/alpha-validator.js.es6 @@ -0,0 +1,13 @@ +import WizardFieldValidator from "../../wizard/components/validator"; + +export default WizardFieldValidator.extend({ + validMessageKey: 'hello', + invalidMessageKey: 'world', + validate() { + if(this.field.value) { + this.field.value.length > 0 ? this.set('isValid', true) : this.set('isValid', false); + } else { + this.set('isValid', false); + } + } +}); \ No newline at end of file diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 28304120..9e91654e 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -176,6 +176,9 @@ en: date_time_format: label: "Format" instructions: "Moment.js format" + validations: + header: "Realtime Validation Settings" + suggested_topics: "Suggested Topics" type: text: "Text" diff --git a/controllers/custom_wizard/admin/wizard.rb b/controllers/custom_wizard/admin/wizard.rb index 9859f115..d27ae036 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/controllers/custom_wizard/admin/wizard.rb @@ -8,6 +8,7 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController each_serializer: CustomWizard::BasicWizardSerializer ), field_types: CustomWizard::Field.types, + realtime_validations: CustomWizard::RealtimeValidation.types, custom_fields: custom_field_list ) end diff --git a/lib/custom_wizard/realtime_validation.rb b/lib/custom_wizard/realtime_validation.rb new file mode 100644 index 00000000..062045bb --- /dev/null +++ b/lib/custom_wizard/realtime_validation.rb @@ -0,0 +1,7 @@ +class CustomWizard::RealtimeValidation + cattr_accessor :types + @@types ||= { + suggested_topics: [:text] + } + end + \ No newline at end of file diff --git a/plugin.rb b/plugin.rb index 52b8348c..0521cc10 100644 --- a/plugin.rb +++ b/plugin.rb @@ -58,6 +58,7 @@ after_initialize do ../lib/custom_wizard/cache.rb ../lib/custom_wizard/custom_field.rb ../lib/custom_wizard/field.rb + ../lib/custom_wizard/realtime_validation.rb ../lib/custom_wizard/mapper.rb ../lib/custom_wizard/log.rb ../lib/custom_wizard/step_updater.rb