From 336428ed5637f0c19c3c88da38eaab5770eb10d8 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Wed, 23 Nov 2022 19:14:01 +0200 Subject: [PATCH 1/5] table generic --- src/components/Sidebar copy.js | 29 ------------ src/components/Table.js | 24 ++++++++++ src/pages/Users.js | 87 +++++++++++++++++++++++----------- 3 files changed, 83 insertions(+), 57 deletions(-) delete mode 100644 src/components/Sidebar copy.js create mode 100644 src/components/Table.js diff --git a/src/components/Sidebar copy.js b/src/components/Sidebar copy.js deleted file mode 100644 index b08e231..0000000 --- a/src/components/Sidebar copy.js +++ /dev/null @@ -1,29 +0,0 @@ -import React from 'react'; -import '../css/components/Sidebar.css'; -import NavLink from '../structures/NavLink'; - -const Sidebar = ({children}) => { - - return
- {children} -
; - -}; - -// Nav menu -const SidebarMenu = ({menuItems, children}) => { - - let key = 0; - return
- {menuItems.map(link => { - const { label, ...rest } = link; - return { label}; - })} - - {children} -
; - -}; - -export {SidebarMenu, Sidebar}; -export default Sidebar; \ No newline at end of file diff --git a/src/components/Table.js b/src/components/Table.js new file mode 100644 index 0000000..034a794 --- /dev/null +++ b/src/components/Table.js @@ -0,0 +1,24 @@ +import React from "react"; + +export const TableListEntry = ({onClick, item, itemKeys}) => { + + return + {itemKeys.map(key => {item[key]})} + ; +}; + +export const Table = ({headerItems, children, items, itemKeys, maxWidth = '30em'}) => { + + let i = 0; + return + + + {headerItems.map(item => )} + + + + {children ? children : items.map(item => )} + +
{item}
; + +}; \ No newline at end of file diff --git a/src/pages/Users.js b/src/pages/Users.js index 29a8138..4d5b3ff 100644 --- a/src/pages/Users.js +++ b/src/pages/Users.js @@ -3,6 +3,7 @@ import { Route, Routes, useNavigate, useParams } from "react-router"; import ErrorBoundary from "../util/ErrorBoundary"; import { get } from '../util/Util'; import '../css/pages/Users.css'; +import { Table, TableListEntry } from "../components/Table"; const Permission = ({name, value}) => { return
  • @@ -59,9 +60,19 @@ const Permissions = ({ perms }) => { ; }; -// TODO: Make generic table list component and use it here and the user list const ApplicationList = ({ apps }) => { + const navigate = useNavigate(); + return + {apps.map(app => { + navigate(`${window.location.pathname}/applications/${app.id}`); + }} + key={app.id} + item={app} + itemKeys={['name', 'id']} + />)} +
    ; }; @@ -80,7 +91,14 @@ const User = ({ user }) => { const response = await get(`/api/users/${user._id}/applications`); if(response.status === 200) updateApps(response.data); })(); - }, [user]); + }, []); + + const ApplicationWrapper = () => { + const { appid } = useParams(); + const app = apps.find(a => a._id === appid); + if (!app) return null; + return ; + }; return
    @@ -106,8 +124,9 @@ const User = ({ user }) => {

    Applications

    } /> + } /> - {apps.map(app => )} + {/* {apps.map(app => )} */}
    @@ -123,37 +142,37 @@ const User = ({ user }) => { ; }; -const UserListEntry = ({ user }) => { +// const UserListEntry = ({ user }) => { - const navigate = useNavigate(); +// const navigate = useNavigate(); - const onClick = () => { - navigate(`/users/${user._id}`); - }; +// const onClick = () => { +// navigate(`/users/${user._id}`); +// }; - return - {user.username} - {user._id} - ; +// return +// {user.username} +// {user._id} +// ; -}; +// }; -// TODO: Make table list generic component -const UserList = ({ users }) => { +// // TODO: Make table list generic component +// const UserList = ({ users }) => { - return - - - - - - - - {users.map(user => )} - -
    UsernameID
    ; +// return +// +// +// +// +// +// +// +// {users.map(user => )} +// +//
    UsernameID
    ; -}; +// }; const Users = () => { @@ -178,9 +197,20 @@ const Users = () => { return ; }; + const navigate = useNavigate(); const UserListWrapper = () => { return
    - + + + {users.map(user => { + navigate(`/users/${user._id}`); + }} + key={user._id} + item={user} + itemKeys={['username', '_id']} />)} +
    +

    Page: {page}

    @@ -188,6 +218,7 @@ const Users = () => { if (users.length === 10) setPage(page + 1); }}>Next
    +
    ; }; From 583e2eb8afe096aa2b3a0e8ed58a45743926b380 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Thu, 24 Nov 2022 00:00:50 +0200 Subject: [PATCH 2/5] close element if clicked outside --- src/components/UserControls.js | 43 ++++++++++++++++++++++++---------- src/util/ClickDetector.js | 35 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 src/util/ClickDetector.js diff --git a/src/components/UserControls.js b/src/components/UserControls.js index 9c4f9eb..67a9903 100644 --- a/src/components/UserControls.js +++ b/src/components/UserControls.js @@ -1,24 +1,41 @@ -import React from "react"; +import React, { useRef } from "react"; import '../css/components/UserControls.css'; import { useLoginContext } from "../structures/UserContext"; +import ClickDetector from "../util/ClickDetector"; +import { clearSession, post } from "../util/Util"; const UserControls = () => { - const [user] = useLoginContext(); + const [user, updateUser] = useLoginContext(); + const detailsRef = useRef(); if (!user) return; - return
    - - Hello {user.displayName} - - -
    -

    Profile

    -
    -

    Logout

    -
    -
    ; + const logOut = async () => { + const response = await post('/api/logout'); + if (response.status === 200) { + clearSession(); + updateUser(); + } + }; + + return { + if (detailsRef.current) detailsRef.current.removeAttribute('open'); + }}> +
    + + Hello {user.displayName} + + + +
    +

    Profile

    +
    +

    Logout

    +
    + +
    +
    ; }; diff --git a/src/util/ClickDetector.js b/src/util/ClickDetector.js new file mode 100644 index 0000000..8f5b351 --- /dev/null +++ b/src/util/ClickDetector.js @@ -0,0 +1,35 @@ +import React, { useEffect, useRef } from "react"; + +const alerter = (ref, callback) => { + useEffect(() => { + + const listener = (event) => { + if (ref.current && !ref.current.contains(event.target)) { + return callback(); + } + }; + + document.addEventListener('mousedown', listener); + + return () => { + document.removeEventListener('mousedown', listener); + }; + + }, [ref]); +}; + +// Component wrapper to enable listening for clicks outside of the given component +const ClickDetector = ({ children, callback }) => { + + const wrapperRef = useRef(null); + alerter(wrapperRef, callback); + + return ( +
    + {children} +
    + ); + +}; + +export default ClickDetector; \ No newline at end of file From cab064a143fa2615c4892ef9359126ea6ef76fac Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Thu, 24 Nov 2022 00:02:01 +0200 Subject: [PATCH 3/5] misc --- src/components/Table.js | 4 +- src/css/index.css | 2 +- src/index.js | 5 +- src/pages/Admin.js | 2 +- src/pages/Empty.js | 2 +- src/pages/Home.js | 8 ++- src/pages/Users.js | 130 +++++++++++++++++++++------------------- src/util/Util.js | 1 - 8 files changed, 85 insertions(+), 69 deletions(-) diff --git a/src/components/Table.js b/src/components/Table.js index ffb57a3..dd9ceea 100644 --- a/src/components/Table.js +++ b/src/components/Table.js @@ -7,10 +7,10 @@ export const TableListEntry = ({onClick, item, itemKeys}) => { ; }; -export const Table = ({headerItems, children, items, itemKeys, maxWidth = '30em'}) => { +export const Table = ({headerItems, children, items, itemKeys}) => { let i = 0; - return + return
    {headerItems.map(item => )} diff --git a/src/css/index.css b/src/css/index.css index 1450bc4..ef0f406 100644 --- a/src/css/index.css +++ b/src/css/index.css @@ -68,7 +68,7 @@ body{ } .pageTitle{ - margin: 0.7em 0 0.7em; + margin: 0; } .flex { diff --git a/src/index.js b/src/index.js index 705503b..1068bdb 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ import React from 'react'; import ReactDOM from 'react-dom/client'; import './css/index.css'; import App from './App'; -import reportWebVitals from './reportWebVitals'; +// import reportWebVitals from './reportWebVitals'; import { UserContext } from './structures/UserContext'; const root = ReactDOM.createRoot(document.getElementById('root')); @@ -11,6 +11,9 @@ root.render( ); +// root.render( +// +// ); // If you want to start measuring performance in your app, pass a function // to log results (for example: reportWebVitals(console.log)) diff --git a/src/pages/Admin.js b/src/pages/Admin.js index b0e3ee1..70f7df8 100644 --- a/src/pages/Admin.js +++ b/src/pages/Admin.js @@ -4,7 +4,7 @@ import '../css/pages/Admin.css'; const Admin = () => { return
    -

    Admin

    +

    Admin

    ; diff --git a/src/pages/Empty.js b/src/pages/Empty.js index 074bf9a..07a37bb 100644 --- a/src/pages/Empty.js +++ b/src/pages/Empty.js @@ -4,7 +4,7 @@ import '../css/pages/Empty.css'; const Empty = () => { return
    -

    Empty

    +

    Empty

    ; diff --git a/src/pages/Home.js b/src/pages/Home.js index 4b8ab9b..6cf75ac 100644 --- a/src/pages/Home.js +++ b/src/pages/Home.js @@ -1,11 +1,17 @@ import React from "react"; import '../css/pages/Home.css'; +import { get } from "../util/Util"; const Home = () => { return
    -

    Home

    +

    Home

    +
    ; }; diff --git a/src/pages/Users.js b/src/pages/Users.js index d8c0f09..f0dfe93 100644 --- a/src/pages/Users.js +++ b/src/pages/Users.js @@ -80,9 +80,18 @@ const ApplicationList = ({ apps }) => { const Application = ({ app }) => { + const descProps = {defaultValue: app.description, placeholder: 'Describe your application'}; + return
    +

    {app.name} ({app.id})

    + + Description + +
    ; }; +// TODO: Groups, description, notes const User = ({ user }) => { const navigate = useNavigate(); @@ -90,14 +99,14 @@ const User = ({ user }) => { useEffect(() => { (async () => { - const response = await get(`/api/users/${user._id}/applications`); + const response = await get(`/api/users/${user.id}/applications`); if(response.status === 200) updateApps(response.data); })(); }, []); const ApplicationWrapper = () => { const { appid } = useParams(); - const app = apps.find(a => a._id === appid); + const app = apps.find(a => a.id === appid); if (!app) return null; return ; }; @@ -110,25 +119,32 @@ const User = ({ user }) => { -

    User {user._id}

    +

    User {user.id}

    + + {user.disabled ? : } + + +

    Created: {new Date(user.createdTimestamp).toDateString()}

    +

    2FA: {user.twoFactor.toString()}

    +

    Disabled: {user.disabled.toString()}

    + - + - + -

    Applications

    +

    Applications

    } /> } /> - {/* {apps.map(app => )} */} @@ -144,37 +160,21 @@ const User = ({ user }) => { ; }; -// const UserListEntry = ({ user }) => { - -// const navigate = useNavigate(); +const CreateUserField = () => { -// const onClick = () => { -// navigate(`/users/${user._id}`); -// }; - -// return
    -// -// -// ; + const [link, setLink] = useState(null); + const getSignupCode = async () => { + const response = await get('/api/signup/code'); + if(response.status === 200) setLink(response.data); + }; -// }; + return
    + {link ? link : } -// // TODO: Make table list generic component -// const UserList = ({ users }) => { - -// return
    {item}
    {user.username}{user._id}
    -// -// -// -// -// -// -// -// {users.map(user => )} -// -//
    UsernameID
    ; + {/* TODO: Create form */} + ; -// }; +}; const Users = () => { @@ -194,47 +194,55 @@ const Users = () => { const UserWrapper = () => { const { id } = useParams(); - const user = users.find(u => u._id === id); + const user = users.find(u => u.id === id); if (!user) return null; return ; }; const navigate = useNavigate(); const UserListWrapper = () => { - return
    - - - {users.map(user => { - navigate(`/users/${user._id}`); - }} - key={user._id} - item={user} - itemKeys={['username', '_id']} />)} -
    + return
    +
    + + {users.map(user => { + navigate(`/users/${user.id}`); + }} + key={user.id} + item={user} + itemKeys={['username', 'id']} />)} +
    -
    - -

    Page: {page}

    - +
    + +

    Page: {page}

    + +
    - + +
    + +
    +
    ; }; return
    -

    Users

    +

    Users

    {error &&

    {error}

    } -
    - - - } /> - } /> - - +
    +
    + + + } /> + } /> + + +
    +
    ; diff --git a/src/util/Util.js b/src/util/Util.js index 5717374..1859373 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -57,7 +57,6 @@ export const post = async (url, body) => { }, body: JSON.stringify(body) }); - console.log(response); return parseResponse(response); }; From 25ed91477d9d3858666350bca0fba9d9b842c5e9 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Thu, 24 Nov 2022 00:04:28 +0200 Subject: [PATCH 4/5] fix funky rendering when logged in from a previous session --- src/App.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/App.js b/src/App.js index 6ac0548..e6d99bc 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,4 @@ -import React, { useEffect } from 'react'; +import React, { useEffect, useState } from 'react'; import { BrowserRouter, Navigate, Route, Routes} from 'react-router-dom'; import './css/App.css'; @@ -16,34 +16,41 @@ import Admin from './pages/Admin'; function App() { - const [user] = useLoginContext(); + const [user, updateUser] = useLoginContext(); + const [loading, setLoading] = useState(true); useEffect(() => { - fetchUser(); + (async () => { + await fetchUser(); + updateUser(); + setLoading(false); + })(); }, []); const menuItems = [ { to: '/home', label: 'Home' }, { to: '/users', label: 'Users' }, { to: '/admin', label: 'Admin' } + ]; -]; + if (loading) return null; return (
    -
    - -
    -
    {user ? - - - +
    +
    + +
    + + + +
    : null}
    From 487ad5e736b084608d9f2b69e1dd401c2795e0c5 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Thu, 24 Nov 2022 00:04:36 +0200 Subject: [PATCH 5/5] update readme --- README.md | 76 +++++-------------------------------------------------- 1 file changed, 7 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 58beeac..0baab08 100644 --- a/README.md +++ b/README.md @@ -1,70 +1,8 @@ -# Getting Started with Create React App +# Navy's webserver framework frontend +A template repository for creating Node.js based webservers with sharding. +Main repository: https://git.corgi.wtf/Navy.gif/webserver-framework-frontend +Backend: https://git.corgi.wtf/Navy.gif/webserver-framework -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). - -## Available Scripts - -In the project directory, you can run: - -### `npm start` - -Runs the app in the development mode.\ -Open [http://localhost:3000](http://localhost:3000) to view it in your browser. - -The page will reload when you make changes.\ -You may also see any lint errors in the console. - -### `npm test` - -Launches the test runner in the interactive watch mode.\ -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. - -### `npm run build` - -Builds the app for production to the `build` folder.\ -It correctly bundles React in production mode and optimizes the build for the best performance. - -The build is minified and the filenames include the hashes.\ -Your app is ready to be deployed! - -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. - -### `npm run eject` - -**Note: this is a one-way operation. Once you `eject`, you can't go back!** - -If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. - -Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. - -You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. - -## Learn More - -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). - -To learn React, check out the [React documentation](https://reactjs.org/). - -### Code Splitting - -This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) - -### Analyzing the Bundle Size - -This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) - -### Making a Progressive Web App - -This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) - -### Advanced Configuration - -This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) - -### Deployment - -This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) - -### `npm run build` fails to minify - -This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) +# Technologies +- React +- React router \ No newline at end of file