Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
49 lines (43 loc) · 1.41 KB
/
Copy pathmain.js
File metadata and controls
49 lines (43 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(() => {
const byId = (id) => document.getElementById(id);
async function check() {
const compNumEl = byId('compNum');
const countryEl = byId('country');
const resultEl = byId('result');
const btn = byId('checkBtn');
const compNum = (compNumEl?.value || '').trim();
const country = (countryEl?.value || '').trim();
if (!compNum || !country) {
resultEl.value = 'Please enter both Comp Number and Country.';
return;
}
if (compNum.length > 3) return alert( 'Comp Number cannot be more than 3 characters.')
btn.disabled = true;
resultEl.value = 'Checking...';
try {
const res = await fetch('/check', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ compNum, country })
});
const data = await res.json();
if (!res.ok) {
throw new Error(data && data.error ? data.error : 'Request failed');
}
if (data.taken === false) {
resultEl.value = ('Comp number is available.')
}
else {
resultEl.value = `Comp number is taken by: ${data.model} - ${data.registration}`;
}
} catch (e) {
resultEl.value = `Error: ${e.message || e}`;
} finally {
btn.disabled = false;
}
}
window.addEventListener('DOMContentLoaded', () => {
const btn = byId('checkBtn');
if (btn) btn.addEventListener('click', check);
});
})();