generated from EyeSeeTea/dhis2-app-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move calculation logic in
useStatisticCalculations
hook and refacto…
…r into a generic `CalculationRow` component
- Loading branch information
fdelemarre
committed
Jul 10, 2024
1 parent
7558e1a
commit 16b3247
Showing
7 changed files
with
99 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/webapp/components/table/statistic-table/CalculationRow.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from "react"; | ||
import { TableCell, TableRow } from "@material-ui/core"; | ||
import styled from "styled-components"; | ||
import { TableColumn } from "./StatisticTable"; | ||
|
||
type CalculationRowProps = { | ||
columns: TableColumn[]; | ||
calculateColumns: string[]; | ||
label: string; | ||
calculate: (column: string) => string | number; | ||
}; | ||
|
||
export const CalculationRow: React.FC<CalculationRowProps> = React.memo( | ||
({ columns, calculateColumns, label, calculate }) => { | ||
const [labelColumn, ...otherColumns] = columns; | ||
return ( | ||
<TableRow> | ||
<FooterTableCell key={`${label}-${labelColumn?.value}`} $boldUnderline> | ||
{label} | ||
</FooterTableCell> | ||
{otherColumns.map(column => ( | ||
<FooterTableCell key={`${label}-${column.value}`}> | ||
{calculateColumns.includes(column.value) ? calculate(column.value) : ""} | ||
</FooterTableCell> | ||
))} | ||
</TableRow> | ||
); | ||
} | ||
); | ||
|
||
const FooterTableCell = styled(TableCell)<{ $boldUnderline?: boolean }>` | ||
background-color: ${props => props.theme.palette.common.greyLight}; | ||
text-decoration: ${props => props.$boldUnderline && "underline"}; | ||
font-weight: ${props => (props.$boldUnderline || !!props.color) && "600"}; | ||
`; |
This file was deleted.
Oops, something went wrong.
63 changes: 0 additions & 63 deletions
63
src/webapp/components/table/statistic-table/PercentTargetMetRow.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useCallback } from "react"; | ||
import { StatisticTableProps } from "../components/table/statistic-table/StatisticTable"; | ||
|
||
export const useStatisticCalculations = ( | ||
rows: StatisticTableProps["rows"], | ||
columnRules: { [key: string]: number } | ||
) => { | ||
const calculateMedian = useCallback( | ||
(column: string) => { | ||
const values = rows.map(row => Number(row[column])).filter(value => !isNaN(value)); | ||
values.sort((a, b) => a - b); | ||
const mid = Math.floor(values.length / 2); | ||
return ( | ||
(values.length % 2 !== 0 | ||
? values[mid] | ||
: ((values[mid - 1] || 0) + (values[mid] || 0)) / 2) || 0 | ||
); | ||
}, | ||
[rows] | ||
); | ||
|
||
const calculatePercentTargetMet = useCallback( | ||
(column: string) => { | ||
const target = columnRules[column] || 7; | ||
const count = rows.filter(row => Number(row[column]) <= target).length; | ||
const percentage = (count / rows.length) * 100 || 0; | ||
return `${percentage.toFixed(0) || 0}%`; | ||
}, | ||
[rows, columnRules] | ||
); | ||
|
||
return { | ||
calculateMedian, | ||
calculatePercentTargetMet, | ||
}; | ||
}; |