From 08f4f0e72dd9da92dbbce7551d9d048bf4158bc5 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Fri, 15 Mar 2024 04:53:43 -0400 Subject: [PATCH] use api for admin config page --- relay/frontend/page/admin-config.haml | 32 +++++++++++++------------ relay/frontend/static/config.js | 34 +++++++++++++++++++++++++++ relay/views/api.py | 2 ++ 3 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 relay/frontend/static/config.js diff --git a/relay/frontend/page/admin-config.haml b/relay/frontend/page/admin-config.haml index ff6c4f6..6519c0e 100644 --- a/relay/frontend/page/admin-config.haml +++ b/relay/frontend/page/admin-config.haml @@ -1,27 +1,29 @@ -extends "base.haml" -set page="Config" + +-block head + %script(type="application/javascript" src="/static/config.js" nonce="{{view.request['hash']}}" defer) + -import "functions.haml" as func -block content %fieldset.section %legend << Config - %form(action="/admin/config" method="POST") - .grid-2col - %label(for="name") << Name - %input(id = "name" name="name" placeholder="Relay Name" value="{{config.name or ''}}") - %label(for="description") << Description - %textarea(id="description" name="note" value="{{config.note or ''}}") << {{config.note}} + .grid-2col + %label(for="name") << Name + %input(id = "name" name="name" placeholder="Relay Name" value="{{config.name or ''}}") - %label(for="theme") << Color Theme - =func.new_select("theme", config.theme, themes) + %label(for="description") << Description + %textarea(id="description" name="note" value="{{config.note or ''}}") << {{config.note}} - %label(for="log-level") << Log Level - =func.new_select("log-level", config.log_level.name, levels) + %label(for="theme") << Color Theme + =func.new_select("theme", config.theme, themes) - %label(for="whitelist-enabled") << Whitelist - =func.new_checkbox("whitelist-enabled", config.whitelist_enabled) + %label(for="log-level") << Log Level + =func.new_select("log-level", config.log_level.name, levels) - %label(for="approval-required") << Approval Required - =func.new_checkbox("approval-required", config.approval_required) + %label(for="whitelist-enabled") << Whitelist + =func.new_checkbox("whitelist-enabled", config.whitelist_enabled) - %input(type="submit" value="Save") + %label(for="approval-required") << Approval Required + =func.new_checkbox("approval-required", config.approval_required) diff --git a/relay/frontend/static/config.js b/relay/frontend/static/config.js new file mode 100644 index 0000000..5d7b52f --- /dev/null +++ b/relay/frontend/static/config.js @@ -0,0 +1,34 @@ +const elems = [ + document.querySelector("#name"), + document.querySelector("#description"), + document.querySelector("#theme"), + document.querySelector("#log-level"), + document.querySelector("#whitelist-enabled"), + document.querySelector("#approval-required") +] + + +async function handle_config_change(event) { + params = { + key: event.target.id, + value: event.target.type === "checkbox" ? event.target.checked : event.target.value + } + + try { + await client.request("POST", "v1/config", params); + + } catch (error) { + alert(error); + return; + } + + if (params.key === "name") { + document.querySelector("#header .title").innerHTML = params.value; + document.querySelector("title").innerHTML = params.value; + } +} + + +for (const elem of elems) { + elem.addEventListener("change", handle_config_change); +} diff --git a/relay/views/api.py b/relay/views/api.py index ca0470c..ddd80e4 100644 --- a/relay/views/api.py +++ b/relay/views/api.py @@ -133,6 +133,8 @@ class Config(View): if isinstance(data, Response): return data + data['key'] = data['key'].replace('-', '_'); + if data['key'] not in ConfigData.USER_KEYS(): return Response.new_error(400, 'Invalid key', 'json')