diff --git a/frontend/src/pages/dashboard.tsx b/frontend/src/pages/dashboard.tsx index f5b4bdc..680b776 100644 --- a/frontend/src/pages/dashboard.tsx +++ b/frontend/src/pages/dashboard.tsx @@ -1,373 +1,155 @@ -import * as icon from '@mdi/js'; +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 React, { ReactElement } from 'react' +import axios from 'axios' import LayoutAuthenticated from '../layouts/Authenticated' import SectionMain from '../components/SectionMain' import SectionTitleLineWithButton from '../components/SectionTitleLineWithButton' -import BaseIcon from "../components/BaseIcon"; +import BaseIcon from '../components/BaseIcon' import { getPageTitle } from '../config' -import Link from "next/link"; +import Link from 'next/link' +import { useAppSelector } from '../stores/hooks' +import { hasPermission } from '../helpers/userPermissions' -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 iconsColor = useAppSelector((state) => state.style.iconsColor) + const corners = useAppSelector((state) => state.style.corners) + const cardsStyle = useAppSelector((state) => state.style.cardsStyle) + const { currentUser } = useAppSelector((state) => state.auth) - const loadingMessage = 'Loading...'; + const loadingMessage = 'Loading...' - - const [users, setUsers] = React.useState(loadingMessage); - const [roles, setRoles] = React.useState(loadingMessage); - const [permissions, setPermissions] = React.useState(loadingMessage); - const [leads, setLeads] = React.useState(loadingMessage); - const [projects, setProjects] = React.useState(loadingMessage); - const [tasks, setTasks] = React.useState(loadingMessage); - const [comments, setComments] = React.useState(loadingMessage); - const [attachments, setAttachments] = React.useState(loadingMessage); + const [leads, setLeads] = React.useState(loadingMessage) + const [projects, setProjects] = React.useState(loadingMessage) + const [tasks, setTasks] = 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','leads','projects','tasks','comments','attachments',]; - const fns = [setUsers,setRoles,setPermissions,setLeads,setProjects,setTasks,setComments,setAttachments,]; + async function loadData() { + const entities = ['leads', 'projects', 'tasks'] + const fns = [setLeads, setProjects, setTasks] - 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}}); - } - - }); + 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]); + 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) + } + }) + }) + } + + React.useEffect(() => { + if (!currentUser) return + loadData().then() + }, [currentUser]) - React.useEffect(() => { - if (!currentUser || !widgetsRole?.role?.value) return; - getWidgets(widgetsRole?.role?.value || '').then(); - }, [widgetsRole?.role?.value]); - return ( <> - - {getPageTitle('Overview')} - + {getPageTitle('Dashboard')} + icon={icon.mdiChartTimelineVariant} + title="TeamFlow PM Dashboard" + main + > {''} - - {hasPermission(currentUser, 'CREATE_ROLES') && } - {!!rolesWidgets.length && - hasPermission(currentUser, 'CREATE_ROLES') && ( -

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

- )} -
- {(isFetchingQuery || loading) && ( -
+
+ {hasPermission(currentUser, 'READ_LEADS') && ( + +
+
+
+
+ Potential Leads +
+
{leads}
+
+
{' '} - Loading widgets... -
- )} - - { rolesWidgets && - rolesWidgets.map((widget) => ( - - ))} -
+
+
+
+ + )} - {!!rolesWidgets.length &&
} - -
- - - {hasPermission(currentUser, 'READ_USERS') && -
-
-
-
- Users -
-
- {users} -
-
-
- -
+ {hasPermission(currentUser, 'READ_PROJECTS') && ( + +
+
+
+
+ Active Projects
+
{projects}
+
+
+ +
- } - - {hasPermission(currentUser, 'READ_ROLES') && -
-
-
-
- Roles -
-
- {roles} -
-
-
- -
+
+ + )} + + {hasPermission(currentUser, 'READ_TASKS') && ( + +
+
+
+
+ Open Tasks
+
{tasks}
+
+
+ +
- } - - {hasPermission(currentUser, 'READ_PERMISSIONS') && -
-
-
-
- Permissions -
-
- {permissions} -
-
-
- -
-
-
- } - - {hasPermission(currentUser, 'READ_LEADS') && -
-
-
-
- Leads -
-
- {leads} -
-
-
- -
-
-
- } - - {hasPermission(currentUser, 'READ_PROJECTS') && -
-
-
-
- Projects -
-
- {projects} -
-
-
- -
-
-
- } - - {hasPermission(currentUser, 'READ_TASKS') && -
-
-
-
- Tasks -
-
- {tasks} -
-
-
- -
-
-
- } - - {hasPermission(currentUser, 'READ_COMMENTS') && -
-
-
-
- Comments -
-
- {comments} -
-
-
- -
-
-
- } - - {hasPermission(currentUser, 'READ_ATTACHMENTS') && -
-
-
-
- Attachments -
-
- {attachments} -
-
-
- -
-
-
- } - - +
+ + )}
@@ -379,3 +161,4 @@ Dashboard.getLayout = function getLayout(page: ReactElement) { } export default Dashboard +