From a2dfc3ef68f384f4e41228ca83cac90971ddcf19 Mon Sep 17 00:00:00 2001 From: JochemVH1 Date: Tue, 28 Jan 2025 13:10:32 +0100 Subject: [PATCH] adjusted sort func in feature to be simpler and moved complexity to sortResult func --- .../components/client/models/getClientFeature.tsx | 6 +++--- .../consultant/ConsultantProjectsList.tsx | 10 +++++----- .../consultant/models/getConsultantFeature.tsx | 8 ++++---- frontend/src/components/controls/table/List.tsx | 6 +++--- .../src/components/controls/table/table-models.ts | 2 +- .../project/models/getProjectFeature.tsx | 14 +++++++------- frontend/src/components/utils.ts | 4 ++-- 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/frontend/src/components/client/models/getClientFeature.tsx b/frontend/src/components/client/models/getClientFeature.tsx index 86321258..f637c91a 100644 --- a/frontend/src/components/client/models/getClientFeature.tsx +++ b/frontend/src/components/client/models/getClientFeature.tsx @@ -7,7 +7,7 @@ import {InvoiceWorkedDays} from '../../invoice/invoice-list/InvoiceWorkedDays'; import InvoiceModel from '../../invoice/models/InvoiceModel'; import {InvoicesSummary} from '../../invoice/controls/InvoicesSummary'; import {DeleteIcon} from '../../controls/icons/DeleteIcon'; -import {sortResult, t} from '../../utils'; +import {t} from '../../utils'; import {searchClientForList} from './searchClientFor'; import {getInvoiceYears} from '../../invoice/models/InvoiceListModel'; import {YearsSelect} from '../../controls/form-controls/select/YearsSelect'; @@ -33,7 +33,7 @@ const clientListConfig = (config: ClientFeatureBuilderConfig): IList{client.btw} ), - sort: (asc) => (c1, c2) => sortResult(c1.name.localeCompare(c2.name) > 0, asc) + sort: (c1, c2) => c1.name.localeCompare(c2.name) }, { key: 'contact', header: 'client.contact', @@ -46,7 +46,7 @@ const clientListConfig = (config: ClientFeatureBuilderConfig): IList{client.telephone} ), - sort: (asc) => (c1, c2) => sortResult(c1.address.localeCompare(c2.address) > 0, asc) + sort: (c1, c2) => c1.address.localeCompare(c2.address) }, { key: 'time-invested', header: 'client.timeTitle', diff --git a/frontend/src/components/consultant/ConsultantProjectsList.tsx b/frontend/src/components/consultant/ConsultantProjectsList.tsx index 0fec5a27..f2b0274a 100644 --- a/frontend/src/components/consultant/ConsultantProjectsList.tsx +++ b/frontend/src/components/consultant/ConsultantProjectsList.tsx @@ -8,7 +8,7 @@ import {useDocumentTitle} from '../hooks/useDocumentTitle'; import {Features, IFeature, IFeatureBuilderConfig} from '../controls/feature/feature-models'; import {ConsultantModel} from './models/ConsultantModel'; import {IProjectModel} from '../project/models/IProjectModel'; -import {formatDate, searchinize, sortResult, t} from '../utils'; +import {formatDate, searchinize, t} from '../utils'; import {IList, IListCell, ListFilters} from '../controls/table/table-models'; import {ClientModel} from '../client/models/ClientModels'; import { ConsultantLinkWithModal } from "./controls/ConsultantLinkWithModal"; @@ -90,22 +90,22 @@ const consultantListConfig = (config: ConsultantFeatureBuilderConfig): IList , footer: (models: ConsultantProject[]) => x.consultant)} />, - sort: (asc) => (cp, cp1) => sortResult(cp.consultant.firstName.localeCompare(cp1.consultant.firstName) > 0, asc), + sort: (cp, cp1) => cp.consultant.firstName.localeCompare(cp1.consultant.firstName), }, { key: 'startDate', header: 'project.startDate', value: p => formatDate(p.project?.startDate), - sort: (asc) => (cp, cp1) => sortResult((cp.project?.startDate?.valueOf() ?? 0) - (cp1.project?.startDate?.valueOf() ?? 0) > 0, asc), + sort: (cp, cp1) => (cp.project?.startDate?.valueOf() ?? 0) - (cp1.project?.startDate?.valueOf() ?? 0), }, { key: 'endDate', header: 'project.endDate', value: p => p.project?.endDate && formatDate(p.project?.endDate), - sort: (asc) => (cp, cp1) => sortResult((cp.project?.endDate?.valueOf() ?? 0) - (cp1.project?.endDate?.valueOf() ?? 0) > 0, asc), + sort: (cp, cp1) => (cp.project?.endDate?.valueOf() ?? 0) - (cp1.project?.endDate?.valueOf() ?? 0), }, { key: 'client', header: 'project.client.clientId', value: p => , - sort: (asc) => (cp, cp1) => sortResult((cp.client?.name ?? '').localeCompare((cp1.client?.name ?? '')) > 0, asc), + sort: (cp, cp1) => (cp.client?.name ?? '').localeCompare((cp1.client?.name ?? '')), }, { key: 'clientTariff', header: 'project.client.tariff', diff --git a/frontend/src/components/consultant/models/getConsultantFeature.tsx b/frontend/src/components/consultant/models/getConsultantFeature.tsx index ddabc4a7..3f8fe4ca 100644 --- a/frontend/src/components/consultant/models/getConsultantFeature.tsx +++ b/frontend/src/components/consultant/models/getConsultantFeature.tsx @@ -1,7 +1,7 @@ /* eslint-disable react/jsx-one-expression-per-line */ import {ConsultantModel} from './ConsultantModel'; import {IList, IListCell, ConsultantListFilters} from '../../controls/table/table-models'; -import {t, searchinize, sortResult} from '../../utils'; +import {t, searchinize} from '../../utils'; import {Features, IFeature, IFeatureBuilderConfig} from '../../controls/feature/feature-models'; import {features} from '../../../trans'; import {EditIcon} from '../../controls/Icon'; @@ -40,11 +40,11 @@ const consultantListConfig = (config: ConsultantFeatureBuilderConfig): IList[] = [{ key: 'name', value: m => , - sort: (asc) => (c, c1) => sortResult(c.firstName.localeCompare(c1.firstName) > 0, asc) + sort: (c, c1) => c.firstName.localeCompare(c1.firstName) }, { key: 'type', value: m => t(`consultant.types.${m.type}`), - sort: (asc) => (c, c1) => sortResult(c.type.localeCompare(c1.type) > 0, asc) + sort: (c, c1) => c.type.localeCompare(c1.type) }, { key: 'email', value: m => { @@ -53,7 +53,7 @@ const consultantListConfig = (config: ConsultantFeatureBuilderConfig): IList (c, c1) => sortResult(c.email.localeCompare(c1.email) > 0, asc) + sort: (c, c1) => c.email.localeCompare(c1.email) }, { key: 'telephone', value: m => m.telephone, diff --git a/frontend/src/components/controls/table/List.tsx b/frontend/src/components/controls/table/List.tsx index efed0d8f..7705e9b5 100644 --- a/frontend/src/components/controls/table/List.tsx +++ b/frontend/src/components/controls/table/List.tsx @@ -8,6 +8,7 @@ import {useSelector} from 'react-redux'; import {ConfacState} from '../../../reducers/app-state'; import { Pagination } from './Pagination'; import { SortDirections } from './table-models'; +import { sortResult } from '../../utils'; type ListProps = { @@ -32,9 +33,8 @@ export const List = ({feature}: ListProps) => { const key = feature.list.filter?.state?.sort.columnName; const cell = feature.list.rows.cells.find(col => col.key === key) if(cell && cell.sort){ - //IListCell.sort returns a function to pass to Array.Sort - const sortFunc = cell.sort(feature.list.filter?.state?.sort.direction === SortDirections.ASC) - data = data.slice().sort(sortFunc); + const asc = feature.list.filter?.state?.sort.direction === SortDirections.ASC; + data = data.slice().sort(sortResult(cell.sort, asc)); } } else if (feature.list.sorter) { diff --git a/frontend/src/components/controls/table/table-models.ts b/frontend/src/components/controls/table/table-models.ts index f636fcb7..0a110c11 100644 --- a/frontend/src/components/controls/table/table-models.ts +++ b/frontend/src/components/controls/table/table-models.ts @@ -98,7 +98,7 @@ export interface IListCell { /** Will span until next cell with a footer */ footer?: string | ((models: TModel[]) => string | React.ReactNode); - sort?: (asc: boolean) => (a: TModel, b: TModel) => number + sort?: (a: TModel, b: TModel) => number } diff --git a/frontend/src/components/project/models/getProjectFeature.tsx b/frontend/src/components/project/models/getProjectFeature.tsx index bca255b5..849d50bf 100644 --- a/frontend/src/components/project/models/getProjectFeature.tsx +++ b/frontend/src/components/project/models/getProjectFeature.tsx @@ -5,7 +5,7 @@ import {Features, IFeature, IFeatureBuilderConfig} from '../../controls/feature/ import {features} from '../../../trans'; import {IProjectModel, ProjectClientModel, ProjectStatus} from './IProjectModel'; import {FullProjectModel} from './FullProjectModel'; -import {t, formatDate, tariffFormat, searchinize, sortResult} from '../../utils'; +import {t, formatDate, tariffFormat, searchinize} from '../../utils'; import {EditIcon} from '../../controls/Icon'; import {InvoiceClientCell} from '../../invoice/invoice-table/InvoiceClientCell'; import { ConsultantLinkWithModal } from "../../consultant/controls/ConsultantLinkWithModal"; @@ -115,23 +115,23 @@ const projectListConfig = (config: ProjectFeatureBuilderConfig): IList , footer: (models: FullProjectModel[]) => x.consultant)} />, - sort: (asc) => (p, p2) => sortResult(p.consultantName.localeCompare(p2.consultantName) > 0, asc) + sort: (p, p2) => p.consultantName.localeCompare(p2.consultantName) }, { key: 'startDate', header: 'project.startDate', value: p => formatDate(p.details.startDate), - sort: (asc) => (p, p2) => sortResult(p.details.startDate.valueOf() - p2.details.startDate.valueOf() > 0, asc) + sort: (p, p2) => p.details.startDate.valueOf() - p2.details.startDate.valueOf() }, { key: 'endDate', header: 'project.endDate', value: p => p.details.endDate && formatDate(p.details.endDate), - sort: (asc) => (p, p2) => sortResult((p.details.endDate?.valueOf() ?? 0) - (p2.details.endDate?.valueOf() ?? 0) > 0, asc) + sort: (p, p2) => (p.details.endDate?.valueOf() ?? 0) - (p2.details.endDate?.valueOf() ?? 0) }, { key: 'partner', header: 'project.partner.clientId', value: p => , footer: (models: FullProjectModel[]) => , - sort: (asc) => (p, p2) => sortResult((p.partner?.name ?? '').localeCompare(p2.partner?.name ?? '') > 0, asc) + sort: (p, p2) => (p.partner?.name ?? '').localeCompare(p2.partner?.name ?? '') }, { key: 'partnerTariff', header: 'project.partner.tariff', @@ -141,12 +141,12 @@ const projectListConfig = (config: ProjectFeatureBuilderConfig): IList , footer: (models: FullProjectModel[]) => , - sort: (asc) => (p, p2) => sortResult((p.endCustomer?.name ?? '').localeCompare(p2.endCustomer?.name ?? '') > 0, asc) + sort: (p, p2) => (p.endCustomer?.name ?? '').localeCompare(p2.endCustomer?.name ?? '') }, { key: 'client', header: 'project.client.clientId', value: p => , - sort: (asc) => (p, p2) => sortResult((p.client?.name ?? '').localeCompare(p2.client?.name ?? '') > 0, asc) + sort:(p, p2) => (p.client?.name ?? '').localeCompare(p2.client?.name ?? '') }, { key: 'clientTariff', header: 'project.client.tariff', diff --git a/frontend/src/components/utils.ts b/frontend/src/components/utils.ts index 5f5bb29a..4d22d018 100644 --- a/frontend/src/components/utils.ts +++ b/frontend/src/components/utils.ts @@ -64,8 +64,8 @@ export const searchinize = (str: string): string => { return latinize(str).trim().toLowerCase(); }; -export const sortResult = (comparison: boolean, asc: boolean):number => { - return comparison ? (asc ? 1 : -1) : (asc ? -1 : 1) +export const sortResult = (sorter: (a: any, b: any) => number, asc: boolean): (a: any, b:any) => number => { + return (a, b) => asc ? sorter(a, b) : sorter(b, a); } export {default as t} from '../trans';