Skip to content

Commit f98d79c

Browse files
momo3404aaronskiba
authored andcommitted
Add copyToken.js to allow for copying token
- The button initially displays 'Copy', then a check mark after it is clicked for two seconds
1 parent d06523e commit f98d79c

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

app/javascript/application.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import 'bootstrap-select';
2323
// Utilities
2424
import './src/utils/accordion';
2525
import './src/utils/autoComplete';
26+
import './src/utils/copyToken.js';
2627
import './src/utils/externalLink';
2728
import './src/utils/modalSearch';
2829
import './src/utils/outOfFocus';
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const initCopyToken = () => {
2+
document.addEventListener('click', function (e) {
3+
const button = e.target.closest('#copy-token-btn');
4+
if (!button) return;
5+
6+
e.preventDefault();
7+
8+
// Prevent spam clicking
9+
if (button.disabled) return;
10+
11+
const tokenInput = document.getElementById('api-token-val');
12+
if (!tokenInput) return;
13+
14+
const originalHTML = button.innerHTML;
15+
16+
// Disable immediately
17+
button.disabled = true;
18+
19+
navigator.clipboard.writeText(tokenInput.value).then(() => {
20+
// Replace button contents with check icon
21+
button.innerHTML = '<i class="fa fa-circle-check" aria-hidden="true"></i>';
22+
23+
// Restore after 2s
24+
setTimeout(() => {
25+
button.innerHTML = originalHTML;
26+
button.disabled = false;
27+
}, 2000);
28+
}).catch(() => {
29+
button.disabled = false;
30+
alert('Failed to copy token');
31+
});
32+
});
33+
};
34+
35+
initCopyToken();

0 commit comments

Comments
 (0)