Skip to content

Commit bb8844f

Browse files
committed
fix(nbp): stop QR code countdown when leaving the page
Part of XI-6523
1 parent 68fa603 commit bb8844f

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
lines changed
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,52 @@
11
let templateValidity = 0;
2-
let finalizing = false;
2+
let intervalID;
3+
let timeoutID;
34

45
const checkStatus = async () => {
5-
if (!finalizing) {
6-
try {
7-
const response = await fetch('/nbp_wallet/relationship_status');
8-
const json = await response.json();
9-
10-
if (json.status === 'ready' && !finalizing) {
11-
finalizing = true;
12-
window.location.pathname = '/nbp_wallet/finalize';
13-
}
14-
} catch (error) {
15-
console.error(error);
6+
try {
7+
const response = await fetch(Routes.nbp_wallet_relationship_status_users_path());
8+
const json = await response.json();
9+
10+
if (json.status === 'ready' && window.location.pathname === Routes.nbp_wallet_connect_users_path()) {
11+
window.location.pathname = Routes.nbp_wallet_finalize_users_path();
12+
return;
1613
}
14+
} catch (error) {
15+
console.error(error);
1716
}
18-
setTimeout(checkStatus, 1000);
17+
timeoutID = setTimeout(checkStatus, 1000);
1918
};
2019

2120
const countdownValidity = () => {
2221
if (templateValidity > 0) {
2322
templateValidity -= 1;
2423
}
25-
if (templateValidity === 0) {
24+
if (templateValidity === 0 && window.location.pathname === Routes.nbp_wallet_connect_users_path()) {
2625
window.location.reload();
2726
}
2827
};
2928

29+
window.addEventListener("turbolinks:before-render", () => {
30+
clearInterval(intervalID);
31+
clearTimeout(timeoutID);
32+
});
33+
34+
window.addEventListener("beforeunload", () => {
35+
clearInterval(intervalID);
36+
clearTimeout(timeoutID);
37+
});
38+
3039
$(document).on('turbolinks:load', function () {
31-
if (window.location.pathname !== '/nbp_wallet/connect') {
40+
if (window.location.pathname !== Routes.nbp_wallet_connect_users_path()) {
3241
return;
3342
}
3443

35-
document.querySelector('.regenerate-qr-code-button').addEventListener('click', () => {
44+
document.querySelector('[data-behavior=reload-on-click]').addEventListener('click', () => {
3645
window.location.reload();
3746
});
3847

3948
// Subtract 5 seconds to make sure the displayed code is always valid (accounting for loading times)
4049
templateValidity = document.querySelector('[data-id="nbp_wallet_qr_code"]').dataset.remainingValidity - 5;
4150
checkStatus();
42-
setInterval(countdownValidity, 1000);
51+
intervalID = setInterval(countdownValidity, 1000);
4352
});

app/assets/stylesheets/users.css.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
display: inline;
1111
}
1212

13-
img.nbp_wallet_qr_code {
13+
img.pixelated {
1414
image-rendering: pixelated;
1515
width: 100%;
1616
}

app/controllers/users/nbp_wallet_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def connect
1111
redirect_to nbp_wallet_finalize_users_path and return
1212
end
1313

14-
@template = Enmeshed::RelationshipTemplate.create!(nbp_uid: @provider_uid)
14+
@relationship_template = Enmeshed::RelationshipTemplate.create!(nbp_uid: @provider_uid)
1515
rescue Enmeshed::ConnectorError, Faraday::Error => e
1616
Sentry.capture_exception(e)
1717
Rails.logger.debug { e }

app/views/users/nbp_wallet/connect.html.slim

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55

66
.row
77
.col-8.col-md-4.col-lg-3
8-
= link_to @template.url
9-
= image_tag @template.qr_code_path, data: {'template-validity': @template.remaining_validity&.seconds}, alt: t('.qr_code_alt_text'), class: 'img-fluid nbp_wallet_qr_code'
10-
= button_tag class: 'btn btn-primary regenerate-qr-code-button w-100 mt-3' do
8+
= link_to @relationship_template.url
9+
= image_tag @relationship_template.qr_code_path,
10+
data: {id: 'nbp_wallet_qr_code', 'remaining-validity': @relationship_template.remaining_validity.seconds},
11+
alt: t('.qr_code_alt_text'),
12+
class: 'img-fluid pixelated'
13+
.btn.btn-primary.w-100.mt-3 data-behavior='reload-on-click'
1114
= t('.regenerate_code')
1215
= link_to destroy_user_session_path, method: :delete, class: 'btn btn-outline-danger w-100 mt-3'
1316
= t('.cancel_registration')
1417
.col-8.col-md-6.mt-3.ms-md-3.mt-md-0
1518
p.fs-6
16-
= t('.info_html', alternative_link: @template.url, app_store_link: @template.app_store_link, play_store_link: @template.play_store_link)
19+
= t('.info_html',
20+
alternative_link: @relationship_template.url,
21+
app_store_link: @relationship_template.app_store_link,
22+
play_store_link: @relationship_template.play_store_link)
1723

1824
hr.mt-5
1925
h5

0 commit comments

Comments
 (0)