Skip to main content

max / makenotwork

1.4 KB · 33 lines History Blame Raw
1 (function() {
2 var cfg = document.getElementById('tab-project-code-cfg');
3 var projectSlug = cfg.dataset.projectSlug;
4 document.querySelectorAll('.collab-add-btn').forEach(function(btn) {
5 btn.addEventListener('click', function() {
6 var repoId = btn.dataset.repoId;
7 var input = document.querySelector('.collab-username-input[data-repo-id="' + repoId + '"]');
8 var username = input.value.trim();
9 if (!username) return;
10 btn.disabled = true;
11 var body = new URLSearchParams();
12 body.append('username', username);
13 fetch('/api/repos/' + repoId + '/collaborators', {
14 method: 'POST',
15 headers: {...csrfHeaders(), 'Content-Type': 'application/x-www-form-urlencoded'},
16 body: body.toString()
17 }).then(function(r) {
18 if (r.ok) {
19 htmx.ajax('GET', '/dashboard/project/' + projectSlug + '/tabs/code', {target: '#tab-content', swap: 'innerHTML'});
20 } else {
21 return r.json().then(function(data) {
22 alert(data.error || 'Failed to add collaborator');
23 });
24 }
25 }).catch(function() {
26 alert('Network error');
27 }).finally(function() {
28 btn.disabled = false;
29 });
30 });
31 });
32 })();
33