diff --git a/internal/web/admin.go b/internal/web/admin.go index 6eeca4e..1719294 100644 --- a/internal/web/admin.go +++ b/internal/web/admin.go @@ -185,6 +185,14 @@ func adminSyncReposFromDB(ctx echo.Context) error { return redirect(ctx, "/admin-panel") } +func adminSettings(ctx echo.Context) error { + setData(ctx, "title", "Admin Settings") + setData(ctx, "htmlTitle", "Admin Settings - Admin panel") + setData(ctx, "adminHeaderPage", "settings") + + return html(ctx, "admin_settings.html") +} + func adminSetSetting(ctx echo.Context) error { key := ctx.FormValue("key") value := ctx.FormValue("value") diff --git a/internal/web/run.go b/internal/web/run.go index 888f5e7..5cf1851 100644 --- a/internal/web/run.go +++ b/internal/web/run.go @@ -188,6 +188,7 @@ func Start() { g2.POST("/gists/:gist/delete", adminGistDelete) g2.POST("/sync-fs", adminSyncReposFromFS) g2.POST("/sync-db", adminSyncReposFromDB) + g2.GET("/settings", adminSettings) g2.PUT("/set-setting", adminSetSetting) } diff --git a/public/admin.ts b/public/admin.ts index 4dffe80..bba4ab4 100644 --- a/public/admin.ts +++ b/public/admin.ts @@ -1,7 +1,10 @@ document.addEventListener('DOMContentLoaded', () => { - registerDomSetting(document.getElementById('disable-signup') as HTMLInputElement); - registerDomSetting(document.getElementById('require-login') as HTMLInputElement); - registerDomSetting(document.getElementById('disable-login-form') as HTMLInputElement); + let elems = Array.from(document.getElementsByClassName("toggle-button")); + for (let elem of elems) { + elem.addEventListener('click', () => { + registerDomSetting(elem as HTMLElement) + }) + } }); const setSetting = (key: string, value: string) => { @@ -9,16 +12,21 @@ const setSetting = (key: string, value: string) => { data.append('key', key); data.append('value', value); data.append('_csrf', ((document.getElementsByName('_csrf')[0] as HTMLInputElement).value)); - fetch('/admin-panel/set-setting', { + return fetch('/admin-panel/set-setting', { method: 'PUT', credentials: 'same-origin', body: data, }); }; -const registerDomSetting = (el: HTMLInputElement) => { - el.addEventListener('change', () => { - setSetting(el.id, el.checked ? '1' : '0'); - }); +const registerDomSetting = (el: HTMLElement) => { + // @ts-ignore + el.dataset["bool"] = !(el.dataset["bool"] === 'true'); + setSetting(el.id, el.dataset["bool"] === 'true' ? '1' : '0') + .then(() => { + el.classList.toggle("bg-primary-600"); + el.classList.toggle("bg-gray-400"); + (el.childNodes.item(1) as HTMLElement).classList.toggle("translate-x-5"); + }); }; diff --git a/templates/base/admin_header.html b/templates/base/admin_header.html index 598a7cc..df028bc 100644 --- a/templates/base/admin_header.html +++ b/templates/base/admin_header.html @@ -15,6 +15,8 @@ {{ else }} text-gray-400 hover:text-slate-300 px-3 py-2 font-medium text-sm rounded-md {{ end }}" aria-current="page">Users Gists + Admin settings diff --git a/templates/pages/admin_index.html b/templates/pages/admin_index.html index d59e0d9..0ef7351 100644 --- a/templates/pages/admin_index.html +++ b/templates/pages/admin_index.html @@ -71,32 +71,7 @@ - -