Skip to content

Commit

Permalink
fix: correctly retrieve targetFormId for redirect state (#2261)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
karrui authored Jun 29, 2021
1 parent 4cd0bac commit 9f82f02
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,4 @@
"webpack-merge": "^4.1.3",
"worker-loader": "^2.0.0"
}
}
}
6 changes: 3 additions & 3 deletions src/public/modules/forms/config/forms.client.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
],
},
Expand Down
43 changes: 23 additions & 20 deletions src/public/modules/forms/services/form-api.client.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -115,15 +102,19 @@ function FormApi(FormErrorService, FormFields) {
AdminViewFormService.getAdminFormView,
null,
injectMyInfo,
{ redirectOnError: true, errorTargetState: 'viewForm' },
{
redirectOnError: true,
errorTargetState: 'viewForm',
targetFormId: formId,
},
formId,
),
getPublicForm: (formId) =>
generateService(
PublicFormService.getPublicFormView,
null,
injectMyInfo,
{ redirectOnError: true },
{ redirectOnError: true, targetFormId: formId },
formId,
),
updateForm: (formId, update) =>
Expand Down Expand Up @@ -165,23 +156,35 @@ function FormApi(FormErrorService, FormFields) {
ExamplesService.queryTemplate,
null,
injectMyInfo,
{ redirectOnError: true, errorTargetState: 'templateForm' },
{
redirectOnError: true,
errorTargetState: 'templateForm',
targetFormId: formId,
},
formId,
),
previewForm: (formId) =>
generateService(
AdminViewFormService.previewForm,
null,
injectMyInfo,
{ redirectOnError: true, errorTargetState: 'previewForm' },
{
redirectOnError: true,
errorTargetState: 'previewForm',
targetFormId: formId,
},
formId,
),
useTemplate: (formId, overrideParams) =>
generateService(
ExamplesService.useTemplate,
null,
null,
{ redirectOnError: true, errorTargetState: 'useTemplate' },
{
redirectOnError: true,
errorTargetState: 'useTemplate',
targetFormId: formId,
},
formId,
overrideParams,
),
Expand Down

0 comments on commit 9f82f02

Please sign in to comment.