Skip to content

Commit 3f8a32c

Browse files
committed
fix(nbp): Stop QR code countdown when leaving the page
Part of XI-6523
1 parent 32ed9b8 commit 3f8a32c

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed
Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,51 @@
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();
1612
}
13+
} catch (error) {
14+
console.error(error);
1715
}
18-
setTimeout(checkStatus, 1000);
16+
timeoutID = setTimeout(checkStatus, 1000);
1917
};
2018

2119
const countdownValidity = () => {
2220
if (templateValidity > 0) {
2321
templateValidity -= 1;
2422
}
25-
if (templateValidity === 0) {
23+
if (templateValidity === 0 && window.location.pathname === Routes.nbp_wallet_connect_users_path()) {
2624
window.location.reload();
2725
}
2826
};
2927

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

35-
document.querySelector('.regenerate-qr-code-button').addEventListener('click', () => {
43+
document.querySelector('[data-behavior=reload-on-click]').addEventListener('click', () => {
3644
window.location.reload();
3745
});
3846

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

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},
11+
alt: t('.qr_code_alt_text'),
12+
class: 'img-responsive'
13+
.buttons.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)