mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
feat(places): All saved shows each place once, All places shows its count
A place saved into several lists from the same trip place showed once per list in All saved and counted that often, while All places groups the copies as one place. All saved now shows such a place once, the first copy read standing for it, and the rail counts All saved and All places the way All places groups them, on the desktop and the phone.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// FE-COMP-LISTSRAIL-001 to FE-COMP-LISTSRAIL-010
|
||||
// FE-COMP-LISTSRAIL-001 to FE-COMP-LISTSRAIL-013
|
||||
import React from 'react';
|
||||
import { render, screen, within } from '../../../tests/helpers/render';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
@@ -142,4 +142,15 @@ describe('ListsRail', () => {
|
||||
expect(screen.getByRole('button', { name: 'New list' })).toHaveTextContent('New list');
|
||||
expect(importBtn).toHaveTextContent('');
|
||||
});
|
||||
|
||||
it('FE-COMP-LISTSRAIL-012: All places shows how many places it holds once that is known', () => {
|
||||
renderRail({ onSelectLibrary: vi.fn(), libraryCount: 7, savedCount: 8 });
|
||||
expect(screen.getByRole('button', { name: /All places/ })).toHaveTextContent('All places7');
|
||||
expect(screen.getByRole('button', { name: /All saved/ })).toHaveTextContent('All saved8');
|
||||
});
|
||||
|
||||
it('FE-COMP-LISTSRAIL-013: without a known count All places shows no number', () => {
|
||||
renderRail({ onSelectLibrary: vi.fn(), libraryCount: null });
|
||||
expect(screen.getByRole('button', { name: /All places/ })).toHaveTextContent(/^All places$/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,6 +14,8 @@ interface ListsRailProps {
|
||||
activeId: ActiveCollectionId
|
||||
/** How many places the lists hold together, beside "All saved". */
|
||||
savedCount?: number
|
||||
/** How many places All places holds, each once; null while unknown. */
|
||||
libraryCount?: number | null
|
||||
/** The overview of every place is open (PLC-07); it sits above the lists. */
|
||||
libraryActive?: boolean
|
||||
onSelectLibrary?: () => void
|
||||
@@ -49,7 +51,7 @@ function ListRow({ list, active, onSelect }: { list: Collection; active: boolean
|
||||
*/
|
||||
export default function ListsRail(props: ListsRailProps): React.ReactElement {
|
||||
const {
|
||||
ownedLists, sharedLists, activeId, incomingInvites, libraryActive = false, onSelectLibrary, savedCount = 0,
|
||||
ownedLists, sharedLists, activeId, incomingInvites, libraryActive = false, onSelectLibrary, savedCount = 0, libraryCount = null,
|
||||
onSelect, onNewList, onImportList, onAcceptInvite, onDeclineInvite, t,
|
||||
} = props
|
||||
|
||||
@@ -78,6 +80,7 @@ export default function ListsRail(props: ListsRailProps): React.ReactElement {
|
||||
<button type="button" onClick={onSelectLibrary} className={`col-row-btn${libraryActive ? ' on' : ''}`} aria-current={libraryActive ? 'page' : undefined}>
|
||||
<span className="ico tile"><Globe2 size={15} /></span>
|
||||
<span className="nm">{t('collections.library.title')}</span>
|
||||
{libraryCount != null && libraryCount > 0 && <span className="ct">{libraryCount}</span>}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// FE-COMP-LIBCOUNT-001 to FE-COMP-LIBCOUNT-002: how many places All places holds, for its row beside the lists.
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { libraryEntry, libraryPage } from '../../../tests/helpers/placesLibrary';
|
||||
import { placesLibraryRepo } from '../../repo/placesLibraryRepo';
|
||||
import { useLibraryCount } from './useLibraryCount';
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('useLibraryCount', () => {
|
||||
it('FE-COMP-LIBCOUNT-001: reads the counts with one entry, and again when the lists change', async () => {
|
||||
const list = vi
|
||||
.spyOn(placesLibraryRepo, 'list')
|
||||
.mockResolvedValueOnce(libraryPage([libraryEntry()], { counts: { all: 7, trip: 2, list: 6 } }))
|
||||
.mockResolvedValueOnce(libraryPage([libraryEntry()], { counts: { all: 8, trip: 2, list: 7 } }));
|
||||
const first = [{ id: 1 }];
|
||||
const { result, rerender } = renderHook(({ lists }) => useLibraryCount(lists), { initialProps: { lists: first } });
|
||||
expect(result.current).toBeNull();
|
||||
await waitFor(() => expect(result.current).toEqual({ all: 7, saved: 6 }));
|
||||
expect(list).toHaveBeenCalledWith({ limit: 1 }, expect.any(AbortSignal));
|
||||
|
||||
rerender({ lists: first });
|
||||
expect(list).toHaveBeenCalledTimes(1);
|
||||
rerender({ lists: [{ id: 1 }, { id: 2 }] });
|
||||
await waitFor(() => expect(result.current).toEqual({ all: 8, saved: 7 }));
|
||||
});
|
||||
|
||||
it('FE-COMP-LIBCOUNT-002: a refused read shows no number', async () => {
|
||||
vi.spyOn(placesLibraryRepo, 'list').mockRejectedValue(new Error('offline'));
|
||||
const { result } = renderHook(() => useLibraryCount([]));
|
||||
await waitFor(() => expect(placesLibraryRepo.list).toHaveBeenCalled());
|
||||
expect(result.current).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { placesLibraryRepo } from '../../repo/placesLibraryRepo';
|
||||
|
||||
/** How many places All places holds, and how many of them are saved in a list; each place once. */
|
||||
export interface LibraryCounts {
|
||||
all: number;
|
||||
saved: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The counts beside All places and All saved in the rail: each place once,
|
||||
* however many lists and trips keep it. Read again whenever `lists` changes
|
||||
* (a place saved, moved or deleted there); null until they are known, and
|
||||
* offline, where All places cannot be read.
|
||||
*/
|
||||
export function useLibraryCount(lists: unknown): LibraryCounts | null {
|
||||
const [count, setCount] = useState<LibraryCounts | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const ctrl = new AbortController();
|
||||
placesLibraryRepo
|
||||
.list({ limit: 1 }, ctrl.signal)
|
||||
.then((page) => setCount({ all: page.counts.all, saved: page.counts.list }))
|
||||
.catch(() => {
|
||||
// Offline or refused: the row shows no number rather than a stale or a wrong one.
|
||||
if (!ctrl.signal.aborted) setCount(null);
|
||||
});
|
||||
return () => ctrl.abort();
|
||||
}, [lists]);
|
||||
|
||||
return count;
|
||||
}
|
||||
Reference in New Issue
Block a user