Skip to content

Commit

Permalink
fix(EMS-1345): Disable submit buttons when a form is submitting (#557)
Browse files Browse the repository at this point in the history
* fix(EMS-1345): UI application creation requests - revert previous changes and immediately wipe eligibility answers from the session

* fix(EMS-1345): disable submit buttons on form submission

* fix(EMS-1345): submit button nunjucks component

* fix(EMS-1345): remove unused function

* fix(EMS-1345): align create application instances in UI controllers

* fix(EMS-1345) use const instead of var in form-submission script

* chore(tech): fix/update E2E test

* fix(EMS-1345): fix typos

* fix(EMS-1345): fix/update failing E2E test

* chore(tech): temporarily disable failing E2E test due to newly found bug

* fix(EMS-1345): revert previous commit disabling test, fix company details submit button issue

* fix(EMS-1345): revert E2E test changes
  • Loading branch information
ttbarnes authored Jun 23, 2023
1 parent 12e263d commit 54f38a3
Show file tree
Hide file tree
Showing 63 changed files with 273 additions and 353 deletions.
2 changes: 2 additions & 0 deletions src/ui/public/js/formSubmission.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/ui/public/js/formSubmission.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/ui/public/js/submitButton.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/ui/public/js/submitButton.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions src/ui/scripts/form-submission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const form = document.getElementById('form');
const submitButton = document.getElementById('submit-button');

/**
* Disable the submit button.
* This prevents a user from spamming the button.
*/
const disableButton = () => {
submitButton.setAttribute('disabled', 'true');
submitButton.setAttribute('aria-disabled', 'true');
submitButton.classList.add('govuk-button--disabled');
};

/**
* If a form exists,
* Add a submit event listener.
* When submitted, disable the submit button
*/
if (form) {
form.addEventListener('submit', () => {
disableButton();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,10 @@ export const post = async (req: Request, res: Response) => {

/**
* If there are eligibility answers in the session:
* 1) Add requestedApplicationCreation to the session
* 2) Create an application
* 3) Remove requestedApplicationCreation from the session
* 4) Remove eligibility answers in the session.
* 5) Redirect to the next part of the flow - "confirm email"
* 1) Sanitise and store eligibility answers.
* 2) Remove eligibility answers from the session.
* 3) Create an application
* 4) Redirect to the next part of the flow - "confirm email"
*/
if (canCreateAnApplication(req.session)) {
const eligibilityAnswers = sanitiseData(req.session.submittedData.insuranceEligibility);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ export const post = async (req: Request, res: Response) => {

/**
* If there are eligibility answers in the session:
* 1) Add requestedApplicationCreation to the session
* 2) Create an application
* 3) Remove requestedApplicationCreation from the session
* 4) Remove eligibility answers in the session.
* 1) Sanitise and store eligibility answers.
* 2) Remove eligibility answers from the session.
* 3) Create an application
* 4) Redirect to the next part of the flow - "dashboard"
*/
if (canCreateAnApplication(req.session)) {
const eligibilityAnswers = sanitiseData(req.session.submittedData.insuranceEligibility);
Expand All @@ -143,8 +143,6 @@ export const post = async (req: Request, res: Response) => {
console.error('Error creating application');
return res.redirect(PROBLEM_WITH_SERVICE);
}

req.session.submittedData.insuranceEligibility = {};
}

// otherwise, redirect to the next part of the flow - dashboard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ export const get = (req: Request, res: Response) =>
export const post = async (req: Request, res: Response) => {
try {
/**
* If the user is signed in and there are eligibility answers in the session:
* 1) Add requestedApplicationCreation to the session
* 2) Create an application
* 3) Remove requestedApplicationCreation from the session
* 4) Wipe the eligibility answers in the session.
* 5) Redirect to the application
* If there are eligibility answers in the session:
* 1) Sanitise and store eligibility answers.
* 2) Remove eligibility answers from the session.
* 3) Create an application
* 4) Redirect to the application
*/
if (req.session.user && canCreateAnApplication(req.session)) {
const eligibilityAnswers = sanitiseData(req.session.submittedData.insuranceEligibility);
Expand Down
2 changes: 1 addition & 1 deletion src/ui/server/middleware/headers/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const security = (req: Request, res: Response, next: () => void) => {
'Content-Security-Policy',
"default-src 'none';connect-src https://region1.google-analytics.com 'self';base-uri 'self';block-all-mixed-content;font-src 'self' data:;form-action 'self';frame-ancestors 'self';img-src 'self';object-src 'none';script-src https://www.googletagmanager.com 'self' 'unsafe-inline';script-src-attr 'self' 'unsafe-inline';style-src 'self';upgrade-insecure-requests",
);
res.setHeader('Cache-Control', 'no-cache, must-revalidate, max-age=604800');
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate, max-age=604800');
res.setHeader('Referrer-Policy', 'same-origin');
res.setHeader('X-Download-Options', 'noopen');
res.setHeader('X-DNS-Prefetch-Control', 'on');
Expand Down
23 changes: 23 additions & 0 deletions src/ui/templates/components/submit-button.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% from 'govuk/components/button/macro.njk' import govukButton %}

{% macro render(params) %}

{% set text = params.text %}
{% set isStartButton = params.isStartButton %}
{% set classes = params.classes %}
{% set href = params.href %}
{% set formaction = params.formaction %}

{{ govukButton({
text: text,
isStartButton: isStartButton,
classes: classes,
href: href,
attributes: {
'data-cy': 'submit-button',
id: 'submit-button',
formaction: formaction
}
}) }}

{% endmacro %}
11 changes: 4 additions & 7 deletions src/ui/templates/cookies.njk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% from 'govuk/components/error-summary/macro.njk' import govukErrorSummary %}
{% from "govuk/components/table/macro.njk" import govukTable %}
{% from 'govuk/components/radios/macro.njk' import govukRadios %}
{% from "govuk/components/button/macro.njk" import govukButton %}
{% import './components/submit-button.njk' as submitButton %}
{% import './components/text-list.njk' as textList %}

{% block pageTitle %}
Expand Down Expand Up @@ -116,7 +116,7 @@
dataCy: 'analytics-info-list'
}) }}

<form method="POST" novalidate>
<form method="POST" id="form" novalidate>

<input type="hidden" name="_csrf" value="{{ csrfToken }}">

Expand Down Expand Up @@ -161,12 +161,9 @@
}
}) }}

