From 0ac5b46349ba508070401c2015d550992e2ae788 Mon Sep 17 00:00:00 2001 From: Steffen Hageland Date: Tue, 17 Sep 2024 12:29:40 +0200 Subject: [PATCH] Special sort for some columns (#251) --- frontend/beCompliant/src/components/Table.tsx | 11 ++--- .../src/components/table/TableSort.ts | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 frontend/beCompliant/src/components/table/TableSort.ts diff --git a/frontend/beCompliant/src/components/Table.tsx b/frontend/beCompliant/src/components/Table.tsx index 707123fa2..d4cbf3f60 100644 --- a/frontend/beCompliant/src/components/Table.tsx +++ b/frontend/beCompliant/src/components/Table.tsx @@ -17,6 +17,7 @@ import { DataTableCell } from './table/DataTableCell'; import { DataTableHeader } from './table/DataTableHeader'; import { TableCell } from './table/TableCell'; import { OptionalField, Question, Table } from '../api/types'; +import { getSortFuncForColumn } from './table/TableSort'; type Props = { data: Question[]; @@ -72,18 +73,14 @@ export function TableComponent({ data, tableData }: Props) { ( a.getValue(columnId) as OptionalField | null )?.value?.[0]?.toLowerCase() || ''; + const valueB = ( b.getValue(columnId) as OptionalField | null )?.value?.[0]?.toLowerCase() || ''; - if (valueA < valueB) { - return -1; - } - if (valueA > valueB) { - return 1; - } - return 0; + const sortFunc = getSortFuncForColumn(columnId); + return sortFunc(valueA, valueB); }, }) ); diff --git a/frontend/beCompliant/src/components/table/TableSort.ts b/frontend/beCompliant/src/components/table/TableSort.ts new file mode 100644 index 000000000..02ef42f24 --- /dev/null +++ b/frontend/beCompliant/src/components/table/TableSort.ts @@ -0,0 +1,42 @@ +type SortFunc = (a: string, b: string) => number; + +export const standardColumnSort: SortFunc = (a, b) => { + return a.localeCompare(b); +}; + +const customSort = (a: string, b: string, priorityOrder: string[]): number => { + const priValueA = priorityOrder.indexOf(a); + const priValueB = priorityOrder.indexOf(b); + + // If for some reason both values are not in the priority order list, lets use standard-sort + if (priValueA < 0 && priValueB < 0) { + return standardColumnSort(a, b); + } + + return priValueA - priValueB; +}; + +export const prirorityColumnSort: SortFunc = (a, b) => { + const priorityOrder = ['kan', 'bør', '(må)', 'må']; + return customSort(a, b, priorityOrder); +}; + +export const ledetidColumnSort: SortFunc = (a, b) => { + const priorityOrder = [ + 'kort (< 1 mnd)', + 'middels (1-3 mnd)', + 'lang (> 3 mnd)', + ]; + return customSort(a, b, priorityOrder); +}; + +export const getSortFuncForColumn = (columnId: string): SortFunc => { + switch (columnId.toLowerCase()) { + case 'pri': + return prirorityColumnSort; + case 'ledetid': + return ledetidColumnSort; + default: + return standardColumnSort; + } +};