Skip to content

Commit 46d7f17

Browse files
committed
Fix radio and select fields
1 parent 89b4866 commit 46d7f17

5 files changed

Lines changed: 70 additions & 15 deletions

File tree

frontend/pages/create.ejs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,24 @@
1313
<div class="checkbox" change="cms">|||</div>
1414
<% } else if (field.type === 'radio') { %>
1515
<div class="radioOptions">
16+
<% if (!field.required) { %>
17+
<div class="radioOption">
18+
<input type="radio" id="none" name="none" value="">
19+
<label for="none">None</label>
20+
</div>
21+
<% } %>
1622
<% (field.options || []).forEach(option => { %>
1723
<div class="radioOption">
18-
<input type="radio" id="<%= option.value %>" name="<%= option.value %>" value="<%= option.label %>">
19-
<label for="<%= option.value %>"><%= option.label %></label>
24+
<input type="radio" id="<%= field.id %>-<%= option.value %>" name="<%= option.value %>" value="<%= option.value %>">
25+
<label for="<%= field.id %>-<%= option.value %>"><%= option.label %></label>
2026
</div>
2127
<% }) %>
2228
</div>
2329
<% } else if (field.type === 'select') { %>
24-
<select>
30+
<select id="<%= field.id %>Select">
31+
<% if (!field.required) { %>
32+
<option value="">None</option>
33+
<% } %>
2534
<% (field.options || []).forEach(option => { %>
2635
<option value="<%= option.value %>"><%= option.label %></option>
2736
<% }) %>

frontend/pages/user.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img src="<%= tenant.logo %>" alt="<%= tenant.name %>">
33
<h1>
44
<% if (tenant.auth && tenant.auth.enabled) { %>
5-
Hey, <%= session[vars.name] || session[vars.userId] %>!
5+
Welcome, <%= session[vars.name] || session[vars.userId] %>!
66
<% } else { %>
77
<%= tenant.name %> commissions
88
<% } %>

frontend/public/scripts.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function anim_out() {
1919
window.onload = function () {
2020
anim_in();
2121
document.querySelectorAll('.inputField').forEach(field => {
22-
field.querySelectorAll('input, textarea').forEach(input => {
22+
field.querySelectorAll('input:not([type="radio"]), textarea').forEach(input => {
2323
if (localStorage.getItem(field.id)) {
2424
input.value = localStorage.getItem(field.id);
2525
document.querySelector('.fixed2').classList.add('visible');
@@ -39,6 +39,26 @@ window.onload = function () {
3939
}, 2000);
4040
};
4141
});
42+
field.querySelectorAll('.radioOption').forEach(radio => {
43+
const input = radio.querySelector('input[type="radio"]');
44+
if (localStorage.getItem(field.id) === input.value) {
45+
field.querySelectorAll('.radioOption input[type="radio"]').forEach(r => r.checked = false);
46+
input.checked = true;
47+
document.querySelector('.fixed2').classList.add('visible');
48+
setTimeout(() => {
49+
document.querySelector('.fixed2').classList.remove('visible');
50+
}, 2000);
51+
};
52+
});
53+
field.querySelectorAll('select').forEach(select => {
54+
if (localStorage.getItem(field.id)) {
55+
select.value = localStorage.getItem(field.id);
56+
document.querySelector('.fixed2').classList.add('visible');
57+
setTimeout(() => {
58+
document.querySelector('.fixed2').classList.remove('visible');
59+
}, 2000);
60+
};
61+
});
4262
});
4363
};
4464

@@ -67,6 +87,17 @@ function next() {
6787
shake(currentField);
6888
return;
6989
};
90+
} else if (input.type === 'radio') {
91+
const radios = currentField.querySelectorAll('input[type="radio"]');
92+
var checked = false;
93+
radios.forEach(radio => {
94+
if (radio.checked) checked = true;
95+
});
96+
if (!checked) {
97+
input.focus();
98+
shake(currentField);
99+
return;
100+
};
70101
} else {
71102
if (!input.value.trim()) {
72103
input.focus();
@@ -117,36 +148,42 @@ function back() {
117148

118149
function saveChange(key, value) {
119150
localStorage.setItem(key, value);
151+
console.log(key, value)
120152
document.querySelector('.fixed2').classList.add('visible');
121153
setTimeout(() => {
122154
document.querySelector('.fixed2').classList.remove('visible');
123155
}, 2000);
124156
};
125157

126158
document.querySelectorAll('.inputField').forEach(field => {
127-
field.querySelectorAll('input, textarea').forEach(input => {
159+
field.querySelectorAll('input:not([type="radio"]), textarea').forEach(input => {
128160
input.addEventListener('keydown', function (event) {
129161
if (event.key === 'Enter') {
130162
event.preventDefault();
131163
next();
132164
};
133-
saveChange(field.id, input.value);
165+
setTimeout(() => saveChange(field.id, input.value), 100);
134166
});
135167
});
136168
field.querySelectorAll('.checkbox').forEach(checkbox => {
137169
checkbox.addEventListener('click', function () {
138170
checkbox.classList.toggle('checked');
139171
const input = field.querySelector('input[type="checkbox"]');
140172
input.checked = !input.checked;
141-
saveChange(field.id, input.checked);
173+
setTimeout(() => saveChange(field.id, input.checked), 100);
142174
});
143175
});
144176
field.querySelectorAll('.radioOption').forEach(radio => {
145-
radio.addEventListener('click', function () {
177+
radio.addEventListener('change', function () {
146178
field.querySelectorAll('.radioOption input[type="radio"]').forEach(r => r.checked = false);
147179
const input = radio.querySelector('input[type="radio"]');
148180
input.checked = true;
149-
saveChange(field.id, input.value);
181+
setTimeout(() => saveChange(field.id, input.value), 100);
182+
});
183+
});
184+
field.querySelectorAll('select').forEach(select => {
185+
select.addEventListener('change', function () {
186+
setTimeout(() => saveChange(field.id, select.selectedOptions[0].value), 100);
150187
});
151188
});
152189
});
@@ -173,6 +210,11 @@ async function create() {
173210
if (input) {
174211
if (input.type === 'checkbox') {
175212
data[field.id] = input.checked;
213+
} if (input.type === 'radio') {
214+
const radios = field.querySelectorAll('input[type="radio"]');
215+
radios.forEach(radio => {
216+
if (radio.checked) data[field.id] = radio.value;
217+
});
176218
} else {
177219
data[field.id] = input.value;
178220
};

frontend/public/styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ input[type="radio"]:checked {
173173
display: flex;
174174
align-items: center;
175175
gap: 5px;
176+
177+
label {
178+
cursor: pointer;
179+
}
176180
}
177181
}
178182

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ app.post('/create', async (req, res) => {
156156
role: getUserRole(req.session) || 'user'
157157
} : {};
158158
if (returnHandler && typeof returnHandler === 'function') {
159-
// try {
160-
// await returnHandler(data);
161-
// } catch (error) {
162-
// console.error('Error in handler function:', error);
159+
try {
160+
await returnHandler(data);
161+
} catch (error) {
162+
console.error('Error in handler function:', error);
163163
return res.status(500).json({ status: 'error', message: 'An error occurred while processing your request. Please try again later.' });
164-
// };
164+
};
165165
};
166166
res.status(200).json({ status: 'success', message: 'Your commission was created successfully.' });
167167
});

0 commit comments

Comments
 (0)