Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LF-4415 Animals IDs not sorted in order #3541

Merged
merged 15 commits into from
Dec 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
LF-4415: Fix sorting and add to table column order
  • Loading branch information
Tbrid committed Dec 6, 2024
commit 133b2351d24a387f060d227c205ce814927da852
22 changes: 14 additions & 8 deletions packages/webapp/src/components/Table/TableV2.jsx
Original file line number Diff line number Diff line change
@@ -150,14 +150,20 @@ export default function TableV2(props) {
// Avoid a layout jump when reaching the last page with empty rows.
const emptyRows = page > 0 ? Math.max(0, (1 + page) * rowsPerPage - data.length) : 0;

const visibleRows = useMemo(
() =>
data
.slice()
.sort(getComparator(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage),
[order, orderBy, page, rowsPerPage, data],
);
const visibleRows = useMemo(() => {
const column = columns.find((col) => col.id == orderBy);
const comparator = column && column.comparator;
return data
.slice()
.sort(
comparator
? order === 'desc'
? (a, b) => -comparator(a, b)
: comparator
: getComparator(order, orderBy),
)
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage);
}, [order, orderBy, page, rowsPerPage, data]);

return (
<Box sx={{ width: '100%' }}>
1 change: 1 addition & 0 deletions packages/webapp/src/components/Table/types.ts
Original file line number Diff line number Diff line change
@@ -58,6 +58,7 @@ export type TableV2Column = {
columnProps?: Object;
label?: string;
sortable?: boolean;
comparator?: any;
};

export type TableRowData = { id?: string | number };
3 changes: 2 additions & 1 deletion packages/webapp/src/containers/Animals/Inventory/index.tsx
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ import { useMediaQuery } from '@mui/material';
import { History } from 'history';
import Cell from '../../../components/Table/Cell';
import { CellKind } from '../../../components/Table/types';
import useAnimalInventory from './useAnimalInventory';
import useAnimalInventory, { animalIDComparator } from './useAnimalInventory';
import type { AnimalInventory } from './useAnimalInventory';
import ActionMenu, { iconAction } from '../../../components/ActionMenu';
import FixedHeaderContainer, {
@@ -122,6 +122,7 @@ function AnimalInventory({
highlightedText={d.batch ? d.count : null}
/>
),
comparator: (a: any, b: any) => animalIDComparator(a, b),
},
{
id: isDesktop ? 'type' : null,
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ export type AnimalInventory = {
id: string;
iconName: AnimalTypeIconKey;
identification: string;
internal_identifier: number;
name: string | null;
type: string;
breed: string;
@@ -152,6 +153,7 @@ const formatAnimalsData = (
id: generateInventoryId(AnimalOrBatchKeys.ANIMAL, animal),
iconName: getDefaultAnimalIconName(defaultAnimalTypes, animal.default_type_id),
identification: chooseIdentification(animal),
internal_identifier: animal.internal_identifier,
type: chooseAnimalTypeLabel(animal, defaultAnimalTypes, customAnimalTypes),
breed: chooseAnimalBreedLabel(animal, defaultAnimalBreeds, customAnimalBreeds),
groups: animal.group_ids.map((id: number) => getProperty(animalGroups, id, 'name')),
@@ -189,6 +191,7 @@ const formatAnimalBatchesData = (
id: generateInventoryId(AnimalOrBatchKeys.BATCH, batch),
iconName: 'BATCH',
identification: chooseIdentification(batch),
internal_identifier: batch.internal_identifier,
type: chooseAnimalTypeLabel(batch, defaultAnimalTypes, customAnimalTypes),
breed: chooseAnimalBreedLabel(batch, defaultAnimalBreeds, customAnimalBreeds),
groups: batch.group_ids.map((id: number) => getProperty(animalGroups, id, 'name')),
@@ -217,20 +220,32 @@ interface BuildInventoryArgs {
defaultAnimalTypes: DefaultAnimalType[];
}

export const animalIDComparator = (a: AnimalInventory, b: AnimalInventory) => {
if (a.name && !b.name) {
return -1;
}
if (b.name && !a.name) {
return 1;
}
if (a.identification && !b.identification) {
return -1;
}
if (b.identification && !a.identification) {
return 1;
}

return (
(a.name && b.name && (a.name.length - b.name.length || a.name.localeCompare(b.name))) ||
(a.identification &&
b.identification &&
(a.identification.length - b.identification.length ||
a.identification.localeCompare(b.identification))) ||
a.internal_identifier - b.internal_identifier
);
};

const sortAnimalsIDs = (inventory: AnimalInventory[]) => {
return inventory.sort((a, b) => {
if (a.name && !b.name) {
return -1;
} else if (!a.name && b.name) {
return 1;
} else {
if (a.identification.length > b.identification.length) {
return 1;
} else {
return a.identification.localeCompare(b.identification);
}
}
});
return inventory.sort(animalIDComparator);
};

export const buildInventory = ({