Add constraint kind validation per level and reorder constraint form
This commit is contained in:
16
main.go
16
main.go
@@ -638,6 +638,22 @@ func handleCreateConstraint(db *sql.DB) http.HandlerFunc {
|
|||||||
http.Error(w, "students must be different", http.StatusBadRequest)
|
http.Error(w, "students must be different", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
switch body.Level {
|
||||||
|
case "student":
|
||||||
|
if body.Kind != "prefer" && body.Kind != "prefer_not" {
|
||||||
|
http.Error(w, "students may only use prefer or prefer not", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "parent":
|
||||||
|
if body.Kind != "must_not" {
|
||||||
|
http.Error(w, "parents may only use must not", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "admin":
|
||||||
|
default:
|
||||||
|
http.Error(w, "invalid level", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
a, b := body.StudentAID, body.StudentBID
|
a, b := body.StudentAID, body.StudentBID
|
||||||
if a > b {
|
if a > b {
|
||||||
a, b = b, a
|
a, b = b, a
|
||||||
|
|||||||
@@ -142,6 +142,30 @@ async function loadStudents() {
|
|||||||
|
|
||||||
const addRow = document.createElement('div');
|
const addRow = document.createElement('div');
|
||||||
addRow.className = 'constraint-add';
|
addRow.className = 'constraint-add';
|
||||||
|
const levelKinds = {
|
||||||
|
student: ['prefer', 'prefer_not'],
|
||||||
|
parent: ['must_not'],
|
||||||
|
admin: ['must', 'prefer', 'prefer_not', 'must_not']
|
||||||
|
};
|
||||||
|
const levelSelect = document.createElement('select');
|
||||||
|
for (const level of ['student', 'parent', 'admin']) {
|
||||||
|
const opt = document.createElement('option');
|
||||||
|
opt.value = level;
|
||||||
|
opt.textContent = level.charAt(0).toUpperCase() + level.slice(1);
|
||||||
|
levelSelect.appendChild(opt);
|
||||||
|
}
|
||||||
|
const kindSelect = document.createElement('select');
|
||||||
|
const updateKinds = () => {
|
||||||
|
kindSelect.innerHTML = '';
|
||||||
|
for (const kind of levelKinds[levelSelect.value]) {
|
||||||
|
const opt = document.createElement('option');
|
||||||
|
opt.value = kind;
|
||||||
|
opt.textContent = kindLabels[kind];
|
||||||
|
kindSelect.appendChild(opt);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
updateKinds();
|
||||||
|
levelSelect.addEventListener('change', updateKinds);
|
||||||
const studentSelect = document.createElement('select');
|
const studentSelect = document.createElement('select');
|
||||||
const defaultOpt = document.createElement('option');
|
const defaultOpt = document.createElement('option');
|
||||||
defaultOpt.value = '';
|
defaultOpt.value = '';
|
||||||
@@ -154,20 +178,6 @@ async function loadStudents() {
|
|||||||
opt.textContent = other.name;
|
opt.textContent = other.name;
|
||||||
studentSelect.appendChild(opt);
|
studentSelect.appendChild(opt);
|
||||||
}
|
}
|
||||||
const kindSelect = document.createElement('select');
|
|
||||||
for (const kind of ['must', 'prefer', 'prefer_not', 'must_not']) {
|
|
||||||
const opt = document.createElement('option');
|
|
||||||
opt.value = kind;
|
|
||||||
opt.textContent = kindLabels[kind];
|
|
||||||
kindSelect.appendChild(opt);
|
|
||||||
}
|
|
||||||
const levelSelect = document.createElement('select');
|
|
||||||
for (const level of ['student', 'parent', 'admin']) {
|
|
||||||
const opt = document.createElement('option');
|
|
||||||
opt.value = level;
|
|
||||||
opt.textContent = level.charAt(0).toUpperCase() + level.slice(1);
|
|
||||||
levelSelect.appendChild(opt);
|
|
||||||
}
|
|
||||||
const cAddBtn = document.createElement('button');
|
const cAddBtn = document.createElement('button');
|
||||||
cAddBtn.className = 'input-action';
|
cAddBtn.className = 'input-action';
|
||||||
cAddBtn.textContent = '+';
|
cAddBtn.textContent = '+';
|
||||||
@@ -182,9 +192,9 @@ async function loadStudents() {
|
|||||||
});
|
});
|
||||||
loadStudents();
|
loadStudents();
|
||||||
});
|
});
|
||||||
addRow.appendChild(studentSelect);
|
|
||||||
addRow.appendChild(kindSelect);
|
|
||||||
addRow.appendChild(levelSelect);
|
addRow.appendChild(levelSelect);
|
||||||
|
addRow.appendChild(kindSelect);
|
||||||
|
addRow.appendChild(studentSelect);
|
||||||
addRow.appendChild(cAddBtn);
|
addRow.appendChild(cAddBtn);
|
||||||
cDetails.appendChild(addRow);
|
cDetails.appendChild(addRow);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user