Skip to content

Commit 120efb9

Browse files
author
John Pinto
committed
Update to Create Account Form.
Moved javascript from app/views/shared/_create_account_form.html.erb to app/views/shared/_create_account_form.html.erb.
1 parent c9f9769 commit 120efb9

2 files changed

Lines changed: 79 additions & 88 deletions

File tree

app/javascript/src/shared/createAccountForm.js

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,83 @@ import { initAutocomplete, scrubOrgSelectionParamsOnSubmit } from '../utils/auto
22
import { togglisePasswords } from '../utils/passwordHelper';
33

44
$(() => {
5-
initAutocomplete('#create-account-org-controls .autocomplete');
6-
// Scrub out the large arrays of data used for the Org Selector JS so that they
7-
// are not a part of the form submissiomn
8-
scrubOrgSelectionParamsOnSubmit('#create_account_form');
9-
togglisePasswords({ selector: '#create_account_form' });
5+
const createAccountForm = document.getElementById('create_account_form');
6+
7+
if (!createAccountForm) {
8+
return;
9+
}
10+
11+
const emailField = createAccountForm.querySelector('input[type="email"]');
12+
const orgSelect = createAccountForm.querySelector('select[name*="org"]');
13+
14+
if (!emailField || !orgSelect) {
15+
return;
16+
}
17+
18+
let currentDomain = '';
19+
let debounceTimer = null;
20+
21+
// Handle email input changes
22+
emailField.addEventListener('input', function() {
23+
const email = this.value;
24+
25+
if (email && email.includes('@')) {
26+
const domain = email.split('@')[1];
27+
28+
// Only fetch if domain has changed
29+
if (domain && domain !== currentDomain) {
30+
currentDomain = domain;
31+
32+
// Clear previous timer
33+
if (debounceTimer) {
34+
clearTimeout(debounceTimer);
35+
}
36+
37+
// Debounce API calls to avoid too many requests
38+
debounceTimer = setTimeout(() => {
39+
fetchOrganizations(domain);
40+
}, 500);
41+
}
42+
} else {
43+
// Clear organizations if email is invalid
44+
currentDomain = '';
45+
resetOrgSelect();
46+
}
47+
});
48+
49+
function fetchOrganizations(domain) {
50+
fetch(`/api/get-orgs-by-domain?domain=${encodeURIComponent(domain)}`)
51+
.then(response => response.json())
52+
.then(data => {
53+
populateOrgSelect(data);
54+
})
55+
.catch(error => {
56+
console.error('Error fetching organizations:', error);
57+
resetOrgSelect();
58+
});
59+
}
60+
61+
function populateOrgSelect(orgs) {
62+
// Clear existing options
63+
orgSelect.innerHTML = '';
64+
65+
// Add prompt option
66+
const promptOption = document.createElement('option');
67+
promptOption.value = '';
68+
promptOption.textContent = 'Select an organisation';
69+
orgSelect.appendChild(promptOption);
70+
71+
// Add organization options
72+
orgs.forEach(function(org) {
73+
const option = document.createElement('option');
74+
option.value = org.id || org.ror_id;
75+
option.textContent = org.org_name;
76+
orgSelect.appendChild(option);
77+
});
78+
}
79+
80+
function resetOrgSelect() {
81+
orgSelect.innerHTML = '<option value="">Select an organisation</option>';
82+
}
1083
});
84+

app/views/shared/_create_account_form.html.erb

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -61,86 +61,3 @@
6161
<% end %>
6262
<%= f.button(_('Create account'), class: "btn btn-secondary", type: "submit") %>
6363
<% end %>
64-
65-
<script>
66-
document.addEventListener('DOMContentLoaded', function() {
67-
const createAccountForm = document.getElementById('create_account_form');
68-
69-
if (!createAccountForm) {
70-
return;
71-
}
72-
73-
const emailField = createAccountForm.querySelector('input[type="email"]');
74-
const orgSelect = createAccountForm.querySelector('select[name*="org"]');
75-
76-
if (!emailField || !orgSelect) {
77-
return;
78-
}
79-
80-
let currentDomain = '';
81-
let debounceTimer = null;
82-
83-
// Handle email input changes
84-
emailField.addEventListener('input', function() {
85-
const email = this.value;
86-
87-
if (email && email.includes('@')) {
88-
const domain = email.split('@')[1];
89-
90-
// Only fetch if domain has changed
91-
if (domain && domain !== currentDomain) {
92-
currentDomain = domain;
93-
94-
// Clear previous timer
95-
if (debounceTimer) {
96-
clearTimeout(debounceTimer);
97-
}
98-
99-
// Debounce API calls to avoid too many requests
100-
debounceTimer = setTimeout(() => {
101-
fetchOrganizations(domain);
102-
}, 500);
103-
}
104-
} else {
105-
// Clear organizations if email is invalid
106-
currentDomain = '';
107-
resetOrgSelect();
108-
}
109-
});
110-
111-
function fetchOrganizations(domain) {
112-
fetch(`/api/get-orgs-by-domain?domain=${encodeURIComponent(domain)}`)
113-
.then(response => response.json())
114-
.then(data => {
115-
populateOrgSelect(data);
116-
})
117-
.catch(error => {
118-
console.error('Error fetching organizations:', error);
119-
resetOrgSelect();
120-
});
121-
}
122-
123-
function populateOrgSelect(orgs) {
124-
// Clear existing options
125-
orgSelect.innerHTML = '';
126-
127-
// Add prompt option
128-
const promptOption = document.createElement('option');
129-
promptOption.value = '';
130-
promptOption.textContent = 'Select an organisation';
131-
orgSelect.appendChild(promptOption);
132-
133-
// Add organization options
134-
orgs.forEach(function(org) {
135-
const option = document.createElement('option');
136-
option.value = org.id || org.ror_id;
137-
option.textContent = org.org_name;
138-
orgSelect.appendChild(option);
139-
});
140-
}
141-
142-
function resetOrgSelect() {
143-
orgSelect.innerHTML = '<option value="">Select an organisation</option>';
144-
}
145-
});
146-
</script>

0 commit comments

Comments
 (0)