update perms button + favicon

This commit is contained in:
Erik 2023-03-22 19:07:03 +02:00
parent d31d106785
commit 4346f81e23
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68
2 changed files with 35 additions and 16 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 114 KiB

View File

@ -1,22 +1,28 @@
import React, { useEffect, useRef, useState } from "react"; import React, { useEffect, useRef, useState } from "react";
import { Route, Routes, useNavigate, useParams } from "react-router"; import { Route, Routes, useNavigate, useParams } from "react-router";
import ErrorBoundary from "../util/ErrorBoundary"; import ErrorBoundary from "../util/ErrorBoundary";
import { get } from '../util/Util'; import { get, post } from '../util/Util';
import '../css/pages/Users.css'; import '../css/pages/Users.css';
import { Table, TableListEntry } from "../components/Table"; import { Table, TableListEntry } from "../components/Table";
const Permission = ({name, value}) => { const Permission = ({ name, value, chain, updatePerms }) => {
const inputRef = useRef();
const onChange = () => {
const val = inputRef.current.value;
updatePerms(chain, val);
};
return <li className="flex is-vertical-align"> return <li className="flex is-vertical-align">
<label>{name}</label> <label>{name}</label>
<input max={10} min={0} type='number' defaultValue={value}></input> <input ref={inputRef} onChange={onChange} max={10} min={0} type='number' defaultValue={value}></input>
</li>; </li>;
}; };
const PermissionGroup = ({ name, value }) => { const PermissionGroup = ({ name, value, chain, updatePerms }) => {
const elements = []; const elements = [];
for (const [perm, val] of Object.entries(value)) { for (const [perm, val] of Object.entries(value)) {
const props = { key: perm, name: perm, value: val }; const props = { key: perm, name: perm, value: val, updatePerms, chain: `${chain}:${perm}` };
if(typeof val ==='object') elements.push(<PermissionGroup {...props} />); if(typeof val ==='object') elements.push(<PermissionGroup {...props} />);
else elements.push(<Permission {...props} />); else elements.push(<Permission {...props} />);
} }
@ -30,12 +36,12 @@ const PermissionGroup = ({ name, value }) => {
</li>; </li>;
}; };
const Permissions = ({ perms }) => { const Permissions = ({ perms, updatePerms }) => {
const elements = []; const elements = [];
const keys = Object.keys(perms); const keys = Object.keys(perms);
for (const perm of keys) { for (const perm of keys) {
const props = { key: perm, name: perm, value: perms[perm] }; const props = { key: perm, name: perm, value: perms[perm], chain: perm, updatePerms };
let Elem = null; let Elem = null;
switch (typeof perms[perm]) { switch (typeof perms[perm]) {
case 'number': case 'number':
@ -96,6 +102,7 @@ const User = ({ user }) => {
const navigate = useNavigate(); const navigate = useNavigate();
const [apps, updateApps] = useState([]); const [apps, updateApps] = useState([]);
const perms = {...user.permissions};
useEffect(() => { useEffect(() => {
(async () => { (async () => {
@ -104,6 +111,22 @@ const User = ({ user }) => {
})(); })();
}, []); }, []);
const updatePerms = (chain, value) => {
value = parseInt(value);
if (isNaN(value)) return;
let selected = perms;
const keys = chain.split(':');
for (const key of keys) {
if (key === keys[keys.length - 1]) selected[key] = value;
else selected = selected[key];
}
};
const commitPerms = async () => {
await post(`/api/users/${user.id}/permissions`, perms);
};
const ApplicationWrapper = () => { const ApplicationWrapper = () => {
const { appid } = useParams(); const { appid } = useParams();
const app = apps.find(a => a.id === appid); const app = apps.find(a => a.id === appid);
@ -130,17 +153,12 @@ const User = ({ user }) => {
<p><b>2FA: </b>{user.twoFactor.toString()}</p> <p><b>2FA: </b>{user.twoFactor.toString()}</p>
<p><b>Disabled: </b>{user.disabled.toString()}</p> <p><b>Disabled: </b>{user.disabled.toString()}</p>
<label htmlFor='username'>Username:</label> <label htmlFor='username'>Username</label>
<input autoComplete='off' id='username' defaultValue={user.username} /> <input autoComplete='off' id='username' defaultValue={user.name} />
<label htmlFor='displayName'>Display Name:</label> <label htmlFor='displayName'>Display Name</label>
<input autoComplete='off' id='displayName' placeholder='Name to display instead of username' defaultValue={user.displayName} /> <input autoComplete='off' id='displayName' placeholder='Name to display instead of username' defaultValue={user.displayName} />
<select>
<option>User</option>
<option>Bob</option>
</select>
<h3 className="mt-5">Applications</h3> <h3 className="mt-5">Applications</h3>
<Routes> <Routes>
<Route path='/' element={<ApplicationList apps={apps} />} /> <Route path='/' element={<ApplicationList apps={apps} />} />
@ -150,7 +168,8 @@ const User = ({ user }) => {
</div> </div>
<div className="col-6-lg col-12"> <div className="col-6-lg col-12">
<Permissions perms={user.permissions} /> <Permissions updatePerms={updatePerms} perms={user.permissions} />
<button onClick={commitPerms} className="button primary">Update</button>
</div> </div>
</div> </div>