From cc538a4f2594b5777fd9cfba83fd6cf32e2bac5e Mon Sep 17 00:00:00 2001 From: chowyiyin Date: Thu, 27 May 2021 10:07:37 +0800 Subject: [PATCH 1/3] refactor: remove form-factory.client.service.js --- src/public/main.js | 1 - .../create-form-modal.client.controller.js | 47 +++++++------------ .../services/form-factory.client.service.js | 27 ----------- 3 files changed, 18 insertions(+), 57 deletions(-) delete mode 100644 src/public/modules/forms/services/form-factory.client.service.js diff --git a/src/public/main.js b/src/public/main.js index e29766efe4..e683cbdcf1 100644 --- a/src/public/main.js +++ b/src/public/main.js @@ -257,7 +257,6 @@ require('./modules/forms/config/forms.client.routes.js') // forms services require('./modules/forms/services/form-fields.client.service.js') -require('./modules/forms/services/form-factory.client.service.js') require('./modules/forms/services/form-api.client.factory.js') require('./modules/forms/services/form-error.client.factory.js') require('./modules/forms/services/spcp-session.client.factory.js') diff --git a/src/public/modules/forms/admin/controllers/create-form-modal.client.controller.js b/src/public/modules/forms/admin/controllers/create-form-modal.client.controller.js index a82b2e615c..422d7526c7 100644 --- a/src/public/modules/forms/admin/controllers/create-form-modal.client.controller.js +++ b/src/public/modules/forms/admin/controllers/create-form-modal.client.controller.js @@ -37,7 +37,7 @@ angular 'responseModeEnum', 'createFormModalOptions', 'FormToDuplicate', - 'FormFactory', + 'FormApi', 'FormFields', 'GTag', 'FormSgSdk', @@ -56,7 +56,7 @@ function CreateFormModalController( responseModeEnum, createFormModalOptions, FormToDuplicate, - FormFactory, + FormApi, FormFields, GTag, FormSgSdk, @@ -257,23 +257,18 @@ function CreateFormModalController( const formMode = vm.mode switch (formMode) { case 'duplicate': { - $q.when( - FormFactory.generateForm( - formMode, - formParams, - FormToDuplicate._id, - ), - ).then((data) => { - vm.closeCreateModal() - externalScope.onDuplicateSuccess(data) - }, handleCreateFormError) + $q.when(FormApi.save(FormToDuplicate._id, formParams)).then( + (data) => { + vm.closeCreateModal() + externalScope.onDuplicateSuccess(data) + }, + handleCreateFormError, + ) break } case 'useTemplate': { const { form } = externalScope - $q.when( - FormFactory.generateForm(formMode, formParams, form._id), - ).then((data) => { + $q.when(FormApi.useTemplate(form._id, formParams)).then((data) => { vm.closeCreateModal() vm.goToWithId('viewForm', data._id + '') GTag.examplesClickCreateNewForm(form) @@ -283,23 +278,17 @@ function CreateFormModalController( case 'createFromTemplate': { // Create new form from template selected const newForm = Object.assign({}, vm.template, formParams) - $q.when(FormFactory.generateForm('create', newForm)).then( - (data) => { - vm.closeCreateModal() - vm.goToWithId('viewForm', data._id + '') - }, - handleCreateFormError, - ) + $q.when(FormApi.create(newForm)).then((data) => { + vm.closeCreateModal() + vm.goToWithId('viewForm', data._id + '') + }, handleCreateFormError) break } case 'create': // Create form - $q.when(FormFactory.generateForm(formMode, formParams)).then( - (data) => { - vm.closeCreateModal() - vm.goToWithId('viewForm', data._id + '') - }, - handleCreateFormError, - ) + $q.when(FormApi.create(formParams)).then((data) => { + vm.closeCreateModal() + vm.goToWithId('viewForm', data._id + '') + }, handleCreateFormError) break } } diff --git a/src/public/modules/forms/services/form-factory.client.service.js b/src/public/modules/forms/services/form-factory.client.service.js deleted file mode 100644 index 79ba86aae2..0000000000 --- a/src/public/modules/forms/services/form-factory.client.service.js +++ /dev/null @@ -1,27 +0,0 @@ -angular.module('forms').service('FormFactory', ['FormApi', FormFactory]) - -function FormFactory(FormApi) { - this.generateForm = generateForm - - /** - * Creates a new form from one of three options - * @param {string} mode One of 'create', 'duplicate', 'useTemplate' - * @param {Object} params Parameters for the form object. This can be any valid - * parameter in the backend Form model - * @param {string} [formId] Form ID for duplication an admin's existing form, - * or using the template of another form - * when responseMode is 'encrypt' - */ - function generateForm(mode, params, formId) { - switch (mode) { - case 'create': - return FormApi.createForm(params) - case 'duplicate': - return FormApi.duplicateForm(formId, params) - case 'useTemplate': - return FormApi.useTemplate(formId, params) - default: - throw new Error('Unsupported mode of form generation.') - } - } -} From 5c9d50bf51f622e5b037ae0b29fcfd67021d7ffa Mon Sep 17 00:00:00 2001 From: chowyiyin Date: Thu, 27 May 2021 17:16:26 +0800 Subject: [PATCH 2/3] fix: wrap form params in object --- .../admin/controllers/create-form-modal.client.controller.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/public/modules/forms/admin/controllers/create-form-modal.client.controller.js b/src/public/modules/forms/admin/controllers/create-form-modal.client.controller.js index 422d7526c7..26be4dc2a8 100644 --- a/src/public/modules/forms/admin/controllers/create-form-modal.client.controller.js +++ b/src/public/modules/forms/admin/controllers/create-form-modal.client.controller.js @@ -278,14 +278,14 @@ function CreateFormModalController( case 'createFromTemplate': { // Create new form from template selected const newForm = Object.assign({}, vm.template, formParams) - $q.when(FormApi.create(newForm)).then((data) => { + $q.when(FormApi.create({ form: newForm })).then((data) => { vm.closeCreateModal() vm.goToWithId('viewForm', data._id + '') }, handleCreateFormError) break } case 'create': // Create form - $q.when(FormApi.create(formParams)).then((data) => { + $q.when(FormApi.create({ form: formParams })).then((data) => { vm.closeCreateModal() vm.goToWithId('viewForm', data._id + '') }, handleCreateFormError) From 8a205a92ec8577457e219e9d6f8ec56b06e07c93 Mon Sep 17 00:00:00 2001 From: chowyiyin Date: Wed, 9 Jun 2021 15:22:59 +0800 Subject: [PATCH 3/3] refactor: change function names and arguments based on formapi changes --- .../create-form-modal.client.controller.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/public/modules/forms/admin/controllers/create-form-modal.client.controller.js b/src/public/modules/forms/admin/controllers/create-form-modal.client.controller.js index 26be4dc2a8..f34a61caf7 100644 --- a/src/public/modules/forms/admin/controllers/create-form-modal.client.controller.js +++ b/src/public/modules/forms/admin/controllers/create-form-modal.client.controller.js @@ -257,13 +257,12 @@ function CreateFormModalController( const formMode = vm.mode switch (formMode) { case 'duplicate': { - $q.when(FormApi.save(FormToDuplicate._id, formParams)).then( - (data) => { - vm.closeCreateModal() - externalScope.onDuplicateSuccess(data) - }, - handleCreateFormError, - ) + $q.when( + FormApi.duplicateForm(FormToDuplicate._id, formParams), + ).then((data) => { + vm.closeCreateModal() + externalScope.onDuplicateSuccess(data) + }, handleCreateFormError) break } case 'useTemplate': { @@ -278,14 +277,14 @@ function CreateFormModalController( case 'createFromTemplate': { // Create new form from template selected const newForm = Object.assign({}, vm.template, formParams) - $q.when(FormApi.create({ form: newForm })).then((data) => { + $q.when(FormApi.createForm(newForm)).then((data) => { vm.closeCreateModal() vm.goToWithId('viewForm', data._id + '') }, handleCreateFormError) break } case 'create': // Create form - $q.when(FormApi.create({ form: formParams })).then((data) => { + $q.when(FormApi.createForm(formParams)).then((data) => { vm.closeCreateModal() vm.goToWithId('viewForm', data._id + '') }, handleCreateFormError)