From bbdf11c84f3efa775585e6eb5210432a7a85a169 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Wed, 1 Apr 2020 23:16:26 +1100 Subject: [PATCH] wip --- .../discourse/controllers/admin-wizard.js.es6 | 11 +- assets/javascripts/discourse/lib/json.js.es6 | 167 ++++++++++-------- .../discourse/models/custom-wizard.js.es6 | 13 +- .../wizard-category-selector.js.es6 | 2 +- controllers/custom_wizard/admin.rb | 2 +- lib/custom_wizard/mapper.rb | 8 +- 6 files changed, 118 insertions(+), 85 deletions(-) diff --git a/assets/javascripts/discourse/controllers/admin-wizard.js.es6 b/assets/javascripts/discourse/controllers/admin-wizard.js.es6 index 8017e0b8..d7557798 100644 --- a/assets/javascripts/discourse/controllers/admin-wizard.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizard.js.es6 @@ -2,6 +2,7 @@ import { default as discourseComputed, observes } from 'discourse-common/utils/d import { notEmpty } from "@ember/object/computed"; import showModal from 'discourse/lib/show-modal'; import { generateId } from '../lib/custom-wizard'; +import { buildProperties } from '../lib/json'; import { dasherize } from "@ember/string"; export default Ember.Controller.extend({ @@ -58,10 +59,16 @@ export default Ember.Controller.extend({ error: null }); - const wizard = this.get('model'); + const wizard = this.model; + + wizard.save().then((result) => { + + this.model.setProperties( + buildProperties(result.wizard) + ); - wizard.save().then(() => { this.set('saving', false); + if (this.get('newWizard')) { this.send("refreshAllWizards"); } else { diff --git a/assets/javascripts/discourse/lib/json.js.es6 b/assets/javascripts/discourse/lib/json.js.es6 index da466471..e0b90043 100644 --- a/assets/javascripts/discourse/lib/json.js.es6 +++ b/assets/javascripts/discourse/lib/json.js.es6 @@ -1,9 +1,17 @@ import { properties } from '../lib/custom-wizard'; import { mappedProperties } from '../lib/mapper'; -import { EmberObject } from '@ember/object'; +import EmberObject from '@ember/object'; function present(val) { - return val && val.length; + if (val === null || val === undefined) { + return false; + } else if (typeof val === 'object') { + return Object.keys(val).length !== 0; + } else if (typeof val === 'string' || val.constructor === Array) { + return val && val.length; + } else { + return false; + } } function mapped(property, type) { @@ -16,14 +24,18 @@ function buildJson(object, type) { properties[type].forEach((p) => { let value = object.get(p); + + if (mapped(p, type)) { + value = buildMappedJson(value); + } if (value) { - result[p] = mapped(p, type) ? buildMappedJson(value) : value; + result[p] = value; } }); return result; -}, +} function buildMappedJson(inputs) { if (!inputs || !inputs.length) return false; @@ -76,7 +88,7 @@ function buildMappedJson(inputs) { return result; } -buildStepJson(object) { +function buildStepJson(object) { let steps = []; let error = null; @@ -143,26 +155,35 @@ function buildObject(json, type) { if (mapped(prop, type)) { let inputs = []; - json[prop].forEach(inputJson => { - let input = {} - - Object.keys(inputJson).forEach(inputKey => { - if (inputKey === 'pairs') { - let pairs = []; - let pairCount = inputJson.pairs.length; - - inputJson.pairs.forEach(pairJson => { - let pair = pairJson; - pair.pairCount = pairCount; - pairs.push(EmberObject.create(pair)); - }); - } else { - input[inputKey] = inputJson[inputKey]; - } + if (present(json[prop])) { + json[prop].forEach(inputJson => { + let input = {} + + Object.keys(inputJson).forEach(inputKey => { + if (inputKey === 'pairs') { + let pairs = []; + let pairCount = inputJson.pairs.length; + + inputJson.pairs.forEach(pairJson => { + let pair = pairJson; + pair.pairCount = pairCount; + + pairs.push( + EmberObject.create(pair) + ); + }); + + input.pairs = pairs; + } else { + input[inputKey] = inputJson[inputKey]; + } + }); + + inputs.push( + EmberObject.create(input) + ); }); - - inputs.push(EmberObject.create(input)); - }); + } params[prop] = Ember.A(inputs); } else { @@ -170,16 +191,15 @@ function buildObject(json, type) { } }); - - EmberObject.create(); + return EmberObject.create(params); } -function buildWizardProperties(json) { +function buildProperties(json) { let steps = Ember.A(); let props = { steps }; - + if (present(json)) { props.id = json.id; props.existingId = true; @@ -189,53 +209,60 @@ function buildWizardProperties(json) { }); if (present(json.steps)) { - json.steps.forEach((s) => { - let fields = Ember.A(); - - if (present(s.fields)) { - s.fields.forEach((f) => { - fields.pushObject(buildObject(f, 'field')); - }); - } - - let actions = Ember.A(); - if (s.actions && s.actions.length) { - s.actions.forEach((a) => { - const actionParams = { isNew: false }; - const action = EmberObject.create($.extend(a, actionParams)); - actions.pushObject(action); - }); - } - - steps.pushObject(EmberObject.create({ - id: s.id, - key: s.key, - title: s.title, - raw_description: s.raw_description, - banner: s.banner, - required_data: s.required_data, - required_data_message: s.required_data_message, - permitted_params: s.permitted_params, - fields, - actions, + json.steps.forEach((stepJson) => { + let stepParams = { isNew: false - })); + }; + + properties.step.forEach((p) => { + stepParams[p] = stepJson[p]; + }); + + stepParams.fields = Ember.A(); + + if (present(stepJson.fields)) { + stepJson.fields.forEach((f) => { + stepParams.fields.pushObject( + buildObject(f, 'field') + ); + }); + } + + stepParams.actions = Ember.A(); + + if (present(stepJson.actions)) { + stepJson.actions.forEach((a) => { + stepParams.actions.pushObject( + buildObject(a, 'action') + ); + }); + } + + steps.pushObject( + EmberObject.create(stepParams) + ); }); }; } else { - props['id'] = ''; - props['name'] = ''; - props['background'] = ''; - props['save_submissions'] = true; - props['multiple_submissions'] = false; - props['after_signup'] = false; - props['after_time'] = false; - props['required'] = false; - props['prompt_completion'] = false; - props['restart_on_revisit'] = false; - props['permitted'] = null; - props['steps'] = Ember.A(); + props.id = ''; + props.name = ''; + props.background = ''; + props.save_submissions = true; + props.multiple_submissions = false; + props.after_signup = false; + props.after_time = false; + props.required = false; + props.prompt_completion = false; + props.restart_on_revisit = false; + props.permitted = null; + props.steps = Ember.A(); } return props; +} + +export { + buildStepJson, + buildJson, + buildProperties } \ No newline at end of file diff --git a/assets/javascripts/discourse/models/custom-wizard.js.es6 b/assets/javascripts/discourse/models/custom-wizard.js.es6 index 2a546e89..4f987d9f 100644 --- a/assets/javascripts/discourse/models/custom-wizard.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard.js.es6 @@ -1,11 +1,10 @@ import { ajax } from 'discourse/lib/ajax'; import EmberObject from "@ember/object"; -import { buildStepJson, buildJson, buildWizardProperties } from '../lib/json'; +import { buildStepJson, buildJson, buildProperties } from '../lib/json'; const CustomWizard = EmberObject.extend({ save() { return new Ember.RSVP.Promise((resolve, reject) => { - let wizardJson = buildJson(this, 'wizard'); if (wizardJson.after_time && !wizardJson.after_time_scheduled) { @@ -35,6 +34,7 @@ const CustomWizard = EmberObject.extend({ wizard: JSON.stringify(wizardJson) } }).then((result) => { + console.log('result: ', result); if (result.error) { reject(result); } else { @@ -59,7 +59,9 @@ CustomWizard.reopenClass({ return ajax("/admin/wizards/custom/all", { type: 'GET' }).then(result => { - return result.wizards.map(w => CustomWizard.create(w)); + return result.wizards.map(wizard => { + return CustomWizard.create(wizard); + }); }); }, @@ -73,10 +75,7 @@ CustomWizard.reopenClass({ create(wizardJson = {}) { const wizard = this._super.apply(this); - - - wizard.setProperties(buildWizardProperties(wizardJson)); - + wizard.setProperties(buildProperties(wizardJson)); return wizard; } }); diff --git a/assets/javascripts/wizard/components/wizard-category-selector.js.es6 b/assets/javascripts/wizard/components/wizard-category-selector.js.es6 index 98c032cc..83d61566 100644 --- a/assets/javascripts/wizard/components/wizard-category-selector.js.es6 +++ b/assets/javascripts/wizard/components/wizard-category-selector.js.es6 @@ -1,4 +1,4 @@ -import CategorySelector from 'discourse/components/category-selector'; +import CategorySelector from 'select-kit/components/category-selector'; import { computed } from "@ember/object"; import { makeArray } from "discourse-common/lib/helpers"; diff --git a/controllers/custom_wizard/admin.rb b/controllers/custom_wizard/admin.rb index 23944c1e..da1bde0f 100644 --- a/controllers/custom_wizard/admin.rb +++ b/controllers/custom_wizard/admin.rb @@ -104,7 +104,7 @@ class CustomWizard::AdminController < ::ApplicationController PluginStore.set('custom_wizard', wizard["id"], wizard) - render json: success_json + render json: success_json.merge(wizard: wizard) end def remove diff --git a/lib/custom_wizard/mapper.rb b/lib/custom_wizard/mapper.rb index dad9f983..a258f75a 100644 --- a/lib/custom_wizard/mapper.rb +++ b/lib/custom_wizard/mapper.rb @@ -4,7 +4,7 @@ class CustomWizard::Mapper USER_FIELDS = ['name', 'username', 'email', 'date_of_birth', 'title', 'locale', 'trust_level'] PROFILE_FIELDS = ['location', 'website', 'bio_raw'] OPERATORS = { - equal: '=', + equal: '==', greater: '>', less: '<', greater_or_equal: '>=', @@ -53,11 +53,11 @@ class CustomWizard::Mapper pairs.each do |pair| key = map_field(pair['key'], pair['key_type']) value = map_field(pair['value'], pair['value_type']) - + begin failed = true unless key.public_send(operator(pair['connector']), value) - rescue => e - byebug + rescue NoMethodError + # end end