From 2d662fb9ccb4a689d5652b4573ccf88684bd6501 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Mon, 14 Apr 2025 12:17:14 +0000 Subject: [PATCH] 1.3 --- .gitignore | 7 + frontend/json/runtimeError.json | 1 + frontend/src/pages/dashboard.tsx | 60 +-- frontend/src/pages/dashboard.tsx.temp | 548 ++++++++++++++++++++++++++ 4 files changed, 557 insertions(+), 59 deletions(-) create mode 100644 .gitignore create mode 100644 frontend/json/runtimeError.json create mode 100644 frontend/src/pages/dashboard.tsx.temp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..339a829 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +node_modules/ +*/node_modules/ +**/node_modules/ +*/build/ +**/build/ +.DS_Store +.env \ No newline at end of file diff --git a/frontend/json/runtimeError.json b/frontend/json/runtimeError.json new file mode 100644 index 0000000..b349867 --- /dev/null +++ b/frontend/json/runtimeError.json @@ -0,0 +1 @@ +{"message":"setWidgetsRole is not defined","stack":"\n at Dashboard (webpack-internal:///./src/pages/dashboard.tsx:36:84)\n at ErrorBoundary (webpack-internal:///./src/components/ErrorBoundary.tsx:233:9)\n at div ()\n at div ()\n at LayoutAuthenticated (webpack-internal:///./src/layouts/Authenticated.tsx:44:11)\n at Provider (webpack-internal:///./node_modules/react-redux/es/components/Provider.js:13:3)\n at MyApp (webpack-internal:///./src/pages/_app.tsx:36:11)\n at PathnameContextProviderAdapter (webpack-internal:///./node_modules/next/dist/shared/lib/router/adapters.js:81:11)\n at ErrorBoundary (webpack-internal:///./node_modules/next/dist/client/components/react-dev-overlay/pages/ErrorBoundary.js:41:9)\n at ReactDevOverlay (webpack-internal:///./node_modules/next/dist/client/components/react-dev-overlay/pages/ReactDevOverlay.js:33:11)\n at Container (webpack-internal:///./node_modules/next/dist/client/index.js:81:1)\n at AppContainer (webpack-internal:///./node_modules/next/dist/client/index.js:189:11)\n at Root (webpack-internal:///./node_modules/next/dist/client/index.js:413:11)"} \ No newline at end of file diff --git a/frontend/src/pages/dashboard.tsx b/frontend/src/pages/dashboard.tsx index 908e52c..3308afc 100644 --- a/frontend/src/pages/dashboard.tsx +++ b/frontend/src/pages/dashboard.tsx @@ -13,7 +13,6 @@ 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 = () => { @@ -36,9 +35,6 @@ const Dashboard = () => { const [staff, setStaff] = React.useState('Loading...'); const [clients, setClients] = React.useState('Loading...'); - const [widgetsRole, setWidgetsRole] = React.useState({ - role: { value: '', label: '' }, - }); const { currentUser } = useAppSelector((state) => state.auth); const { isFetchingQuery } = useAppSelector((state) => state.openAi); @@ -98,24 +94,12 @@ const Dashboard = () => { }); } - 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 ( <> @@ -131,52 +115,10 @@ const Dashboard = () => { {''} - {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 &&
}
{ + 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 [users, setUsers] = React.useState('Loading...'); + const [employees, setEmployees] = React.useState('Loading...'); + const [inventory, setInventory] = React.useState('Loading...'); + const [machinery, setMachinery] = React.useState('Loading...'); + const [quality_controls, setQuality_controls] = React.useState('Loading...'); + const [raw_materials, setRaw_materials] = React.useState('Loading...'); + const [suppliers, setSuppliers] = React.useState('Loading...'); + const [work_orders, setWork_orders] = React.useState('Loading...'); + const [roles, setRoles] = React.useState('Loading...'); + const [permissions, setPermissions] = React.useState('Loading...'); + const [companies, setCompanies] = React.useState('Loading...'); + const [staff, setStaff] = React.useState('Loading...'); + const [clients, setClients] = React.useState('Loading...'); + + const { currentUser } = useAppSelector((state) => state.auth); + const { isFetchingQuery } = useAppSelector((state) => state.openAi); + + const { rolesWidgets, loading } = useAppSelector((state) => state.roles); + + const organizationId = currentUser?.companies?.id; + + async function loadData() { + const entities = [ + 'users', + 'employees', + 'inventory', + 'machinery', + 'quality_controls', + 'raw_materials', + 'suppliers', + 'work_orders', + 'roles', + 'permissions', + 'companies', + 'staff', + 'clients', + ]; + const fns = [ + setUsers, + setEmployees, + setInventory, + setMachinery, + setQuality_controls, + setRaw_materials, + setSuppliers, + setWork_orders, + setRoles, + setPermissions, + setCompanies, + setStaff, + setClients, + ]; + + 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); + } + }); + }); + } + + React.useEffect(() => { + if (!currentUser) return; + loadData().then(); + + {getPageTitle('Dashboard')} + + + + {''} + + + +
+
+ + +
+ {hasPermission(currentUser, 'READ_USERS') && ( + +
+
+
+
+ Users +
+
+ {users} +
+
+
+ +
+
+
+ + )} + + {hasPermission(currentUser, 'READ_EMPLOYEES') && ( + +
+
+
+
+ Employees +
+
+ {employees} +
+
+
+ +
+
+
+ + )} + + {hasPermission(currentUser, 'READ_INVENTORY') && ( + +
+
+
+
+ Inventory +
+
+ {inventory} +
+
+
+ +
+
+
+ + )} + + {hasPermission(currentUser, 'READ_MACHINERY') && ( + +
+
+
+
+ Machinery +
+
+ {machinery} +
+
+
+ +
+
+
+ + )} + + {hasPermission(currentUser, 'READ_QUALITY_CONTROLS') && ( + +
+
+
+
+ Quality controls +
+
+ {quality_controls} +
+
+
+ +
+
+
+ + )} + + {hasPermission(currentUser, 'READ_RAW_MATERIALS') && ( + +
+
+
+
+ Raw materials +
+
+ {raw_materials} +
+
+
+ +
+
+
+ + )} + + {hasPermission(currentUser, 'READ_SUPPLIERS') && ( + +
+
+
+
+ Suppliers +
+
+ {suppliers} +
+
+
+ +
+
+
+ + )} + + {hasPermission(currentUser, 'READ_WORK_ORDERS') && ( + +
+
+
+
+ Work orders +
+
+ {work_orders} +
+
+
+ +
+
+
+ + )} + + {hasPermission(currentUser, 'READ_ROLES') && ( + +
+
+
+
+ Roles +
+
+ {roles} +
+
+
+ +
+
+
+ + )} + + {hasPermission(currentUser, 'READ_PERMISSIONS') && ( + +
+
+
+
+ Permissions +
+
+ {permissions} +
+
+
+ +
+
+
+ + )} + + {hasPermission(currentUser, 'READ_COMPANIES') && ( + +
+
+
+
+ Companies +
+
+ {companies} +
+
+
+ +
+
+
+ + )} + + {hasPermission(currentUser, 'READ_STAFF') && ( + +
+
+
+
+ Staff +
+
+ {staff} +
+
+
+ +
+
+
+ + )} + + {hasPermission(currentUser, 'READ_CLIENTS') && ( + +
+
+
+
+ Clients +
+
+ {clients} +
+
+
+ +
+
+
+ + )} +
+
+ + ); +}; + +Dashboard.getLayout = function getLayout(page: ReactElement) { + return {page}; +}; + +export default Dashboard;