From 9bfbcc56f640f5b814bb1b30b8dd63437bc42f50 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Fri, 13 Jan 2023 16:45:59 -0800 Subject: [PATCH] fixing navigation to admin dashboard --- net/web/src/access/admin/Admin.jsx | 4 +- net/web/src/access/admin/useAdmin.hook.js | 21 +++- net/web/src/context/useAppContext.hook.js | 10 ++ net/web/src/dashboard/Dashboard.jsx | 14 +-- net/web/src/dashboard/Dashboard.styled.js | 1 + .../src/dashboard/accountItem/AccountItem.jsx | 4 +- .../accountItem/useAccountItem.hook.js | 12 ++- net/web/src/dashboard/useDashboard.hook.js | 99 +++++++++++-------- 8 files changed, 103 insertions(+), 62 deletions(-) diff --git a/net/web/src/access/admin/Admin.jsx b/net/web/src/access/admin/Admin.jsx index a006e463..fa2d820e 100644 --- a/net/web/src/access/admin/Admin.jsx +++ b/net/web/src/access/admin/Admin.jsx @@ -1,5 +1,5 @@ import { Button, Modal, Form, Input } from 'antd'; -import { SettingOutlined, LockOutlined } from '@ant-design/icons'; +import { UserOutlined, LockOutlined } from '@ant-design/icons'; import { AdminWrapper } from './Admin.styled'; import { useAdmin } from './useAdmin.hook'; @@ -30,7 +30,7 @@ export function Admin() {
Databag
actions.navUser()}> - +
Admin
diff --git a/net/web/src/access/admin/useAdmin.hook.js b/net/web/src/access/admin/useAdmin.hook.js index 918d1328..5e5d396f 100644 --- a/net/web/src/access/admin/useAdmin.hook.js +++ b/net/web/src/access/admin/useAdmin.hook.js @@ -3,6 +3,7 @@ import { useNavigate } from "react-router-dom"; import { getNodeStatus } from 'api/getNodeStatus'; import { setNodeStatus } from 'api/setNodeStatus'; import { getNodeConfig } from 'api/getNodeConfig'; +import { AppContext } from 'context/AppContext'; export function useAdmin() { @@ -14,6 +15,7 @@ export function useAdmin() { }); const navigate = useNavigate(); + const app = useContext(AppContext); const updateState = (value) => { setState((s) => ({ ...s, ...value })); @@ -32,6 +34,12 @@ export function useAdmin() { check(); }, []); + useEffect(() => { + if (app.state.adminToken) { + navigate('/dashboard'); + } + }, [app.state]); + const actions = { setPassword: (password) => { updateState({ password }); @@ -46,14 +54,17 @@ export function useAdmin() { if (state.unclaimed === true) { await setNodeStatus(state.password); } - const status = await getNodeConfig(state.password); - const config = encodeURIComponent(JSON.stringify(config)); - const pass= encodeURIComponent(state.password); - +console.log("CHECK1"); + await getNodeConfig(state.password); +console.log("CHECK2"); updateState({ busy: false }); - navigate(`/dashboard?config=${status}&pass=${pass}`); +console.log("CHECK3"); +console.log(app); + app.actions.setAdmin(state.password); +console.log("DONE!"); } catch(err) { +console.log("ERROR!"); console.log(err); updateState({ busy: false }); throw new Error("login failed"); diff --git a/net/web/src/context/useAppContext.hook.js b/net/web/src/context/useAppContext.hook.js index 1f0e8d3f..589573e3 100644 --- a/net/web/src/context/useAppContext.hook.js +++ b/net/web/src/context/useAppContext.hook.js @@ -14,6 +14,7 @@ import { createWebsocket } from 'api/fetchUtil'; export function useAppContext(websocket) { const [state, setState] = useState({ status: null, + adminToken: null, }); const [appRevision, setAppRevision] = useState(); @@ -26,6 +27,7 @@ export function useAppContext(websocket) { const ws = useRef(null); const updateState = (value) => { +console.log(" UPDATE STATE", value); setState((s) => ({ ...s, ...value })) } @@ -77,6 +79,14 @@ export function useAppContext(websocket) { create: async (username, password, token) => { await appCreate(username, password, token) }, + setAdmin: (token) => { +console.log("TOKEN SET!", token); + updateState({ adminToken: token }); +console.log("TOKEN SET: done"); + }, + clearAdmin: () => { + updateState({ adminToken: null }); + }, } const appCreate = async (username, password, token) => { diff --git a/net/web/src/dashboard/Dashboard.jsx b/net/web/src/dashboard/Dashboard.jsx index 3447bd23..13dccf01 100644 --- a/net/web/src/dashboard/Dashboard.jsx +++ b/net/web/src/dashboard/Dashboard.jsx @@ -4,9 +4,12 @@ import { ExclamationCircleOutlined, SettingOutlined, CopyOutlined, UserAddOutlin import { useDashboard } from './useDashboard.hook'; import { AccountItem } from './accountItem/AccountItem'; -export function Dashboard({ token, config, logout }) { +export function Dashboard() { - const { state, actions } = useDashboard(token, config); +console.log("IN DASHBOARD"); + + const { state, actions } = useDashboard(); +console.log("IN DASHBOARD here"); const onClipboard = (value) => { navigator.clipboard.writeText(value); @@ -33,7 +36,7 @@ export function Dashboard({ token, config, logout }) {
} - onClick={() => logout()}> + onClick={() => actions.logout()}>
{ state.errorMessage && (
@@ -63,7 +66,7 @@ export function Dashboard({ token, config, logout }) {
} - onClick={() => logout()}> + onClick={() => actions.logout()}>
{ state.errorMessage && ( @@ -89,8 +92,7 @@ export function Dashboard({ token, config, logout }) { itemLayout="horizontal" dataSource={state.accounts} loading={state.loading} - renderItem={item => ()} + renderItem={item => ()} />
diff --git a/net/web/src/dashboard/Dashboard.styled.js b/net/web/src/dashboard/Dashboard.styled.js index 6f2ffd7b..966869e3 100644 --- a/net/web/src/dashboard/Dashboard.styled.js +++ b/net/web/src/dashboard/Dashboard.styled.js @@ -21,6 +21,7 @@ export const DashboardWrapper = styled.div` border-radius: 4px; max-width: 100%; max-height: 80%; + background-color: ${Colors.formBackground}; .header { color: #444444; diff --git a/net/web/src/dashboard/accountItem/AccountItem.jsx b/net/web/src/dashboard/accountItem/AccountItem.jsx index db291681..497d5da0 100644 --- a/net/web/src/dashboard/accountItem/AccountItem.jsx +++ b/net/web/src/dashboard/accountItem/AccountItem.jsx @@ -4,9 +4,9 @@ import { useAccountItem } from './useAccountItem.hook'; import { ExclamationCircleOutlined, CopyOutlined, UserDeleteOutlined, UnlockOutlined, CloseCircleOutlined, CheckCircleOutlined } from '@ant-design/icons'; import { Modal, Tooltip, Button } from 'antd'; -export function AccountItem({ token, item, remove }) { +export function AccountItem({ item, remove }) { - const { state, actions } = useAccountItem(token, item, remove); + const { state, actions } = useAccountItem(item, remove); const removeAccount = () => { Modal.confirm({ diff --git a/net/web/src/dashboard/accountItem/useAccountItem.hook.js b/net/web/src/dashboard/accountItem/useAccountItem.hook.js index bc853067..42e5b0b3 100644 --- a/net/web/src/dashboard/accountItem/useAccountItem.hook.js +++ b/net/web/src/dashboard/accountItem/useAccountItem.hook.js @@ -3,8 +3,9 @@ import { getAccountImageUrl } from 'api/getAccountImageUrl'; import { setAccountStatus } from 'api/setAccountStatus'; import { addAccountAccess } from 'api/addAccountAccess'; import { ViewportContext } from 'context/ViewportContext'; +import { AppContext } from 'context/AppContext'; -export function useAccountItem(token, item, remove) { +export function useAccountItem(item, remove) { const [state, setState] = useState({ statusBusy: false, @@ -13,6 +14,7 @@ export function useAccountItem(token, item, remove) { showAccess: false, }); + const app = useContext(AppContext); const viewport = useContext(ViewportContext); const updateState = (value) => { @@ -27,9 +29,9 @@ export function useAccountItem(token, item, remove) { name: item?.name, guid: item?.guid, handle: item?.handle, - imageUrl: item?.imageSet ? getAccountImageUrl(token, item?.accountId) : null, + imageUrl: item?.imageSet ? getAccountImageUrl(app.state.adminToken, item?.accountId) : null, }); - }, [token, item]); + }, [app.state.adminToken, item]); useEffect(() => { updateState({ display: viewport.state.display }); @@ -40,7 +42,7 @@ export function useAccountItem(token, item, remove) { if (!state.accessBusy) { updateState({ accessBusy: true }); try { - let access = await addAccountAccess(token, item.accountId); + let access = await addAccountAccess(app.state.adminToken, item.accountId); updateState({ accessToken: access, showAccess: true }); } catch (err) { @@ -69,7 +71,7 @@ export function useAccountItem(token, item, remove) { if (!state.statusBusy) { updateState({ statusBusy: true }); try { - await setAccountStatus(token, item.accountId, disabled); + await setAccountStatus(app.state.adminToken, item.accountId, disabled); updateState({ disabled, activeClass: disabled ? 'inactive' : 'active' }); } catch(err) { diff --git a/net/web/src/dashboard/useDashboard.hook.js b/net/web/src/dashboard/useDashboard.hook.js index 9342820e..39953265 100644 --- a/net/web/src/dashboard/useDashboard.hook.js +++ b/net/web/src/dashboard/useDashboard.hook.js @@ -1,9 +1,11 @@ import { useContext, useRef, useState, useEffect } from 'react'; +import { getNodeConfig } from 'api/getNodeConfig'; import { setNodeConfig } from 'api/setNodeConfig'; import { getNodeAccounts } from 'api/getNodeAccounts'; import { removeAccount } from 'api/removeAccount'; import { addAccountCreate } from 'api/addAccountCreate'; -import { useNavigation, useLocation } from 'react-router-dom'; +import { useNavigate } from 'react-router-dom'; +import { AppContext } from 'context/AppContext'; export function useDashboard() { @@ -24,20 +26,32 @@ export function useDashboard() { accounts: [], }); - const navigate = useNavigation(); - const location = useLocation(); - const token = useRef(); +console.log("HERE1"); + const navigate = useNavigate(); +console.log("HERE2"); + const app = useContext(AppContext); +console.log("HERE4"); const updateState = (value) => { setState((s) => ({ ...s, ...value })); } + useEffect(() => { + if (!app.state.adminToken) { + navigate('/admin'); + } + else { + syncConfig(); + syncAccounts(); + } + }, [app]); + const actions = { setCreateLink: async () => { if (!state.createBusy) { updateState({ busy: true }); try { - const create = await addAccountCreate(token.current) + const create = await addAccountCreate(app.state.adminToken) updateState({ createToken: create, showCreate: true }); } catch (err) { @@ -50,7 +64,7 @@ export function useDashboard() { updateState({ showCreate }); }, removeAccount: async (accountId) => { - await removeAccount(token, accountId); + await removeAccount(app.state.adminToken, accountId); actions.getAccounts(); }, setHost: (domain) => { @@ -77,6 +91,12 @@ export function useDashboard() { setShowSettings: (value) => { updateState({ showSettings: value }); }, + logout: () => { + app.actions.clearAdmin(); + }, + getAccounts: async () => { + await syncAccounts(); + }, setSettings: async () => { if (!state.busy) { updateState({ busy: true }); @@ -84,7 +104,7 @@ export function useDashboard() { const { domain, keyType, accountStorage, pushSupported, enableImage, enableAudio, enableVideo } = state; const storage = accountStorage * 1073741824; const config = { domain, accountStorage: storage, keyType, enableImage, enableAudio, enableVideo, pushSupported }; - await setNodeConfig(token.current, config); + await setNodeConfig(app.state.adminToken, config); updateState({ busy: false, showSettings: false }); } catch(err) { @@ -94,46 +114,41 @@ export function useDashboard() { } } }, - getAccounts: async () => { - if (!state.busy) { - updateState({ busy: true }); - try { - let accounts = await getNodeAccounts(token.current); - accounts.sort((a, b) => { - if (a.handle < b.handle) { - return -1; - } - if (a.handle > b.handle) { - return 1; - } - return 0; - }); - updateState({ busy: false, accounts }); - } - catch(err) { - console.log(err); - updateState({ busy: false, errorMessage: 'failed to load accounts' }); - } - } - }, }; - useEffect(() => { - const params = new URLSearchParams(location); - const pass = params.get("pass"); - if (!pass) { - navigate('/admin'); - } - else { - token.current = pass; - const config = JSON.parse(params.get("config")); + const syncConfig = async () => { + try { + const config = await getNodeConfig(app.state.adminToken); const { storage, domain, keyType, pushSupported, enableImage, enableAudio, enableVideo } = config; - const accountStorage = Math.ceil(accountStorage / 1073741824); + const accountStorage = Math.ceil(storage / 1073741824); updateState({ domain, accountStorage, keyType, enableImage, enableAudio, enableVideo, pushSupported }); - actions.getAccounts(); } - }, []); - + catch(err) { + console.log(err); + updateState({ errorMessage: 'failed to sync config' }); + } + }; + + const syncAccounts = async () => { + try { + const accounts = await getNodeAccounts(app.state.adminToken); + accounts.sort((a, b) => { + if (a.handle < b.handle) { + return -1; + } + if (a.handle > b.handle) { + return 1; + } + return 0; + }); + updateState({ accounts }); + } + catch(err) { + console.log(err); + updateState({ errorMessage: 'failed to sync accounts' }); + } + }; + return { state, actions }; }