Sort pick lists alphabetically

This commit is contained in:
beatz174-bit
2025-11-23 15:00:05 +10:00
parent 2f7d5024e8
commit 32b2e3eada
2 changed files with 71 additions and 4 deletions
+54
View File
@@ -0,0 +1,54 @@
import { MemoryRouter } from 'react-router-dom';
import { render, screen, within } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { PickListsScreen } from './PickListsScreen';
const areasMock = [
{ id: 'area-1', name: 'Front Counter', created_at: 0, updated_at: 0 },
{ id: 'area-2', name: 'back room', created_at: 0, updated_at: 0 },
{ id: 'area-3', name: 'Cafe', created_at: 0, updated_at: 0 },
];
const pickListsMock = [
{ id: 'list-2', area_id: 'area-2', created_at: 3 },
{ id: 'list-3', area_id: 'area-3', created_at: 4 },
{ id: 'list-1', area_id: 'area-1', created_at: 5 },
];
vi.mock('../hooks/dataHooks', () => ({
usePickLists: () => pickListsMock,
useAreas: () => areasMock,
}));
vi.mock('../context/DBProvider', () => ({
useDatabase: () => ({
pickItems: {
where: () => ({
equals: () => ({ delete: vi.fn() }),
}),
},
pickLists: {
update: vi.fn(),
delete: vi.fn(),
where: () => ({
equals: () => ({ delete: vi.fn() }),
}),
},
}),
}));
describe('PickListsScreen sorting', () => {
it('sorts pick lists alphabetically by area name', () => {
render(
<MemoryRouter>
<PickListsScreen />
</MemoryRouter>,
);
const listItems = screen.getAllByRole('listitem');
expect(within(listItems[0]).getByText('back room')).toBeVisible();
expect(within(listItems[1]).getByText('Cafe')).toBeVisible();
expect(within(listItems[2]).getByText('Front Counter')).toBeVisible();
});
});
+17 -4
View File
@@ -31,11 +31,24 @@ export const PickListsScreen = () => {
const [areaId, setAreaId] = useState(''); const [areaId, setAreaId] = useState('');
const [notes, setNotes] = useState(''); const [notes, setNotes] = useState('');
const sortedLists = useMemo(() => { const areaNameById = useMemo(() => {
return [...lists].sort((a, b) => a.created_at - b.created_at); const map = new Map<string, string>();
}, [lists]); areas.forEach((area) => map.set(area.id, area.name));
return map;
}, [areas]);
const getAreaName = (areaId: string) => areas.find((a) => a.id === areaId)?.name ?? 'Unknown area'; const sortedLists = useMemo(() => {
const locale = new Intl.Collator(undefined, { sensitivity: 'base' });
return [...lists].sort((a, b) => {
const nameA = areaNameById.get(a.area_id) ?? 'Unknown area';
const nameB = areaNameById.get(b.area_id) ?? 'Unknown area';
const nameComparison = locale.compare(nameA, nameB);
if (nameComparison !== 0) return nameComparison;
return a.created_at - b.created_at;
});
}, [areaNameById, lists]);
const getAreaName = (areaId: string) => areaNameById.get(areaId) ?? 'Unknown area';
const openEdit = (list: PickList) => { const openEdit = (list: PickList) => {
setEditingList(list); setEditingList(list);