import * as icon from '@mdi/js'; import Head from 'next/head' import React from 'react' import axios from 'axios'; import type { ReactElement } from 'react' import LayoutAuthenticated from '../layouts/Authenticated' import SectionMain from '../components/SectionMain' import SectionTitleLineWithButton from '../components/SectionTitleLineWithButton' import BaseIcon from "../components/BaseIcon"; import { getPageTitle } from '../config' import Link from "next/link"; import { hasPermission } from "../helpers/userPermissions"; import { fetchWidgets } from '../stores/roles/rolesSlice'; import { WidgetCreator } from '../components/WidgetCreator/WidgetCreator'; import { SmartWidget } from '../components/SmartWidget/SmartWidget'; import { useAppDispatch, useAppSelector } from '../stores/hooks'; const Dashboard = () => { const dispatch = useAppDispatch(); const iconsColor = useAppSelector((state) => state.style.iconsColor); const corners = useAppSelector((state) => state.style.corners); const cardsStyle = useAppSelector((state) => state.style.cardsStyle); const loadingMessage = 'Loading...'; const [users, setUsers] = React.useState(loadingMessage); const [roles, setRoles] = React.useState(loadingMessage); const [permissions, setPermissions] = React.useState(loadingMessage); const [intersections, setIntersections] = React.useState(loadingMessage); const [traffic_lights, setTraffic_lights] = React.useState(loadingMessage); const [sensors, setSensors] = React.useState(loadingMessage); const [sensor_readings, setSensor_readings] = React.useState(loadingMessage); const [algorithms, setAlgorithms] = React.useState(loadingMessage); const [schedules, setSchedules] = React.useState(loadingMessage); const [incidents, setIncidents] = React.useState(loadingMessage); const [logs, setLogs] = React.useState(loadingMessage); const [widgetsRole, setWidgetsRole] = React.useState({ role: { value: '', label: '' }, }); const { currentUser } = useAppSelector((state) => state.auth); const { isFetchingQuery } = useAppSelector((state) => state.openAi); const { rolesWidgets, loading } = useAppSelector((state) => state.roles); async function loadData() { const entities = ['users','roles','permissions','intersections','traffic_lights','sensors','sensor_readings','algorithms','schedules','incidents','logs',]; const fns = [setUsers,setRoles,setPermissions,setIntersections,setTraffic_lights,setSensors,setSensor_readings,setAlgorithms,setSchedules,setIncidents,setLogs,]; const requests = entities.map((entity, index) => { if(hasPermission(currentUser, `READ_${entity.toUpperCase()}`)) { return axios.get(`/${entity.toLowerCase()}/count`); } else { fns[index](null); return Promise.resolve({data: {count: null}}); } }); Promise.allSettled(requests).then((results) => { results.forEach((result, i) => { if (result.status === 'fulfilled') { fns[i](result.value.data.count); } else { fns[i](result.reason.message); } }); }); } async function getWidgets(roleId) { await dispatch(fetchWidgets(roleId)); } React.useEffect(() => { if (!currentUser) return; loadData().then(); setWidgetsRole({ role: { value: currentUser?.app_role?.id, label: currentUser?.app_role?.name } }); }, [currentUser]); React.useEffect(() => { if (!currentUser || !widgetsRole?.role?.value) return; getWidgets(widgetsRole?.role?.value || '').then(); }, [widgetsRole?.role?.value]); return ( <> {getPageTitle('Overview')} {''} {hasPermission(currentUser, 'CREATE_ROLES') && } {!!rolesWidgets.length && hasPermission(currentUser, 'CREATE_ROLES') && (

{`${widgetsRole?.role?.label || 'Users'}'s widgets`}

)}
{(isFetchingQuery || loading) && (
{' '} Loading widgets...
)} { rolesWidgets && rolesWidgets.map((widget) => ( ))}
{!!rolesWidgets.length &&
}
{hasPermission(currentUser, 'READ_USERS') &&
Users
{users}
} {hasPermission(currentUser, 'READ_ROLES') &&
Roles
{roles}
} {hasPermission(currentUser, 'READ_PERMISSIONS') &&
Permissions
{permissions}
} {hasPermission(currentUser, 'READ_INTERSECTIONS') &&
Intersections
{intersections}
} {hasPermission(currentUser, 'READ_TRAFFIC_LIGHTS') &&
Traffic lights
{traffic_lights}
} {hasPermission(currentUser, 'READ_SENSORS') &&
Sensors
{sensors}
} {hasPermission(currentUser, 'READ_SENSOR_READINGS') &&
Sensor readings
{sensor_readings}
} {hasPermission(currentUser, 'READ_ALGORITHMS') &&
Algorithms
{algorithms}
} {hasPermission(currentUser, 'READ_SCHEDULES') &&
Schedules
{schedules}
} {hasPermission(currentUser, 'READ_INCIDENTS') &&
Incidents
{incidents}
} {hasPermission(currentUser, 'READ_LOGS') &&
Logs
{logs}
}
) } Dashboard.getLayout = function getLayout(page: ReactElement) { return {page} } export default Dashboard