From 72798e44943703f166b369234f2eef569fc8dfac Mon Sep 17 00:00:00 2001 From: Luke Berndt Date: Sat, 17 Jun 2023 09:32:26 -0400 Subject: [PATCH] Add All Systems List --- admin/server/controllers/systems.js | 22 ++++++++++++ admin/server/index.js | 1 + admin/src/AllSystems/AllSystems.js | 53 ++++++++++++++++------------- admin/src/AllSystems/SystemRow.js | 49 ++++++++++++++++++++++++++ admin/src/App/App.js | 3 +- admin/src/features/api/apiSlice.js | 2 +- 6 files changed, 104 insertions(+), 26 deletions(-) create mode 100644 admin/src/AllSystems/SystemRow.js diff --git a/admin/server/controllers/systems.js b/admin/server/controllers/systems.js index 4973fa5..190f1ca 100644 --- a/admin/server/controllers/systems.js +++ b/admin/server/controllers/systems.js @@ -3,6 +3,7 @@ const System = require("../models/system"); const SystemStat = require("../models/system_stat"); const Talkgroup = require("../models/talkgroup"); const Group = require("../models/group"); +const User = require("../models/user"); const crypto = require("crypto"); exports.isLoggedIn = function (req, res, next) { @@ -12,6 +13,27 @@ exports.isLoggedIn = function (req, res, next) { // ------------------------------------------- +exports.listUserSystems = async function (req,res,next) { + if (!req.user.admin) { + res.status(401); + res.json({ success: false, message: "Not Authorized" }); + return; + } + const users = await User.find({}, "_id email firstName lastName lastLogin"); + let list = []; + console.log(users); + for (let index = 0; index < users.length; index++) { + let user = users[index].toObject(); + const userId = new mongoose.Types.ObjectId(user._id); + const systems = await System.find({ userId: userId }, "name shortName description systemType city state county country lastActive"); + + user["systems"] = systems; + list.push(user); + } + res.json(list); + return; +} + exports.listAllSystems = async function (req, res, next) { if (!req.user.admin) { res.status(401); diff --git a/admin/server/index.js b/admin/server/index.js index 5473a93..d6ddb96 100644 --- a/admin/server/index.js +++ b/admin/server/index.js @@ -153,6 +153,7 @@ app.get("/groups/:shortName/:groupId?", isLoggedIn, groups.getGroups); app.delete("/groups/:shortName/:groupId", isLoggedIn, groups.deleteGroup); app.get("/systems", isLoggedIn, systems.listSystems) app.get("/admin/systems", isAdmin, systems.listAllSystems) +app.get("/admin/users", isAdmin, systems.listUserSystems) app.delete("/systems/:shortName", isLoggedIn, systems.deleteSystem) app.post("/systems/:shortName", [isLoggedIn, systems.ownSystem, systems.validateSystem, systems.updateSystem]) app.post("/systems", [isLoggedIn, systems.uniqueShortName, systems.validateSystem, systems.createSystem]) diff --git a/admin/src/AllSystems/AllSystems.js b/admin/src/AllSystems/AllSystems.js index ec5df2a..fe593a2 100644 --- a/admin/src/AllSystems/AllSystems.js +++ b/admin/src/AllSystems/AllSystems.js @@ -1,19 +1,17 @@ import { Container, - Divider, Header, - Table + Table, + Icon } from "semantic-ui-react"; import { useGetAllSystemsQuery, } from '../features/api/apiSlice' +import SystemRow from "./SystemRow"; // ---------------------------------------------------- -const ListSystems = () => { - const { data, isSuccess } = useGetAllSystemsQuery(); - let systems = false; - if (isSuccess) { - systems = data.systems; - } +const AllSystems = () => { + const { data: users, isSuccess } = useGetAllSystemsQuery(); + //https://stackoverflow.com/questions/36559661/how-can-i-dispatch-from-child-components-in-react-redux //https://stackoverflow.com/questions/42597602/react-onclick-pass-event-with-parameter @@ -24,29 +22,36 @@ const ListSystems = () => {
Radio Systems
- {systems && - systems.map((system) => - + {users && + users.map((user) => + + +
+ + + {user.firstName} {user.lastName} + {user.email} + +
+
ShortName Name + Description + Location Last Active - Owner - Email + - - - {system.shortName} - {system.name} - {system.lastActive} - {system.userId.firstName} {system.userId.lastName} - {system.userId.email} - - -
+ {user.systems && + user.systems.map((system) => + + )} + + + )} @@ -55,4 +60,4 @@ const ListSystems = () => { } -export default ListSystems; +export default AllSystems; diff --git a/admin/src/AllSystems/SystemRow.js b/admin/src/AllSystems/SystemRow.js new file mode 100644 index 0000000..edd96c1 --- /dev/null +++ b/admin/src/AllSystems/SystemRow.js @@ -0,0 +1,49 @@ +import { + Container, + Header, + Table, + Icon + } from "semantic-ui-react"; + + + const SystemRow = (props) => { + const { system } = props; + var location = ""; + let lastActive = ""; + if (system.lastActive) { + lastActive = new Date(system.lastActive).toLocaleDateString(); + } + + if (system) { + + + switch (system.systemType) { + case "state": + location = system.state; + break; + case "city": + location = system.city + ", " + system.state; + break; + case "county": + location = system.county + ", " + system.state; + break; + case "international": + location = system.country; + break; + default: + location = "unknown"; + } + } + + return ( + + {system.shortName} + {system.name} + {system.description} + {location} + {lastActive} + + ) + } + + export default SystemRow; \ No newline at end of file diff --git a/admin/src/App/App.js b/admin/src/App/App.js index 3fad512..8d5a75e 100644 --- a/admin/src/App/App.js +++ b/admin/src/App/App.js @@ -5,6 +5,7 @@ import {AdminRestricted, Restricted} from "./Restricted" import About from "../Components/About" import System from "../System/System" +import AllSystems from "../AllSystems/AllSystems" import ListSystems from "../System/ListSystems" import UpdateSystem from "../System/UpdateSystem" import CreateSystem from "../System/CreateSystem" @@ -20,7 +21,7 @@ const App = (params) => { } /> } /> } /> - } /> + } /> } /> (
Miss
)} /> diff --git a/admin/src/features/api/apiSlice.js b/admin/src/features/api/apiSlice.js index 6c11272..3af13dc 100644 --- a/admin/src/features/api/apiSlice.js +++ b/admin/src/features/api/apiSlice.js @@ -138,7 +138,7 @@ export const apiSlice = createApi({ }), getAllSystems: builder.query({ // The URL for the request is '/fakeApi/posts' - query: () => ({ url: `/admin/systems`, credentials: "include" }) + query: () => ({ url: `/admin/users`, credentials: "include" }) }), createSystem: builder.mutation({ query: (system) => ({