Serve level_kinds from server, use for both validation and UI
This commit is contained in:
26
main.go
26
main.go
@@ -29,6 +29,11 @@ var schema string
|
||||
var (
|
||||
htmlTemplates *template.Template
|
||||
jsTemplates *texttemplate.Template
|
||||
levelKinds = map[string][]string{
|
||||
"student": {"prefer", "prefer_not"},
|
||||
"parent": {"must_not"},
|
||||
"admin": {"must", "prefer", "prefer_not", "must_not"},
|
||||
}
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -452,7 +457,7 @@ func handleTripMe(db *sql.DB) http.HandlerFunc {
|
||||
students = []studentInfo{}
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]any{"role": role, "students": students})
|
||||
json.NewEncoder(w).Encode(map[string]any{"role": role, "students": students, "level_kinds": levelKinds})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -893,22 +898,15 @@ func handleCreateConstraint(db *sql.DB) http.HandlerFunc {
|
||||
http.Error(w, "students must be different", http.StatusBadRequest)
|
||||
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:
|
||||
validKinds := levelKinds[body.Level]
|
||||
if validKinds == nil {
|
||||
http.Error(w, "invalid level", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if !slices.Contains(validKinds, body.Kind) {
|
||||
http.Error(w, "invalid kind for level", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if role != "admin" {
|
||||
if body.Level != role {
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
|
||||
@@ -411,11 +411,7 @@ async function loadStudents() {
|
||||
|
||||
const addRow = document.createElement('div');
|
||||
addRow.className = 'constraint-add';
|
||||
const levelKinds = {
|
||||
student: ['prefer', 'prefer_not'],
|
||||
parent: ['must_not'],
|
||||
admin: ['must', 'prefer', 'prefer_not', 'must_not']
|
||||
};
|
||||
const levelKinds = me.level_kinds;
|
||||
const levelSelect = document.createElement('wa-select');
|
||||
levelSelect.size = 'small';
|
||||
for (const level of ['student', 'parent', 'admin']) {
|
||||
@@ -618,9 +614,7 @@ async function renderMemberView(me) {
|
||||
? { '': 'OK to room with', prefer: 'Would like to room with', prefer_not: 'Would prefer not to room with' }
|
||||
: { '': 'OK to room with', must_not: 'Not OK to room with' };
|
||||
|
||||
const kindOptions = me.role === 'student'
|
||||
? ['', 'prefer', 'prefer_not']
|
||||
: ['', 'must_not'];
|
||||
const kindOptions = ['', ...me.level_kinds[me.role]];
|
||||
|
||||
const pendingSelects = [];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user