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

Open
wants to merge 5 commits into
base: integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/de/crop_nutrients.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
"VITAMIN_B12": "Vitamin B12",
"MAX_ROOTING": "Maximale Wurzelentwicklung",
"NUTRIENT_CREDITS": "Nährstoffgutschriften"
}
}
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/es/crop_nutrients.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
"VITAMIN_B12": "Vitamina B12",
"MAX_ROOTING": "Enraizamiento máximo",
"NUTRIENT_CREDITS": "Creditos nutricionales"
}
}
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/fr/crop_nutrients.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
"VITAMIN_B12": "Vitamine B12",
"MAX_ROOTING": "Enracinement Maximal",
"NUTRIENT_CREDITS": "Nutriments supplémentaires"
}
}
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/hi/crop_nutrients.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
"VITAMIN_B12": "विटामिन B12",
"MAX_ROOTING": "अधिकतम जड़न",
"NUTRIENT_CREDITS": "पोषक तत्व क्रेडिट"
}
}
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/ml/crop_nutrients.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
"VITAMIN_B12": "വിറ്റാമിൻ ബി12",
"MAX_ROOTING": "പരമാവധി വേരൂന്നാൻ",
"NUTRIENT_CREDITS": "പോഷകങ്ങളുടെ ക്രെഡിറ്റ്"
}
}
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/pa/crop_nutrients.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
"VITAMIN_B12": "ਵਿਟਾਮਿਨ ਬੀ 12",
"MAX_ROOTING": "ਅਧਿਕਤਮ ਰੀਫਲੈਕਸ",
"NUTRIENT_CREDITS": "ਪੌਸ਼ਟਿਕ ਕ੍ਰੈਡਿਟ"
}
}
2 changes: 1 addition & 1 deletion packages/webapp/public/locales/pt/crop_nutrients.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
"VITAMIN_B12": "Vitamina B12",
"MAX_ROOTING": "Enraizamento máximo",
"NUTRIENT_CREDITS": "Créditos de nutriente"
}
}
22 changes: 14 additions & 8 deletions packages/webapp/src/components/Table/TableV2.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,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%' }}>
Expand Down
1 change: 1 addition & 0 deletions packages/webapp/src/components/Table/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type TableV2Column = {
columnProps?: Object;
label?: string;
sortable?: boolean;
comparator?: any;
};

export type TableRowData = { id?: string | number };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Paper, 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 { AnimalInventoryItem } from './useAnimalInventory';
import ActionMenu, { iconAction } from '../../../components/ActionMenu';
import FixedHeaderContainer, {
Expand Down Expand Up @@ -309,6 +309,7 @@ export default function AnimalInventory({
highlightedText={d.batch ? d.count : null}
/>
),
comparator: (a: any, b: any) => animalIDComparator(a, b),
},
{
id: isDesktop ? 'type' : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export type AnimalInventoryItem = {
id: string;
iconName: AnimalTypeIconKey;
identification: string;
internal_identifier: number;
name: string | null;
type: string;
breed: string;
path: string;
Expand Down Expand Up @@ -153,11 +155,13 @@ 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),
path: createSingleAnimalViewURL(animal.internal_identifier),
count: 1,
batch: false,
name: animal.name,
location: animal.location_id ? locationsMap[animal.location_id] : '',
// preserve some untransformed data for filtering
sex_id: animal.sex_id,
Expand Down Expand Up @@ -190,10 +194,12 @@ 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),
path: createSingleAnimalViewURL(batch.internal_identifier),
count: batch.count,
name: batch.name,
batch: true,
location: batch.location_id ? locationsMap[batch.location_id] : '',
// preserve some untransformed data for filtering
Expand All @@ -218,6 +224,34 @@ interface BuildInventoryArgs {
locationsMap: { [key: string]: string };
}

export const animalIDComparator = (a: AnimalInventoryItem, b: AnimalInventoryItem) => {
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: AnimalInventoryItem[]) => {
return inventory.sort(animalIDComparator);
};

export const buildInventory = ({
animals,
animalBatches,
Expand Down Expand Up @@ -246,7 +280,7 @@ export const buildInventory = ({
),
];

const sortedInventory = inventory.sort(getComparator(orderEnum.ASC, 'identification'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking care of this!
getComparator function is used for sorting when clicking the "IDENTIFICATION" column title, and I think the same logic needs to be applied there too.

const sortedInventory = sortAnimalsIDs(inventory);

return sortedInventory;
};
Expand Down
Loading
Loading