diff --git a/src/presentation/controller/AbstractController.ts b/src/presentation/controller/AbstractController.ts index 88b1fbd..5dd0dbb 100644 --- a/src/presentation/controller/AbstractController.ts +++ b/src/presentation/controller/AbstractController.ts @@ -6,6 +6,7 @@ abstract class AbstractController { protected getRouting( routing: PagesRouting, pageType: PageType, + currentUrl: string, transactionId = "", submissionId = "" ) { @@ -21,6 +22,8 @@ abstract class AbstractController { submissionId ); + pageRouting = this.addLangToUrls(currentUrl, pageRouting); + return pageRouting; } @@ -31,7 +34,8 @@ abstract class AbstractController { } protected templateName(url: string): string { - const splitted = url.split("/"); + const urlWithoutQueryParams = url.split("?")[0]; + const splitted = urlWithoutQueryParams.split("/"); return splitted[splitted.length - 1]; } @@ -42,7 +46,9 @@ abstract class AbstractController { } protected insertTransactionId(url: string, transactionId: string): string { - return transactionId ? url.replace(`:${TRANSACTION_ID}`, transactionId) : url; + return transactionId + ? url.replace(`:${TRANSACTION_ID}`, transactionId) + : url; } protected insertSubmissionId(url: string, submissionId: string): string { @@ -73,6 +79,28 @@ abstract class AbstractController { ), }; } + + private addLangToUrls( + currentUrl: string, + pageRouting: PageRouting + ): PageRouting { + const currentUrlParams = new URLSearchParams( + new URL(`http://${currentUrl}`)?.search + ); + + if (currentUrlParams.has("lang")) { + const langQuery = `?lang=${currentUrlParams.get("lang")}`; + + return { + ...pageRouting, + previousUrl: `${pageRouting.previousUrl}${langQuery}`, + currentUrl: `${pageRouting.currentUrl}${langQuery}`, + nextUrl: `${pageRouting.nextUrl}${langQuery}`, + }; + } + + return pageRouting; + } } export default AbstractController; diff --git a/src/presentation/controller/global/Controller.ts b/src/presentation/controller/global/Controller.ts index 9752ea7..7fa1d26 100644 --- a/src/presentation/controller/global/Controller.ts +++ b/src/presentation/controller/global/Controller.ts @@ -16,7 +16,11 @@ class GlobalController extends AbstractController { const pageType = super.pageType(request.path); - const pageRouting = super.getRouting(registrationsRouting, pageType); + const pageRouting = super.getRouting( + registrationsRouting, + pageType, + request.url + ); response.render(super.templateName(pageRouting.currentUrl), { props: pageRouting, diff --git a/src/presentation/controller/registration/Controller.ts b/src/presentation/controller/registration/Controller.ts index d41e382..97606c9 100644 --- a/src/presentation/controller/registration/Controller.ts +++ b/src/presentation/controller/registration/Controller.ts @@ -36,6 +36,7 @@ class RegistrationController extends AbstractController { const pageRouting = super.getRouting( registrationsRouting, pageType, + request.url, transactionId, submissionId ); @@ -72,7 +73,8 @@ class RegistrationController extends AbstractController { const registrationRouting = super.getRouting( registrationsRouting, - pageType + pageType, + request.url ); if (result.errors?.length) { @@ -109,6 +111,7 @@ class RegistrationController extends AbstractController { const registrationRouting = super.getRouting( registrationsRouting, type, + request.url, request.params[TRANSACTION_ID], request.params[SUBMISSION_ID] ); @@ -146,6 +149,7 @@ class RegistrationController extends AbstractController { const registrationRouting = super.getRouting( registrationsRouting, pageType, + request.url, transactionId, submissionId ); diff --git a/src/views/includes/back-link.njk b/src/views/includes/back-link.njk index ca67b28..d51c4d9 100644 --- a/src/views/includes/back-link.njk +++ b/src/views/includes/back-link.njk @@ -5,12 +5,3 @@ }, href: props.previousUrl }) }} - - \ No newline at end of file