Skip to content

Commit

Permalink
Multiple editors - handle editing recently deleted section (#421)
Browse files Browse the repository at this point in the history
* handle section deleted by editor when fetching page data
  • Loading branch information
john-tco authored Mar 27, 2024
1 parent 08917d2 commit 1aa4bba
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
23 changes: 23 additions & 0 deletions packages/admin/src/utils/QuestionPageGetServerSideProps.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ describe('QuestionPageGetServerSideProps', () => {
});
});

it('should redirect to the multiple editors error page', async () => {
mockGetDataFunction.mockRejectedValueOnce({
config: { url: '/application-forms/application-id' },
response: {
data: {
error: { message: 'MULTIPLE_EDITORS_SECTION_DELETED' },
},
},
});

const response = await QuestionPageGetServerSideProps(
getProps(getDefaultProps)
);

expectObjectEquals(response, {
redirect: {
destination:
'/build-application/application-id/error-multiple-editors?error=The section or question you were editing has been deleted and your changes could not be saved.',
statusCode: 302,
},
});
});

it('Should redirect to the error service page if there is an error when getting data', async () => {
mockGetDataFunction.mockRejectedValueOnce('Error');

Expand Down
4 changes: 4 additions & 0 deletions packages/admin/src/utils/QuestionPageGetServerSideProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
generateValidationPropsType,
NextRedirect,
} from './QuestionPageGetServerSidePropsTypes';
import { handleMultipleEditorsError } from './serviceErrorHelpers';

/**
* Abstracts away how we handle redirects, fetch & update data.
Expand Down Expand Up @@ -71,6 +72,9 @@ async function fetchAndHandlePageData<K extends FetchPageData>(
try {
return await fetchPageData(jwt);
} catch (err: any) {
const multipleEditorsRedirect = handleMultipleEditorsError(err);
if (multipleEditorsRedirect) return multipleEditorsRedirect;

if (err?.response?.data?.code) {
return generateRedirect(
`/error-page/code/${err.response.data.code}?href=${
Expand Down
18 changes: 3 additions & 15 deletions packages/admin/src/utils/callServiceMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
generateErrorPageAdvertAlreadyPublished,
generateErrorPageMultipleEditors,
generateErrorPageRedirectV2,
handleMultipleEditorsError,
} from './serviceErrorHelpers';
import { GetServerSidePropsContext, Redirect } from 'next';
import { advertIsPublishedOrScheduled } from '../pages/scheme/[schemeId]/advert/[advertId]/summary/components/util';
Expand Down Expand Up @@ -84,21 +85,8 @@ export default async function callServiceMethod<
}

// If we want to display the Multiple Editors error page
if (err?.response?.data?.error?.message.includes('MULTIPLE_EDITORS')) {
const isSectionDeletedError =
err?.response?.data?.error?.message ===
'MULTIPLE_EDITORS_SECTION_DELETED';

const applicationId = err.config.url
.split('/application-forms/')
.pop()
.split('/')[0];

return generateErrorPageMultipleEditors(
applicationId,
isSectionDeletedError
);
}
const multipleEditorsErrorRedirect = handleMultipleEditorsError(err);
if (multipleEditorsErrorRedirect) return multipleEditorsErrorRedirect;

// If we encounter an error that conforms to the new way of handling form validation errors
if (err.code) {
Expand Down
19 changes: 19 additions & 0 deletions packages/admin/src/utils/serviceErrorHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,26 @@ const generateErrorMessageFromStatusCode = (errorCode: string): string => {
}
};

const handleMultipleEditorsError = (err: any) => {
if (err?.response?.data?.error?.message.includes('MULTIPLE_EDITORS')) {
const isSectionDeletedError =
err?.response?.data?.error?.message ===
'MULTIPLE_EDITORS_SECTION_DELETED';

const applicationId = err.config.url
.split('/application-forms/')
.pop()
.split('/')[0];

return generateErrorPageMultipleEditors(
applicationId,
isSectionDeletedError
);
}
};

export {
handleMultipleEditorsError,
generateErrorPageParams,
generateErrorPageRedirect,
generateErrorPageRedirectV2,
Expand Down

0 comments on commit 1aa4bba

Please sign in to comment.