|
1 | 1 | let templateValidity = 0; |
2 | | -let finalizing = false; |
| 2 | +let intervalID; |
| 3 | +let timeoutID; |
3 | 4 |
|
4 | 5 | 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(); |
16 | 12 | } |
| 13 | + } catch (error) { |
| 14 | + console.error(error); |
17 | 15 | } |
18 | | - setTimeout(checkStatus, 1000); |
| 16 | + timeoutID = setTimeout(checkStatus, 1000); |
19 | 17 | }; |
20 | 18 |
|
21 | 19 | const countdownValidity = () => { |
22 | 20 | if (templateValidity > 0) { |
23 | 21 | templateValidity -= 1; |
24 | 22 | } |
25 | | - if (templateValidity === 0) { |
| 23 | + if (templateValidity === 0 && window.location.pathname === Routes.nbp_wallet_connect_users_path()) { |
26 | 24 | window.location.reload(); |
27 | 25 | } |
28 | 26 | }; |
29 | 27 |
|
| 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 | + |
30 | 38 | $(document).on('turbolinks:load', function () { |
31 | | - if (window.location.pathname !== '/nbp_wallet/connect') { |
| 39 | + if (window.location.pathname !== Routes.nbp_wallet_connect_users_path()) { |
32 | 40 | return; |
33 | 41 | } |
34 | 42 |
|
35 | | - document.querySelector('.regenerate-qr-code-button').addEventListener('click', () => { |
| 43 | + document.querySelector('[data-behavior=reload-on-click]').addEventListener('click', () => { |
36 | 44 | window.location.reload(); |
37 | 45 | }); |
38 | 46 |
|
39 | 47 | // Subtract 5 seconds to make sure the displayed code is always valid (accounting for loading times) |
40 | 48 | templateValidity = document.querySelector('[data-id="nbp_wallet_qr_code"]').dataset.remainingValidity - 5; |
41 | 49 | checkStatus(); |
42 | | - setInterval(countdownValidity, 1000); |
| 50 | + intervalID = setInterval(countdownValidity, 1000); |
43 | 51 | }); |
0 commit comments