diff --git a/src/pages/AdminPanel/ClusterController.jsx b/src/pages/AdminPanel/ClusterController.jsx index b442ad0..a526197 100644 --- a/src/pages/AdminPanel/ClusterController.jsx +++ b/src/pages/AdminPanel/ClusterController.jsx @@ -9,6 +9,7 @@ import { makeStringSorter } from '../../components/Table' import { AdminClusterService, AdminDepositService } from '../../services/api' +import { arrayOrDefault } from '../../utils' export default function ClusterController() { const [deposits, setDeposits] = useState([]) @@ -33,7 +34,7 @@ export default function ClusterController() { const updateTable = () => invokeWebApiWrapperAsync( async () => { const clusters = await AdminClusterService.getAll() - setClusters(clusters) + setClusters(arrayOrDefault(clusters)) }, setShowLoader, `Не удалось загрузить список кустов` @@ -41,9 +42,9 @@ export default function ClusterController() { useEffect(() => invokeWebApiWrapperAsync( async () => { - let deposits = await AdminDepositService.getAll() - deposits = deposits?.map((deposit) => ({ value: deposit.id, label: deposit.caption })) - setDeposits(deposits ?? []) + let deposits = arrayOrDefault(await AdminDepositService.getAll()) + deposits = deposits.map((deposit) => ({ value: deposit.id, label: deposit.caption })) + setDeposits(deposits) }, setShowLoader, `Не удалось загрузить список месторождений` diff --git a/src/pages/AdminPanel/CompanyController.jsx b/src/pages/AdminPanel/CompanyController.jsx index 58a4bf0..8faf0cd 100644 --- a/src/pages/AdminPanel/CompanyController.jsx +++ b/src/pages/AdminPanel/CompanyController.jsx @@ -3,6 +3,7 @@ import { invokeWebApiWrapperAsync } from '../../components/factory' import LoaderPortal from '../../components/LoaderPortal' import { EditableTable, makeColumn, makeActionHandler, makeStringSorter } from '../../components/Table' import { AdminCompanyService } from '../../services/api' +import { arrayOrDefault } from '../../utils' const companyColumns = [ makeColumn('Название', 'caption', { @@ -24,7 +25,7 @@ export default function CompanyController() { const updateTable = () => invokeWebApiWrapperAsync( async() => { const companies = await AdminCompanyService.getAll() - setCompanies(companies) + setCompanies(arrayOrDefault(companies)) }, setShowLoader, `Не удалось загрузить список кустов` diff --git a/src/pages/AdminPanel/DepositController.jsx b/src/pages/AdminPanel/DepositController.jsx index 8e537bb..35244e7 100644 --- a/src/pages/AdminPanel/DepositController.jsx +++ b/src/pages/AdminPanel/DepositController.jsx @@ -3,6 +3,7 @@ import { invokeWebApiWrapperAsync } from '../../components/factory' import LoaderPortal from '../../components/LoaderPortal' import { EditableTable, makeColumn, makeActionHandler } from '../../components/Table' import { AdminDepositService } from '../../services/api' +import { arrayOrDefault } from '../../utils' const depositColumns = [ makeColumn('Название', 'caption', { width: 200, editable: true }), @@ -17,7 +18,7 @@ export default function DepositController() { const updateTable = () => invokeWebApiWrapperAsync( async() => { const deposits = await AdminDepositService.getAll() - setDeposits(deposits) + setDeposits(arrayOrDefault(deposits)) }, setShowLoader, `Не удалось загрузить список месторождении` diff --git a/src/pages/AdminPanel/PermissionController.jsx b/src/pages/AdminPanel/PermissionController.jsx index 8390a73..6f87cf8 100644 --- a/src/pages/AdminPanel/PermissionController.jsx +++ b/src/pages/AdminPanel/PermissionController.jsx @@ -8,6 +8,7 @@ import { makeStringSorter } from '../../components/Table' import { AdminPermissionService } from '../../services/api' +import { arrayOrDefault } from '../../utils' const columns = [ makeColumn('Название', 'name', { @@ -27,7 +28,7 @@ export const PermissionController = () => { const updateTable = async () => invokeWebApiWrapperAsync( async () => { const permission = await AdminPermissionService.getAll() - setPermissions(permission ?? []) + setPermissions(arrayOrDefault(permission)) }, setShowLoader, `Не удалось загрузить список прав` diff --git a/src/pages/AdminPanel/UserController.jsx b/src/pages/AdminPanel/UserController.jsx index 98cafd9..2ef4756 100644 --- a/src/pages/AdminPanel/UserController.jsx +++ b/src/pages/AdminPanel/UserController.jsx @@ -4,6 +4,7 @@ import LoaderPortal from '../../components/LoaderPortal' import { EditableTable, makeColumn, makeSelectColumn, makeActionHandler, makeStringSorter, makeNumericSorter } from '../../components/Table' import { AdminCompanyService, AdminUserService } from '../../services/api' import { createLoginRules, nameRules, phoneRules, emailRules } from '../../utils/validationRules' +import { arrayOrDefault } from '../../utils' export default function UserController() { const [companies, setCompanies] = useState([]) @@ -54,7 +55,7 @@ export default function UserController() { const updateTable = () => invokeWebApiWrapperAsync( async() => { const users = await AdminUserService.getAll() - setUsers(users) + setUsers(arrayOrDefault(users)) }, setShowLoader, `Не удалось загрузить список пользователей` @@ -62,9 +63,9 @@ export default function UserController() { useEffect(() => invokeWebApiWrapperAsync( async () => { - let companies = await AdminCompanyService.getAll() + let companies = arrayOrDefault(await AdminCompanyService.getAll()) companies = companies?.map((company) => ({ value: company.id, label: company.caption })) - setCompanies(companies ?? []) + setCompanies(companies) }, setShowLoader, `Не удалось загрузить список компаний` diff --git a/src/pages/AdminPanel/VisitLog.jsx b/src/pages/AdminPanel/VisitLog.jsx index 40b6512..54cf066 100644 --- a/src/pages/AdminPanel/VisitLog.jsx +++ b/src/pages/AdminPanel/VisitLog.jsx @@ -4,6 +4,7 @@ import { invokeWebApiWrapperAsync } from '../../components/factory' import LoaderPortal from '../../components/LoaderPortal' import { makeColumn, makeDateSorter, makeStringSorter, Table } from '../../components/Table' import { RequerstTrackerService } from '../../services/api' +import { arrayOrDefault } from '../../utils' const columns = [ makeColumn('Логин', 'login', { sorter: makeStringSorter('login') }), @@ -20,7 +21,7 @@ export const VisitLog = () => { useEffect(() => invokeWebApiWrapperAsync( async () => { - const logData = await RequerstTrackerService.getUsersStat(1000) + const logData = arrayOrDefault(await RequerstTrackerService.getUsersStat(1000)) logData.forEach((log) => log.key = `${log.login}${log.ip}`) setLogData(logData) }, diff --git a/src/utils/index.ts b/src/utils/index.ts index 252ffa6..b122442 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -7,3 +7,5 @@ export const mainFrameSize = () => ({ width: window.innerWidth, height: window.innerHeight - headerHeight }) + +export const arrayOrDefault = (arr?: unknown, def: T[] = []): T[] => Array.isArray(arr) ? arr : def