2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-08-10 22:00:56 +00:00

Improve table rendering

This commit is contained in:
Oliver Walters
2024-11-06 00:21:00 +00:00
parent 15f6930d6b
commit 7ff81c4632
3 changed files with 60 additions and 20 deletions

View File

@@ -1,12 +1,12 @@
import { Badge, Center, MantineSize } from '@mantine/core'; import { Badge, Center, MantineSize } from '@mantine/core';
import { colorMap } from '../../defaults/backendMappings'; import { statusColorMap } from '../../defaults/backendMappings';
import { ModelType } from '../../enums/ModelType'; import { ModelType } from '../../enums/ModelType';
import { resolveItem } from '../../functions/conversion'; import { resolveItem } from '../../functions/conversion';
import { useGlobalStatusState } from '../../states/StatusState'; import { useGlobalStatusState } from '../../states/StatusState';
export interface StatusCodeInterface { export interface StatusCodeInterface {
key: string; key: number;
label: string; label: string;
name: string; name: string;
color: string; color: string;
@@ -54,7 +54,7 @@ function renderStatusLabel(
// Fallbacks // Fallbacks
if (color == null) color = 'default'; if (color == null) color = 'default';
color = colorMap[color] || colorMap['default']; color = statusColorMap[color] || statusColorMap['default'];
const size = options.size || 'xs'; const size = options.size || 'xs';
if (!text) { if (!text) {

View File

@@ -20,7 +20,7 @@ export const statusCodeList: Record<string, ModelType> = {
/* /*
* Map the colors used in the backend to the colors used in the frontend * Map the colors used in the backend to the colors used in the frontend
*/ */
export const colorMap: { [key: string]: string } = { export const statusColorMap: { [key: string]: string } = {
dark: 'dark', dark: 'dark',
warning: 'yellow', warning: 'yellow',
success: 'green', success: 'green',

View File

@@ -1,7 +1,13 @@
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { Badge, Group, Text } from '@mantine/core';
import { useCallback, useMemo, useState } from 'react'; import { useCallback, useMemo, useState } from 'react';
import { AddItemButton } from '../../components/buttons/AddItemButton'; import { AddItemButton } from '../../components/buttons/AddItemButton';
import {
StatusCodeInterface,
StatusCodeListInterface
} from '../../components/render/StatusRenderer';
import { statusColorMap } from '../../defaults/backendMappings';
import { ApiEndpoints } from '../../enums/ApiEndpoints'; import { ApiEndpoints } from '../../enums/ApiEndpoints';
import { UserRoles } from '../../enums/Roles'; import { UserRoles } from '../../enums/Roles';
import { useCustomStateFields } from '../../forms/CommonForms'; import { useCustomStateFields } from '../../forms/CommonForms';
@@ -12,6 +18,7 @@ import {
} from '../../hooks/UseForm'; } from '../../hooks/UseForm';
import { useTable } from '../../hooks/UseTable'; import { useTable } from '../../hooks/UseTable';
import { apiUrl } from '../../states/ApiState'; import { apiUrl } from '../../states/ApiState';
import { useGlobalStatusState } from '../../states/StatusState';
import { useUserState } from '../../states/UserState'; import { useUserState } from '../../states/UserState';
import { TableColumn } from '../Column'; import { TableColumn } from '../Column';
import { InvenTreeTable } from '../InvenTreeTable'; import { InvenTreeTable } from '../InvenTreeTable';
@@ -23,12 +30,48 @@ import { RowAction, RowDeleteAction, RowEditAction } from '../RowActions';
export default function CustomStateTable() { export default function CustomStateTable() {
const table = useTable('customstates'); const table = useTable('customstates');
const statusCodes = useGlobalStatusState();
// Find the associated logical state key
const getLogicalState = useCallback(
(group: string, key: number) => {
const valuesList = Object.values(statusCodes.status ?? {}).find(
(value: StatusCodeListInterface) => value.statusClass === group
);
const value = Object.values(valuesList?.values ?? {}).find(
(value: StatusCodeInterface) => value.key === key
);
return value?.label ?? '';
},
[statusCodes]
);
const user = useUserState(); const user = useUserState();
const columns: TableColumn[] = useMemo(() => { const columns: TableColumn[] = useMemo(() => {
return [ return [
{
accessor: 'reference_status',
title: t`Status`,
sortable: true
},
{
accessor: 'logical_key',
title: t`Logical State`,
sortable: true,
render: (record: any) => {
let stateText = getLogicalState(
record.reference_status,
record.logical_key
);
return stateText ? stateText : record.logical_key;
}
},
{ {
accessor: 'name', accessor: 'name',
title: t`Identifier`,
sortable: true sortable: true
}, },
{ {
@@ -36,29 +79,26 @@ export default function CustomStateTable() {
title: t`Display Name`, title: t`Display Name`,
sortable: true sortable: true
}, },
{
accessor: 'color'
},
{ {
accessor: 'key', accessor: 'key',
sortable: true sortable: true
}, },
{ {
accessor: 'logical_key', accessor: 'color',
sortable: true render: (record: any) => {
}, return (
{ <Badge
accessor: 'model_name', color={statusColorMap[record.color] || statusColorMap['default']}
title: t`Model`, variant="filled"
sortable: true size="xs"
}, >
{ {record.color}
accessor: 'reference_status', </Badge>
title: t`Status`, );
sortable: true }
} }
]; ];
}, []); }, [getLogicalState]);
const newCustomStateFields = useCustomStateFields(); const newCustomStateFields = useCustomStateFields();
const editCustomStateFields = useCustomStateFields(); const editCustomStateFields = useCustomStateFields();