diff --git a/main.go b/main.go index 2907831..4274b58 100644 --- a/main.go +++ b/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) diff --git a/static/trip.js b/static/trip.js index ec24942..6a61174 100644 --- a/static/trip.js +++ b/static/trip.js @@ -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 = [];