Skip to content

Commit

Permalink
refactor: replace $resource in angularjs form-api.client.factory.js w…
Browse files Browse the repository at this point in the history
…ith typescript FormService (#1947)

* refactor: replace  object with typescript FormService

* test: add test for FormService

* refactor: edit calls to fit new function signature and remove angularjs

* rfix: add missing parameter in transferOwner and cfix reading error message in collaborators client controller

* refactor: add .when to all FormApi calls

* refactor: extract interceptors from FormService and replace original interceptors

* test: remove interceptor from FormService tests

* refactor: add .when to FormApi calls

* docs: add documentation and reaname some functions

* refactor: edit calls to fit new function signature and remove angularjs

* refactor: add .when to all FormApi calls

* refactor: add .when to FormApi calls

* refactor: rename getDashboardViews to getDashboardView

* fix: rename getDashboardViews in form-api.client.factory.js

* refactor: remove headers for IE11 support

* refactor: split FormService into different services

* split test into relevant files

* refactor: update other files after rename of AdminFormService

* refactor: change services and rename formapi functions

* refactor: remove outdated comment

* refactor: remove unnecessary

* docs: update jsdocs regarding AdminFormService

* refactor: remove unnecessary q.when

* refactor: change from absolute to relative import in AdminViewFormService

* refactor: shift deleteForm to UpdateFormService

* fix: change service deleteForm

* refactor: make tcalls to transformations explicit

* test: edit tests to improve readability

* refactor: update imports

* test: fix imports in tests

* style: fix eslint style in tests

* style: fix prettier errors

* fix: fix import statement in AdminSubmissionService

* fix: fix import statement in test

* fix: replace formApi.template with formApi.queryTemplate

* fix: fix incorrect endpoint for query and use of template
  • Loading branch information
chowyiyin authored Jun 9, 2021
1 parent fe35c55 commit e8f6c65
Show file tree
Hide file tree
Showing 24 changed files with 948 additions and 188 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { LogicType } = require('../../../../../types')
const AdminFormService = require('../../../../services/AdminFormService')
const UpdateFormService = require('../../../../services/UpdateFormService')

angular.module('forms').component('editLogicComponent', {
templateUrl: 'modules/forms/admin/componentViews/edit-logic.client.view.html',
Expand Down Expand Up @@ -82,7 +82,7 @@ function editLogicComponentController($uibModal, FormFields, Toastr, $q) {

vm.deleteLogic = function (logicIndex) {
const logicIdToDelete = vm.myform.form_logics[logicIndex]._id
$q.when(AdminFormService.deleteFormLogic(vm.myform._id, logicIdToDelete))
$q.when(UpdateFormService.deleteFormLogic(vm.myform._id, logicIdToDelete))
.then(() => {
vm.myform.form_logics.splice(logicIndex, 1)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { StatusCodes } = require('http-status-codes')
const get = require('lodash/get')
const { LogicType } = require('../../../../../types')
const AdminFormService = require('../../../../services/AdminFormService')
const UpdateFormService = require('../../../../services/UpdateFormService')
const FieldFactory = require('../../helpers/field-factory')
const { UPDATE_FORM_TYPES } = require('../constants/update-form-types')

Expand Down Expand Up @@ -191,7 +191,9 @@ function AdminFormController(
case UPDATE_FORM_TYPES.CreateField: {
const { body } = update
return $q
.when(AdminFormService.createSingleFormField($scope.myform._id, body))
.when(
UpdateFormService.createSingleFormField($scope.myform._id, body),
)
.then((updatedFormField) => {
// !!! Convert retrieved form field objects into their class counterparts.
const updatedFieldClass =
Expand All @@ -210,7 +212,7 @@ function AdminFormController(
const { fieldId } = update
return $q
.when(
AdminFormService.deleteSingleFormField($scope.myform._id, fieldId),
UpdateFormService.deleteSingleFormField($scope.myform._id, fieldId),
)
.then(() => {
// Success, remove deleted form field
Expand All @@ -225,7 +227,7 @@ function AdminFormController(
const { fieldId, body } = update
return $q
.when(
AdminFormService.updateSingleFormField(
UpdateFormService.updateSingleFormField(
$scope.myform._id,
fieldId,
body,
Expand Down Expand Up @@ -253,7 +255,7 @@ function AdminFormController(
const { fieldId } = update
return $q
.when(
AdminFormService.duplicateSingleFormField(
UpdateFormService.duplicateSingleFormField(
$scope.myform._id,
fieldId,
),
Expand All @@ -277,7 +279,7 @@ function AdminFormController(

return $q
.when(
AdminFormService.reorderSingleFormField(
UpdateFormService.reorderSingleFormField(
$scope.myform._id,
fieldId,
newPosition,
Expand All @@ -295,8 +297,10 @@ function AdminFormController(
.catch(handleUpdateError)
}
default:
return FormApi.update({ formId: $scope.myform._id }, { form: update })
.$promise.then((savedForm) => {
// This block should not be reached. All updateForm calls should have an update type.
return $q
.when(FormApi.updateForm($scope.myform._id, update))
.then((savedForm) => {
// Updating this form updates lastModified
// and also updates myform if a formToUse is passed in
$scope.myform = savedForm
Expand All @@ -307,7 +311,7 @@ function AdminFormController(

$scope.updateFormEndPage = (newEndPage) => {
return $q
.when(AdminFormService.updateFormEndPage($scope.myform._id, newEndPage))
.when(UpdateFormService.updateFormEndPage($scope.myform._id, newEndPage))
.then((updatedEndPage) => {
$scope.myform.endPage = updatedEndPage
})
Expand All @@ -317,7 +321,7 @@ function AdminFormController(
$scope.updateFormStartPage = (newStartPage) => {
return $q
.when(
AdminFormService.updateFormStartPage($scope.myform._id, newStartPage),
UpdateFormService.updateFormStartPage($scope.myform._id, newStartPage),
)
.then((updatedStartPage) => {
$scope.myform.startPage = updatedStartPage
Expand All @@ -333,7 +337,7 @@ function AdminFormController(
$scope.updateFormSettings = (settingsToUpdate) => {
return $q
.when(
AdminFormService.updateFormSettings(
UpdateFormService.updateFormSettings(
$scope.myform._id,
settingsToUpdate,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { get } = require('lodash')
const { StatusCodes } = require('http-status-codes')
const AdminFormService = require('../../../../services/AdminFormService')
const UpdateFormService = require('../../../../services/UpdateFormService')

angular
.module('forms')
Expand Down Expand Up @@ -73,17 +73,14 @@ function CollaboratorModalController(
return
}

FormApi.transferOwner(
{ formId: $scope.myform._id },
{ email: $scope.transferOwnerEmail },
)
.$promise.then((res) => {
$q.when(FormApi.transferOwner($scope.myform._id, $scope.transferOwnerEmail))
.then((res) => {
$scope.myform = res.form
externalScope.refreshFormDataFromCollab($scope.myform)
Toastr.success('Form ownership transferred. You are now an Editor.')
})
.catch((err) => {
Toastr.error(err.data.message)
Toastr.error(err.response.data.message)
return
})
.finally(() => {
Expand All @@ -92,13 +89,16 @@ function CollaboratorModalController(
}

/**
* Calls AdminFormService to update the permission list (collaborators) of a form
* Calls UpdateFormService to update the permission list (collaborators) of a form
* @param {Array} permissionList - New permission list for the form
*/
$scope.updatePermissionList = (permissionList) => {
return $q
.when(
AdminFormService.updateCollaborators($scope.myform._id, permissionList),
UpdateFormService.updateCollaborators(
$scope.myform._id,
permissionList,
),
)
.then((updatedCollaborators) => {
$scope.myform.permissionList = updatedCollaborators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ angular
'FormSgSdk',
'externalScope',
'MailTo',
'$q',
CreateFormModalController,
])

Expand All @@ -61,6 +62,7 @@ function CreateFormModalController(
FormSgSdk,
externalScope,
MailTo,
$q,
) {
const vm = this

Expand Down Expand Up @@ -255,23 +257,23 @@ function CreateFormModalController(
const formMode = vm.mode
switch (formMode) {
case 'duplicate': {
FormFactory.generateForm(
formMode,
formParams,
FormToDuplicate._id,
).$promise.then((data) => {
$q.when(
FormFactory.generateForm(
formMode,
formParams,
FormToDuplicate._id,
),
).then((data) => {
vm.closeCreateModal()
externalScope.onDuplicateSuccess(data)
}, handleCreateFormError)
break
}
case 'useTemplate': {
const { form } = externalScope
FormFactory.generateForm(
formMode,
formParams,
form._id,
).$promise.then((data) => {
$q.when(
FormFactory.generateForm(formMode, formParams, form._id),
).then((data) => {
vm.closeCreateModal()
vm.goToWithId('viewForm', data._id + '')
GTag.examplesClickCreateNewForm(form)
Expand All @@ -281,7 +283,7 @@ function CreateFormModalController(
case 'createFromTemplate': {
// Create new form from template selected
const newForm = Object.assign({}, vm.template, formParams)
FormFactory.generateForm('create', newForm).$promise.then(
$q.when(FormFactory.generateForm('create', newForm)).then(
(data) => {
vm.closeCreateModal()
vm.goToWithId('viewForm', data._id + '')
Expand All @@ -291,7 +293,7 @@ function CreateFormModalController(
break
}
case 'create': // Create form
FormFactory.generateForm(formMode, formParams).$promise.then(
$q.when(FormFactory.generateForm(formMode, formParams)).then(
(data) => {
vm.closeCreateModal()
vm.goToWithId('viewForm', data._id + '')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ angular
'$uibModalInstance',
'externalScope',
'FormApi',
'$q',
DeleteFormModalController,
])

function DeleteFormModalController($uibModalInstance, externalScope, FormApi) {
function DeleteFormModalController(
$uibModalInstance,
externalScope,
FormApi,
$q,
) {
const vm = this

vm.cancel = function () {
Expand All @@ -28,9 +34,7 @@ function DeleteFormModalController($uibModalInstance, externalScope, FormApi) {
}`,
)
}
FormApi.delete({
formId: vm.myforms[formIndex]._id,
}).$promise.then(
$q.when(FormApi.deleteForm(vm.myforms[formIndex]._id)).then(
function () {
vm.myforms.splice(formIndex, 1)
vm.cancel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { range } = require('lodash')
const { LogicType } = require('../../../../../types')
const FormLogic = require('../../services/form-logic/form-logic.client.service')
const AdminFormService = require('../../../../services/AdminFormService')
const UpdateFormService = require('../../../../services/UpdateFormService')

angular
.module('forms')
Expand Down Expand Up @@ -272,7 +272,7 @@ function EditLogicModalController(
}

vm.createNewLogic = function (newLogic) {
$q.when(AdminFormService.createFormLogic(vm.myform._id, newLogic))
$q.when(UpdateFormService.createFormLogic(vm.myform._id, newLogic))
.then((createdLogic) => {
vm.formLogics.push(createdLogic)
externalScope.myform.form_logics.push(createdLogic) // update global myform
Expand All @@ -290,7 +290,7 @@ function EditLogicModalController(
vm.updateExistingLogic = function (logicIndex, updatedLogic) {
const logicIdToUpdate = vm.formLogics[logicIndex]._id
$q.when(
AdminFormService.updateFormLogic(
UpdateFormService.updateFormLogic(
vm.myform._id,
logicIdToUpdate,
updatedLogic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ angular
'$timeout',
'$window',
'Toastr',
'$q',
ListFormsController,
])

Expand All @@ -29,6 +30,7 @@ function ListFormsController(
$timeout,
$window,
Toastr,
$q,
) {
const vm = this

Expand Down Expand Up @@ -74,7 +76,7 @@ function ListFormsController(
// Massage user email into a name
turnEmailToName()

FormApi.query(function (_forms) {
$q.when(FormApi.getDashboardView()).then((_forms) => {
vm.myforms = _forms
})
}
Expand Down Expand Up @@ -192,9 +194,9 @@ function ListFormsController(
resolve: {
FormToDuplicate: () => {
// Retrieve the form so that we can populate the modal with any existing email recipients
return FormApi.preview({
formId: vm.myforms[formIndex]._id,
}).$promise.then((res) => res.form)
return $q
.when(FormApi.previewForm(vm.myforms[formIndex]._id))
.then((res) => res.form)
},
createFormModalOptions: () => ({ mode: 'duplicate' }),
externalScope: () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const get = require('lodash/get')
const FieldVerificationService = require('../../../../services/FieldVerificationService')
const PublicFormAuthService = require('../../../../services/PublicFormAuthService')
const PublicFormService = require('../../../../services/PublicFormService')
const AdminFormService = require('../../../../services/AdminFormService')
const UpdateFormService = require('../../../../services/UpdateFormService')
const {
getVisibleFieldIds,
getLogicUnitPreventingSubmit,
Expand Down Expand Up @@ -324,7 +324,7 @@ function submitFormDirective(
const content = { responses: submissionContent.responses }

const submitFn = form.isPreview
? AdminFormService.submitEmailModeFormPreview
? UpdateFormService.submitEmailModeFormPreview
: PublicFormService.submitEmailModeForm

return $q
Expand All @@ -341,7 +341,7 @@ function submitFormDirective(
}
case responseModeEnum.ENCRYPT: {
const submitFn = form.isPreview
? AdminFormService.submitStorageModeFormPreview
? UpdateFormService.submitStorageModeFormPreview
: PublicFormService.submitStorageModeForm

return $q
Expand Down
Loading

0 comments on commit e8f6c65

Please sign in to comment.