Skip to content

Commit

Permalink
MLPAB-1147: Fix destructive changes dialog flakiness (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
acsauk authored Oct 3, 2023
1 parent cb85f83 commit 755481e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 47 deletions.
11 changes: 5 additions & 6 deletions web/assets/data-loss-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export class DataLossWarning {
if (this.dialog && this.dialogOverlay && this.returnToTaskListButton) {
this.formValuesOnPageLoad = this.getEncodedStringifiedFormValues()
this.formValuesPriorToPageLoad = this.getFormValuesFromCookie()

this.registerListeners()
}
}
Expand Down Expand Up @@ -60,10 +59,10 @@ export class DataLossWarning {
e.preventDefault()
}
})
document.getElementById('save-and-continue-btn').addEventListener('click', this.addFormValuesToCookie.bind(this))
document.getElementById('return-to-tasklist-btn').addEventListener('click', this.toggleDialogVisibility.bind(this))
document.getElementById('back-to-page-dialog-btn').addEventListener('click', this.toggleDialogVisibility.bind(this))
document.getElementById('return-to-task-list-dialog-btn').addEventListener('click', this.toggleDialogVisibility.bind(this))
document.getElementById('save-and-continue-btn').addEventListener('click', () => { this.addFormValuesToCookie() })
}

handleTrapFocus(e) {
Expand Down Expand Up @@ -96,13 +95,13 @@ export class DataLossWarning {

addFormValuesToCookie() {
// so the cookie isn't available for longer than required
const tenSecondsFutureDate = new Date();
tenSecondsFutureDate.setTime(tenSecondsFutureDate.getTime() + 10)
const tenSecondsFutureDate = new Date()
tenSecondsFutureDate.setSeconds(tenSecondsFutureDate.getSeconds() + 10)

document.cookie = `formValues=${this.getEncodedStringifiedFormValues()}; expires=${tenSecondsFutureDate.toUTCString()}; SameSite=Lax; Secure`;
document.cookie = `formValues=${this.getEncodedStringifiedFormValues()}; expires=${tenSecondsFutureDate.toUTCString()}; SameSite=Lax; Secure`
}

getFormValuesFromCookie() {
return document.cookie.split("; ").find((row) => row.startsWith("formValues="))?.split("=")[1];
return document.cookie.split("; ").find((row) => row.startsWith("formValues="))?.split("=")[1]
}
}
91 changes: 50 additions & 41 deletions web/assets/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,60 @@ import $ from 'jquery';
import { CrossServiceHeader } from './service-header';
import { DataLossWarning } from './data-loss-warning';

window.$ = $
// Account for DOMContentLoaded firing before JS runs
if (document.readyState !== "loading") {
init()
} else {
document.addEventListener('DOMContentLoaded', init)
}

GOVUKFrontend.initAll();
MOJFrontend.initAll();
function init() {
window.$ = $

const header = document.querySelector("[data-module='one-login-header']");
if (header) {
new CrossServiceHeader(header).init();
}
GOVUKFrontend.initAll();
MOJFrontend.initAll();

new DataLossWarning().init()
const header = document.querySelector("[data-module='one-login-header']");
if (header) {
new CrossServiceHeader(header).init();
}

const backLink = document.querySelector('.govuk-back-link');
if (backLink) {
backLink.addEventListener('click', function (e) {
window.history.back();
e.preventDefault();
}, false);
}
new DataLossWarning().init()

function metaContent(name) {
return document.querySelector(`meta[name=${name}]`).content;
}
const backLink = document.querySelector('.govuk-back-link');
if (backLink) {
backLink.addEventListener('click', function (e) {
window.history.back();
e.preventDefault();
}, false);
}

function metaContent(name) {
return document.querySelector(`meta[name=${name}]`).content;
}

try {
const config = {
sessionSampleRate: 1,
guestRoleArn: metaContent('rum-guest-role-arn'),
identityPoolId: metaContent('rum-identity-pool-id'),
endpoint: metaContent('rum-endpoint'),
telemetries: ["http", "errors", "performance"],
allowCookies: true,
enableXRay: true
};

const APPLICATION_ID = metaContent('rum-application-id');
const APPLICATION_VERSION = '1.0.0';
const APPLICATION_REGION = metaContent('rum-application-region');

try {
const config = {
sessionSampleRate: 1,
guestRoleArn: metaContent('rum-guest-role-arn'),
identityPoolId: metaContent('rum-identity-pool-id'),
endpoint: metaContent('rum-endpoint'),
telemetries: ["http", "errors", "performance"],
allowCookies: true,
enableXRay: true
};

const APPLICATION_ID = metaContent('rum-application-id');
const APPLICATION_VERSION = '1.0.0';
const APPLICATION_REGION = metaContent('rum-application-region');

const awsRum = new AwsRum(
APPLICATION_ID,
APPLICATION_VERSION,
APPLICATION_REGION,
config
);
} catch (error) {
// Ignore errors thrown during CloudWatch RUM web client initialization
const awsRum = new AwsRum(
APPLICATION_ID,
APPLICATION_VERSION,
APPLICATION_REGION,
config
);
} catch (error) {
// Ignore errors thrown during CloudWatch RUM web client initialization
}
}

0 comments on commit 755481e

Please sign in to comment.