Skip to content

Commit

Permalink
adjusted sort func in feature to be simpler and moved complexity to s…
Browse files Browse the repository at this point in the history
…ortResult func
  • Loading branch information
JochemVH1 committed Jan 28, 2025
1 parent 1ee622a commit a2dfc3e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions frontend/src/components/client/models/getClientFeature.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -33,7 +33,7 @@ const clientListConfig = (config: ClientFeatureBuilderConfig): IList<ClientModel
<span>{client.btw}</span>
</>
),
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',
Expand All @@ -46,7 +46,7 @@ const clientListConfig = (config: ClientFeatureBuilderConfig): IList<ClientModel
<span>{client.telephone}</span>
</>
),
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',
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/components/consultant/ConsultantProjectsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -90,22 +90,22 @@ const consultantListConfig = (config: ConsultantFeatureBuilderConfig): IList<Con
header: 'project.consultant',
value: p => <ConsultantLinkWithModal consultant={p.consultant} showType />,
footer: (models: ConsultantProject[]) => <ConsultantCountFooter consultants={models.map(x => 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 => <InvoiceClientCell client={p.client} />,
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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -40,11 +40,11 @@ const consultantListConfig = (config: ConsultantFeatureBuilderConfig): IList<Con
const cells: IListCell<ConsultantModel>[] = [{
key: 'name',
value: m => <ConsultantLinkWithModal consultant={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 => {
Expand All @@ -53,7 +53,7 @@ const consultantListConfig = (config: ConsultantFeatureBuilderConfig): IList<Con
}
return '';
},
sort: (asc) => (c, c1) => sortResult(c.email.localeCompare(c1.email) > 0, asc)
sort: (c, c1) => c.email.localeCompare(c1.email)
}, {
key: 'telephone',
value: m => m.telephone,
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/controls/table/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/controls/table/table-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface IListCell<TModel> {
/** 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
}


Expand Down
14 changes: 7 additions & 7 deletions frontend/src/components/project/models/getProjectFeature.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -115,23 +115,23 @@ const projectListConfig = (config: ProjectFeatureBuilderConfig): IList<FullProje
header: 'project.consultant',
value: p => <ConsultantLinkWithModal consultant={p.consultant} showType />,
footer: (models: FullProjectModel[]) => <ConsultantCountFooter consultants={models.map(x => 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 => <InvoiceClientCell client={p.partner} />,
footer: (models: FullProjectModel[]) => <ProjectForecastPartnerFooter models={models} />,
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',
Expand All @@ -141,12 +141,12 @@ const projectListConfig = (config: ProjectFeatureBuilderConfig): IList<FullProje
header: '',
value: p => <ProjectEndCustomerIcon endCustomer={p.details.endCustomer} endCustomerClientModel={p.endCustomer}/>,
footer: (models: FullProjectModel[]) => <ProjectClientForecastFooter models={models} />,
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 => <InvoiceClientCell client={p.client} />,
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',
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit a2dfc3e

Please sign in to comment.