{{ govukButton({
{{ submitButton.render({
text: CONTENT_STRINGS.BUTTONS.SAVE_CHANGES,
classes: "change-cookies-submit-button",
attributes: {
'data-cy': 'submit-button'
}
classes: "change-cookies-submit-button"
}) }}

</form>
Expand Down
1 change: 1 addition & 0 deletions src/ui/templates/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

<script src="/assets/js/govukFrontend.js" type="text/javascript"></script>
<script src="/assets/js/cookies.js" type="text/javascript"></script>
<script src="/assets/js/formSubmission.js" type="text/javascript"></script>

{% if cookieConsent %}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ googleAnalyticsId }}"></script>
Expand Down
11 changes: 4 additions & 7 deletions src/ui/templates/insurance/account/create/your-details.njk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% from "govuk/components/back-link/macro.njk" import govukBackLink %}
{% from 'govuk/components/error-summary/macro.njk' import govukErrorSummary %}
{% from "govuk/components/input/macro.njk" import govukInput %}
{% from 'govuk/components/button/macro.njk' import govukButton %}
{% import '../../../components/submit-button.njk' as submitButton %}

{% block pageTitle %}
{{ CONTENT_STRINGS.PAGE_TITLE }}
Expand All @@ -29,7 +29,7 @@

<p data-cy="intro">{{ CONTENT_STRINGS.INTRO }}</p>

<form method="POST" novalidate>
<form method="POST" id="form" novalidate>

<input type="hidden" name="_csrf" value="{{ csrfToken }}">

Expand Down Expand Up @@ -164,11 +164,8 @@
</div>

<div class="govuk-button-group govuk-!-margin-top-5">
{{ govukButton({
text: CONTENT_STRINGS.BUTTONS.CONTINUE,
attributes: {
'data-cy': 'submit-button'
}
{{ submitButton.render({
text: CONTENT_STRINGS.BUTTONS.CONTINUE
}) }}
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends 'index.njk' %}
{% from 'govuk/components/button/macro.njk' import govukButton %}
{% import '../../../components/submit-button.njk' as submitButton %}

{% block pageTitle %}
{{ CONTENT_STRINGS.PAGE_TITLE }}
Expand All @@ -16,16 +16,13 @@

<p class="govuk-body" data-cy="if-you-would-like">{{ CONTENT_STRINGS.IF_YOU_WOULD_LIKE }}</p>

<form method="POST" novalidate>
<form method="POST" id="form" novalidate>

<input type="hidden" name="_csrf" value="{{ csrfToken }}">

<div class="govuk-button-group">
{{ govukButton({
text: CONTENT_STRINGS.BUTTONS.SEND_NEW_LINK,
attributes: {
'data-cy': 'submit-button'
}
{{ submitButton.render({
text: CONTENT_STRINGS.BUTTONS.SEND_NEW_LINK
}) }}
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% from "govuk/components/back-link/macro.njk" import govukBackLink %}
{% from 'govuk/components/error-summary/macro.njk' import govukErrorSummary %}
{% from "govuk/components/input/macro.njk" import govukInput %}
{% from 'govuk/components/button/macro.njk' import govukButton %}
{% import '../../../components/submit-button.njk' as submitButton %}

{% block pageTitle %}
{{ CONTENT_STRINGS.PAGE_TITLE }}
Expand Down Expand Up @@ -31,7 +31,7 @@
</div>
</div>

<form method="POST" novalidate>
<form method="POST" id="form" novalidate>

<input type="hidden" name="_csrf" value="{{ csrfToken }}">

Expand Down Expand Up @@ -81,11 +81,8 @@
}) }}

<div class="govuk-button-group">
{{ govukButton({
text: CONTENT_STRINGS.BUTTONS.SUBMIT,
attributes: {
'data-cy': 'submit-button'
}
{{ submitButton.render({
text: CONTENT_STRINGS.BUTTONS.SUBMIT
}) }}
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% from "govuk/components/back-link/macro.njk" import govukBackLink %}
{% from 'govuk/components/error-summary/macro.njk' import govukErrorSummary %}
{% from "govuk/components/input/macro.njk" import govukInput %}
{% from 'govuk/components/button/macro.njk' import govukButton %}
{% import '../../../components/submit-button.njk' as submitButton %}

{% block pageTitle %}
{{ CONTENT_STRINGS.PAGE_TITLE }}
Expand Down Expand Up @@ -31,7 +31,7 @@
</div>
</div>

<form method="POST" novalidate>
<form method="POST" id="form" novalidate>

<input type="hidden" name="_csrf" value="{{ csrfToken }}">

Expand Down Expand Up @@ -65,11 +65,8 @@
}) }}

<div class="govuk-button-group">
{{ govukButton({
text: CONTENT_STRINGS.BUTTONS.SUBMIT,
attributes: {
'data-cy': 'submit-button'
}
{{ submitButton.render({
text: CONTENT_STRINGS.BUTTONS.SUBMIT
}) }}
</div>

Expand Down
11 changes: 4 additions & 7 deletions src/ui/templates/insurance/account/sign-in/enter-code.njk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% from 'govuk/components/error-summary/macro.njk' import govukErrorSummary %}
{% from "govuk/components/notification-banner/macro.njk" import govukNotificationBanner %}
{% from "govuk/components/input/macro.njk" import govukInput %}
{% from 'govuk/components/button/macro.njk' import govukButton %}
{% import '../../../components/submit-button.njk' as submitButton %}

{% block pageTitle %}
{{ CONTENT_STRINGS.PAGE_TITLE }}
Expand Down Expand Up @@ -49,7 +49,7 @@

<p data-cy="intro">{{ CONTENT_STRINGS.INTRO }}</p>

<form method="POST" novalidate>
<form method="POST" id="form" novalidate>

<input type="hidden" name="_csrf" value="{{ csrfToken }}">

Expand Down Expand Up @@ -82,11 +82,8 @@
}) }}

<div class="govuk-button-group">
{{ govukButton({
text: CONTENT_STRINGS.BUTTONS.CONTINUE,
attributes: {
'data-cy': 'submit-button'
}
{{ submitButton.render({
text: CONTENT_STRINGS.BUTTONS.CONTINUE
}) }}
</div>

Expand Down
11 changes: 4 additions & 7 deletions src/ui/templates/insurance/account/sign-in/request-new-code.njk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends 'index.njk' %}
{% from "govuk/components/back-link/macro.njk" import govukBackLink %}
{% from 'govuk/components/button/macro.njk' import govukButton %}
{% from "govuk/components/details/macro.njk" import govukDetails %}
{% import '../../../components/submit-button.njk' as submitButton %}

{% block pageTitle %}
{{ CONTENT_STRINGS.PAGE_TITLE }}
Expand All @@ -24,16 +24,13 @@

<p data-cy="intro">{{ CONTENT_STRINGS.INTRO }}</p>

<form method="POST" novalidate>
<form method="POST" id="form" novalidate>

<input type="hidden" name="_csrf" value="{{ csrfToken }}">

<div class="govuk-button-group">
{{ govukButton({
text: CONTENT_STRINGS.BUTTONS.SEND_NEW_SECURITY_CODE,
attributes: {
'data-cy': 'submit-button'
}
{{ submitButton.render({
text: CONTENT_STRINGS.BUTTONS.SEND_NEW_SECURITY_CODE
}) }}
</div>

Expand Down
11 changes: 4 additions & 7 deletions src/ui/templates/insurance/account/sign-in/sign-in.njk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% from 'govuk/components/error-summary/macro.njk' import govukErrorSummary %}
{% from "govuk/components/notification-banner/macro.njk" import govukNotificationBanner %}
{% from "govuk/components/input/macro.njk" import govukInput %}
{% from 'govuk/components/button/macro.njk' import govukButton %}
{% import '../../../components/submit-button.njk' as submitButton %}

{% block pageTitle %}
{{ CONTENT_STRINGS.PAGE_TITLE }}
Expand Down Expand Up @@ -73,7 +73,7 @@

<p data-cy="intro">{{ CONTENT_STRINGS.INTRO }}</p>

<form method="POST" novalidate>
<form method="POST" id="form" novalidate>

<input type="hidden" name="_csrf" value="{{ csrfToken }}">

Expand Down Expand Up @@ -132,11 +132,8 @@
}) }}

<div class="govuk-button-group">
{{ govukButton({
text: CONTENT_STRINGS.BUTTONS.CONTINUE,
attributes: {
'data-cy': 'submit-button'
}
{{ submitButton.render({
text: CONTENT_STRINGS.BUTTONS.CONTINUE
}) }}
</div>

Expand Down
Loading

0 comments on commit 54f38a3

Please sign in to comment.