Skip to content

Commit

Permalink
fix : lp-318 - persists language across pages
Browse files Browse the repository at this point in the history
  • Loading branch information
lduranteau committed Dec 19, 2024
1 parent 2cac58c commit 26810d3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
32 changes: 30 additions & 2 deletions src/presentation/controller/AbstractController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ abstract class AbstractController {
protected getRouting(
routing: PagesRouting,
pageType: PageType,
currentUrl: string,
transactionId = "",
submissionId = ""
) {
Expand All @@ -21,6 +22,8 @@ abstract class AbstractController {
submissionId
);

pageRouting = this.addLangToUrls(currentUrl, pageRouting);

return pageRouting;
}

Expand All @@ -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];
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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;
6 changes: 5 additions & 1 deletion src/presentation/controller/global/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/presentation/controller/registration/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class RegistrationController extends AbstractController {
const pageRouting = super.getRouting(
registrationsRouting,
pageType,
request.url,
transactionId,
submissionId
);
Expand Down Expand Up @@ -72,7 +73,8 @@ class RegistrationController extends AbstractController {

const registrationRouting = super.getRouting(
registrationsRouting,
pageType
pageType,
request.url
);

if (result.errors?.length) {
Expand Down Expand Up @@ -109,6 +111,7 @@ class RegistrationController extends AbstractController {
const registrationRouting = super.getRouting(
registrationsRouting,
type,
request.url,
request.params[TRANSACTION_ID],
request.params[SUBMISSION_ID]
);
Expand Down Expand Up @@ -146,6 +149,7 @@ class RegistrationController extends AbstractController {
const registrationRouting = super.getRouting(
registrationsRouting,
pageType,
request.url,
transactionId,
submissionId
);
Expand Down
9 changes: 0 additions & 9 deletions src/views/includes/back-link.njk
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,3 @@
},
href: props.previousUrl
}) }}

<script>
const backLink = document.querySelector('.govuk-back-link');
backLink.addEventListener('click', function(event) {
event.preventDefault();
window.history.back();
});
</script>

0 comments on commit 26810d3

Please sign in to comment.