Skip to content

Commit

Permalink
Special sort for some columns (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
etse authored Sep 17, 2024
1 parent 594a496 commit 0ac5b46
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
11 changes: 4 additions & 7 deletions frontend/beCompliant/src/components/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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);
},
})
);
Expand Down
42 changes: 42 additions & 0 deletions frontend/beCompliant/src/components/table/TableSort.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};

0 comments on commit 0ac5b46

Please sign in to comment.