Skip to content

Commit

Permalink
[PUI] Tweaks (#7180)
Browse files Browse the repository at this point in the history
* Table: Add loading state

* Update BOM pricing panel

* Fix for TableStringValue

- Order of operations
  • Loading branch information
SchrodingersGat authored May 8, 2024
1 parent fc9c75e commit c72dc2b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/frontend/src/components/details/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ function NameBadge({ pk, type }: { pk: string | number; type: BadgeType }) {
function TableStringValue(props: Readonly<FieldProps>) {
let value = props?.field_value;

if (value === undefined) {
return '---';
}

if (props.field_data?.value_formatter) {
value = props.field_data.value_formatter();
}

if (value === undefined) {
return '---';
}

if (props.field_data?.badge) {
return <NameBadge pk={value} type={props.field_data.badge} />;
}
Expand Down
6 changes: 6 additions & 0 deletions src/frontend/src/hooks/UseTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export type TableState = {
tableKey: string;
refreshTable: () => void;
activeFilters: TableFilter[];
isLoading: boolean;
setIsLoading: (value: boolean) => void;
setActiveFilters: (filters: TableFilter[]) => void;
clearActiveFilters: () => void;
expandedRecords: any[];
Expand Down Expand Up @@ -120,9 +122,13 @@ export function useTable(tableName: string): TableState {
[records]
);

const [isLoading, setIsLoading] = useState<boolean>(false);

return {
tableKey,
refreshTable,
isLoading,
setIsLoading,
activeFilters,
setActiveFilters,
clearActiveFilters,
Expand Down
54 changes: 33 additions & 21 deletions src/frontend/src/pages/part/pricing/BomPricingPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { apiUrl } from '../../../states/ApiState';
import { TableColumn } from '../../../tables/Column';
import { DateColumn, PartColumn } from '../../../tables/ColumnRenderers';
import { InvenTreeTable } from '../../../tables/InvenTreeTable';
import { NoPricingData } from './PricingPanel';
import { LoadingPricingData, NoPricingData } from './PricingPanel';

// Display BOM data as a pie chart
function BomPieChart({
Expand Down Expand Up @@ -209,6 +209,10 @@ export default function BomPricingPanel({

const [chartType, setChartType] = useState<string>('pie');

const hasData: boolean = useMemo(() => {
return !table.isLoading && bomPricingData.length > 0;
}, [table.isLoading, bomPricingData.length]);

return (
<Stack spacing="xs">
<SimpleGrid cols={2}>
Expand All @@ -227,26 +231,34 @@ export default function BomPricingPanel({
modelField: 'sub_part'
}}
/>
{bomPricingData.length > 0 ? (
<Stack spacing="xs">
{chartType == 'bar' && (
<BomBarChart data={bomPricingData} currency={pricing?.currency} />
)}
{chartType == 'pie' && (
<BomPieChart data={bomPricingData} currency={pricing?.currency} />
)}
<SegmentedControl
value={chartType}
onChange={setChartType}
data={[
{ value: 'pie', label: t`Pie Chart` },
{ value: 'bar', label: t`Bar Chart` }
]}
/>
</Stack>
) : (
<NoPricingData />
)}
<Stack spacing="xs">
{table.isLoading && <LoadingPricingData />}
{hasData && (
<Stack spacing="xs">
{chartType == 'bar' && (
<BomBarChart
data={bomPricingData}
currency={pricing?.currency}
/>
)}
{chartType == 'pie' && (
<BomPieChart
data={bomPricingData}
currency={pricing?.currency}
/>
)}
<SegmentedControl
value={chartType}
onChange={setChartType}
data={[
{ value: 'pie', label: t`Pie Chart` },
{ value: 'bar', label: t`Bar Chart` }
]}
/>
</Stack>
)}
{!hasData && !table.isLoading && <NoPricingData />}
</Stack>
</SimpleGrid>
</Stack>
);
Expand Down
13 changes: 13 additions & 0 deletions src/frontend/src/pages/part/pricing/PricingPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
AccordionControlProps,
Alert,
Box,
Center,
Loader,
Space,
Stack,
Text,
Expand Down Expand Up @@ -68,3 +70,14 @@ export function NoPricingData() {
</Stack>
);
}

export function LoadingPricingData() {
return (
<Center>
<Stack spacing="xs">
<Text>{t`Loading pricing data`}</Text>
<Loader />
</Stack>
</Center>
);
}
4 changes: 4 additions & 0 deletions src/frontend/src/tables/InvenTreeTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ export function InvenTreeTable<T = any>({
refetchOnMount: true
});

useEffect(() => {
tableState.setIsLoading(isFetching);
}, [isFetching]);

// Update tableState.records when new data received
useEffect(() => {
tableState.setRecords(data ?? []);
Expand Down

0 comments on commit c72dc2b

Please sign in to comment.