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