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.
refactor into
MedianRow
& PercentTargetMetRow
- Loading branch information
fdelemarre
committed
Jul 2, 2024
1 parent
101d20c
commit 411b40a
Showing
3 changed files
with
135 additions
and
56 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/webapp/components/table/performance-overview-table/MedianRow.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,57 @@ | ||
import React from "react"; | ||
import _ from "../../../../domain/entities/generic/Collection"; | ||
import { TableCell, TableRow } from "@material-ui/core"; | ||
import styled from "styled-components"; | ||
import { PerformanceOverviewTableProps } from "./PerformanceOverviewTable"; | ||
|
||
export type TableColumn = { | ||
value: string; | ||
label: string; | ||
dark?: boolean; | ||
}; | ||
|
||
type MedianRowProps = { | ||
columns: TableColumn[]; | ||
rows: { | ||
[key: TableColumn["value"]]: string; | ||
}[]; | ||
calculateColumns: TableColumn["value"][]; | ||
}; | ||
|
||
export const MedianRow: React.FC<MedianRowProps> = React.memo( | ||
({ rows, columns, calculateColumns }) => { | ||
const calculateMedian = ( | ||
rows: PerformanceOverviewTableProps["rows"], | ||
column: TableColumn["value"] | ||
) => { | ||
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; | ||
}; | ||
|
||
return ( | ||
<TableRow> | ||
{columns.map((column, columnIndex) => ( | ||
<FooterTableCell | ||
key={`median-${column.value}`} | ||
$boldUnderline={columnIndex === 0} | ||
> | ||
{columnIndex === 0 && "Median"} | ||
{calculateColumns.includes(column.value) | ||
? calculateMedian(rows, 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"}; | ||
`; |
63 changes: 63 additions & 0 deletions
63
src/webapp/components/table/performance-overview-table/PercentTargetMetRow.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,63 @@ | ||
import React from "react"; | ||
import _ from "../../../../domain/entities/generic/Collection"; | ||
import { TableCell, TableRow } from "@material-ui/core"; | ||
import styled from "styled-components"; | ||
import { PerformanceOverviewTableProps } from "./PerformanceOverviewTable"; | ||
|
||
export type TableColumn = { | ||
value: string; | ||
label: string; | ||
dark?: boolean; | ||
}; | ||
|
||
type PercentTargetMetRowProps = { | ||
columns: TableColumn[]; | ||
rows: { | ||
[key: TableColumn["value"]]: string; | ||
}[]; | ||
columnRules: { | ||
[key: TableColumn["value"]]: number; | ||
}; | ||
calculateColumns: TableColumn["value"][]; | ||
}; | ||
|
||
export const PercentTargetMetRow: React.FC<PercentTargetMetRowProps> = React.memo( | ||
({ rows, columns, columnRules, calculateColumns }) => { | ||
const calculatePercentTargetMet = ( | ||
rows: PerformanceOverviewTableProps["rows"], | ||
column: TableColumn["value"], | ||
target: number | ||
) => { | ||
const count = rows.filter(row => Number(row[column]) <= target).length; | ||
const percentage = (count / rows.length) * 100 || 0; | ||
return `${percentage.toFixed(0) || 0}%`; | ||
}; | ||
|
||
return ( | ||
<TableRow> | ||
{columns.map((column, columnIndex) => { | ||
const rule = columnRules[column.value] || 7; | ||
|
||
return ( | ||
<FooterTableCell | ||
key={`percent-${column.value}`} | ||
$boldUnderline={columnIndex === 0} | ||
> | ||
{columnIndex === 0 && "% Target Met"} | ||
|
||
{calculateColumns.includes(column.value) | ||
? calculatePercentTargetMet(rows, column.value, rule) | ||
: ""} | ||
</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 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