From fe62da1c54b28b3a1074961acc83ed236f6eacc1 Mon Sep 17 00:00:00 2001 From: Kar Rui Lau Date: Mon, 28 Jun 2021 12:01:28 +0800 Subject: [PATCH] fix: correctly retrieve targetFormId for redirect state this fixes the bug where the targetFormId was incorrectly retrieving the first 24 characters of the URL, which will fail as the API URLs have changed to be more semantic. instead, take in the targetFormId directly as we already know the formId. --- package.json | 2 +- .../forms/config/forms.client.routes.js | 6 +-- .../forms/services/form-api.client.factory.js | 43 ++++++++++--------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 4798bdfd66..fb8587bbb2 100644 --- a/package.json +++ b/package.json @@ -254,4 +254,4 @@ "webpack-merge": "^4.1.3", "worker-loader": "^2.0.0" } -} \ No newline at end of file +} diff --git a/src/public/modules/forms/config/forms.client.routes.js b/src/public/modules/forms/config/forms.client.routes.js index 07bb491756..6a098605af 100644 --- a/src/public/modules/forms/config/forms.client.routes.js +++ b/src/public/modules/forms/config/forms.client.routes.js @@ -109,9 +109,9 @@ angular.module('forms').config([ resolve: { FormData: [ 'FormApi', - '$transition$', - function (FormApi, $transition$) { - return FormApi.getAdminForm($transition$.params().formId) + '$stateParams', + function (FormApi, $stateParams) { + return FormApi.getAdminForm($stateParams.formId) }, ], }, diff --git a/src/public/modules/forms/services/form-api.client.factory.js b/src/public/modules/forms/services/form-api.client.factory.js index c26de14407..9f6ef1c0fd 100644 --- a/src/public/modules/forms/services/form-api.client.factory.js +++ b/src/public/modules/forms/services/form-api.client.factory.js @@ -12,19 +12,6 @@ angular .module('forms') .factory('FormApi', ['FormErrorService', 'FormFields', FormApi]) -// Helper function for getting formID from path starting with /:formId/. -// If form ID is not found, returns an empty string. -const extractFormId = (path) => { - if (!path) { - return '' - } - const formId = path.substring(1, 25) - if (formId.length !== 24) { - return '' - } - return formId -} - /** * Service for making API calls to /:formId/:accessMode endpoint, which is used * for all CRUD operations for forms. @@ -75,12 +62,12 @@ function FormApi(FormErrorService, FormFields) { * If redirectOnError is true, this is the redirect state which will be passed to FormErrorService */ const handleError = (err, errorParams) => { - const { redirectOnError, errorTargetState } = errorParams + const { redirectOnError, errorTargetState, targetFormId } = errorParams if (redirectOnError) { FormErrorService.redirect({ response: err.response, targetState: errorTargetState, - targetFormId: extractFormId(get(err.response, 'config.url')), + targetFormId, }) } else { throw err // just pass error on @@ -115,7 +102,11 @@ function FormApi(FormErrorService, FormFields) { AdminViewFormService.getAdminFormView, null, injectMyInfo, - { redirectOnError: true, errorTargetState: 'viewForm' }, + { + redirectOnError: true, + errorTargetState: 'viewForm', + targetFormId: formId, + }, formId, ), getPublicForm: (formId) => @@ -123,7 +114,7 @@ function FormApi(FormErrorService, FormFields) { PublicFormService.getPublicFormView, null, injectMyInfo, - { redirectOnError: true }, + { redirectOnError: true, targetFormId: formId }, formId, ), updateForm: (formId, update) => @@ -165,7 +156,11 @@ function FormApi(FormErrorService, FormFields) { ExamplesService.queryTemplate, null, injectMyInfo, - { redirectOnError: true, errorTargetState: 'templateForm' }, + { + redirectOnError: true, + errorTargetState: 'templateForm', + targetFormId: formId, + }, formId, ), previewForm: (formId) => @@ -173,7 +168,11 @@ function FormApi(FormErrorService, FormFields) { AdminViewFormService.previewForm, null, injectMyInfo, - { redirectOnError: true, errorTargetState: 'previewForm' }, + { + redirectOnError: true, + errorTargetState: 'previewForm', + targetFormId: formId, + }, formId, ), useTemplate: (formId, overrideParams) => @@ -181,7 +180,11 @@ function FormApi(FormErrorService, FormFields) { ExamplesService.useTemplate, null, null, - { redirectOnError: true, errorTargetState: 'useTemplate' }, + { + redirectOnError: true, + errorTargetState: 'useTemplate', + targetFormId: formId, + }, formId, overrideParams, ),