feat(web): the wishlist and the not-interested lists
Paged rows with remove, not interested and interested again. An artist links to its page only when it is in the library, since one kept alive only by a mark answers 404. The wishlist can hide the artists you already have.
This commit is contained in:
parent
60349153c0
commit
6d03b1b661
4 changed files with 221 additions and 0 deletions
|
|
@ -74,6 +74,13 @@
|
|||
"partial": "Last.fm could not be reached for some artists, so this list may be shorter.",
|
||||
"busy": "Discover is busy with another request. Try again in a moment.",
|
||||
"retryIn": "Try again in {{seconds}} s",
|
||||
"marked": {
|
||||
"hideOwned": "Hide the ones I already have",
|
||||
"remove": "Remove",
|
||||
"interestedAgain": "Interested again",
|
||||
"emptyWishlist": "Nothing on your wishlist yet. Add artists from the suggestions.",
|
||||
"emptyDismissed": "You have not dismissed any artist."
|
||||
},
|
||||
"seeds": {
|
||||
"label": "Find an artist to start from",
|
||||
"list": "Starting artists",
|
||||
|
|
|
|||
|
|
@ -74,6 +74,13 @@
|
|||
"partial": "Last.fm non era raggiungibile per alcuni artisti, quindi la lista può essere più corta.",
|
||||
"busy": "Discover è occupato con un'altra richiesta. Riprova tra poco.",
|
||||
"retryIn": "Riprova tra {{seconds}} s",
|
||||
"marked": {
|
||||
"hideOwned": "Nascondi quelli che ho già",
|
||||
"remove": "Togli",
|
||||
"interestedAgain": "Mi interessa di nuovo",
|
||||
"emptyWishlist": "Ancora niente nella wishlist. Aggiungi artisti dai suggerimenti.",
|
||||
"emptyDismissed": "Non hai scartato nessun artista."
|
||||
},
|
||||
"seeds": {
|
||||
"label": "Cerca un artista da cui partire",
|
||||
"list": "Artisti di partenza",
|
||||
|
|
|
|||
126
web/src/pages/discover/MarkedList.test.tsx
Normal file
126
web/src/pages/discover/MarkedList.test.tsx
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { render, screen, waitFor, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { MemoryRouter } from 'react-router-dom'
|
||||
import { beforeEach, expect, test, vi } from 'vitest'
|
||||
import { sessionStore } from '@/auth/session'
|
||||
import { initI18n } from '@/i18n'
|
||||
import { apiError, mockFetch, type MockRoutes } from '@/test/mockFetch'
|
||||
import MarkedList from './MarkedList'
|
||||
|
||||
const none = () => new Response(null, { status: 204 })
|
||||
|
||||
const marked = (id: number, name: string, in_library = false) => ({
|
||||
artist: { id, name },
|
||||
in_library,
|
||||
marked_at: '2026-11-12T10:00:00Z',
|
||||
})
|
||||
|
||||
const page = (items: object[], total = items.length, offset = 0) => ({ items, total, limit: 50, offset })
|
||||
|
||||
function renderList(kind: 'wishlist' | 'dismissed') {
|
||||
const client = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } })
|
||||
return render(
|
||||
<QueryClientProvider client={client}>
|
||||
<MemoryRouter>
|
||||
<MarkedList kind={kind} />
|
||||
</MemoryRouter>
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
}
|
||||
|
||||
function wishlist(over: MockRoutes = {}): MockRoutes {
|
||||
return { 'GET /api/v1/wishlist': () => page([marked(1, 'Portishead'), marked(2, 'Björk', true)]), ...over }
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
|
||||
await initI18n()
|
||||
sessionStore.setState({ status: 'authenticated', user: { id: 1, username: 'tom', is_admin: false } })
|
||||
})
|
||||
|
||||
test('an artist you have links to its page, one you only wish for is just a name', async () => {
|
||||
mockFetch(wishlist())
|
||||
renderList('wishlist')
|
||||
expect(await screen.findByRole('link', { name: 'Björk' })).toHaveAttribute('href', '/artists/2')
|
||||
expect(screen.getByText('Portishead')).toBeInTheDocument()
|
||||
expect(screen.queryByRole('link', { name: 'Portishead' })).not.toBeInTheDocument()
|
||||
expect(screen.getByText('In your library')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('"Hide the ones I already have" leaves only the wishes', async () => {
|
||||
mockFetch(wishlist())
|
||||
renderList('wishlist')
|
||||
await screen.findByText('Portishead')
|
||||
await userEvent.click(screen.getByRole('checkbox', { name: 'Hide the ones I already have' }))
|
||||
expect(screen.queryByText('Björk')).not.toBeInTheDocument()
|
||||
expect(screen.getByText('Portishead')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('removing a wish asks the server and the artist is gone when the list is read again', async () => {
|
||||
let items = [marked(1, 'Portishead'), marked(2, 'Björk', true)]
|
||||
const { calls } = mockFetch({
|
||||
'GET /api/v1/wishlist': () => page(items),
|
||||
'DELETE /api/v1/wishlist/1': () => {
|
||||
items = items.filter((m) => m.artist.id !== 1)
|
||||
return none()
|
||||
},
|
||||
})
|
||||
renderList('wishlist')
|
||||
const row = (await screen.findByText('Portishead')).closest('li') as HTMLElement
|
||||
await userEvent.click(within(row).getByRole('button', { name: 'Remove' }))
|
||||
await waitFor(() => expect(screen.queryByText('Portishead')).not.toBeInTheDocument())
|
||||
expect(calls.map((c) => c.key)).toContain('DELETE /api/v1/wishlist/1')
|
||||
})
|
||||
|
||||
test('"Not interested" on a wish dismisses the artist', async () => {
|
||||
const { calls } = mockFetch(wishlist({ 'POST /api/v1/discover/1/dismiss': none }))
|
||||
renderList('wishlist')
|
||||
const row = (await screen.findByText('Portishead')).closest('li') as HTMLElement
|
||||
await userEvent.click(within(row).getByRole('button', { name: 'Not interested' }))
|
||||
expect(calls.map((c) => c.key)).toContain('POST /api/v1/discover/1/dismiss')
|
||||
})
|
||||
|
||||
test('an empty wishlist says how to fill it', async () => {
|
||||
mockFetch({ 'GET /api/v1/wishlist': () => page([]) })
|
||||
renderList('wishlist')
|
||||
expect(await screen.findByText('Nothing on your wishlist yet. Add artists from the suggestions.')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('the dismissed artists can be wanted again', async () => {
|
||||
const { calls } = mockFetch({
|
||||
'GET /api/v1/discover/dismissed': () => page([marked(5, 'Oasis')]),
|
||||
'DELETE /api/v1/discover/5/dismiss': none,
|
||||
})
|
||||
renderList('dismissed')
|
||||
const row = (await screen.findByText('Oasis')).closest('li') as HTMLElement
|
||||
await userEvent.click(within(row).getByRole('button', { name: 'Interested again' }))
|
||||
expect(calls.map((c) => c.key)).toContain('DELETE /api/v1/discover/5/dismiss')
|
||||
})
|
||||
|
||||
test('an empty dismissed list says so', async () => {
|
||||
mockFetch({ 'GET /api/v1/discover/dismissed': () => page([]) })
|
||||
renderList('dismissed')
|
||||
expect(await screen.findByText('You have not dismissed any artist.')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('a long list is read a page at a time', async () => {
|
||||
const { calls } = mockFetch({
|
||||
'GET /api/v1/wishlist': ({ url }) => {
|
||||
const offset = Number(url.searchParams.get('offset'))
|
||||
return offset === 0 ? page([marked(1, 'First')], 2, 0) : page([marked(2, 'Second')], 2, offset)
|
||||
},
|
||||
})
|
||||
renderList('wishlist')
|
||||
await screen.findByText('First')
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Show more' }))
|
||||
expect(await screen.findByText('Second')).toBeInTheDocument()
|
||||
expect(calls).toHaveLength(2)
|
||||
})
|
||||
|
||||
test('a failure is said in words with a way to try again', async () => {
|
||||
mockFetch({ 'GET /api/v1/wishlist': () => apiError(500, 'internal') })
|
||||
renderList('wishlist')
|
||||
const alert = await screen.findByRole('alert')
|
||||
expect(within(alert).getByRole('button', { name: 'Try again' })).toBeInTheDocument()
|
||||
})
|
||||
81
web/src/pages/discover/MarkedList.tsx
Normal file
81
web/src/pages/discover/MarkedList.tsx
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
import { useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { ApiError } from '@/api/client'
|
||||
import { useArtistMarks, useDismissed, useWishlist, type MarkedArtist } from '@/api/discover'
|
||||
import LoadMore from '@/components/LoadMore'
|
||||
import { EmptyMessage, ErrorMessage, Loading } from '@/components/Status'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { errorMessageKey } from '@/i18n'
|
||||
|
||||
/** The wishlist, or the artists you are not interested in: one row each, with what you can do about it. */
|
||||
export default function MarkedList({ kind }: { kind: 'wishlist' | 'dismissed' }) {
|
||||
const { t } = useTranslation()
|
||||
const wished = useWishlist({ enabled: kind === 'wishlist' })
|
||||
const dismissed = useDismissed({ enabled: kind === 'dismissed' })
|
||||
const query = kind === 'wishlist' ? wished : dismissed
|
||||
const marks = useArtistMarks()
|
||||
const [hideOwned, setHideOwned] = useState(false)
|
||||
|
||||
const all = query.data?.items ?? []
|
||||
const items = hideOwned ? all.filter((m) => !m.in_library) : all
|
||||
const failure = marks.removeFromWishlist.error ?? marks.dismiss.error ?? marks.undismiss.error
|
||||
|
||||
if (query.isError) return <ErrorMessage error={query.error} onRetry={() => void query.refetch()} />
|
||||
if (query.isPending) return <Loading />
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
{kind === 'wishlist' && all.length > 0 && (
|
||||
<label className="flex min-h-11 items-center gap-3 font-semibold">
|
||||
<input type="checkbox" checked={hideOwned} onChange={(e) => setHideOwned(e.target.checked)} className="size-5 accent-primary" />
|
||||
{t('discover.marked.hideOwned')}
|
||||
</label>
|
||||
)}
|
||||
|
||||
{all.length === 0 ? (
|
||||
<EmptyMessage>{t(kind === 'wishlist' ? 'discover.marked.emptyWishlist' : 'discover.marked.emptyDismissed')}</EmptyMessage>
|
||||
) : (
|
||||
<ul className="flex flex-col gap-3">
|
||||
{items.map((m: MarkedArtist) => (
|
||||
<li key={m.artist.id} className="flex flex-wrap items-center gap-4 rounded-lg bg-card p-4 text-card-foreground">
|
||||
<div className="flex min-w-0 flex-1 flex-col">
|
||||
{m.in_library ? (
|
||||
<Link to={`/artists/${m.artist.id}`} className="truncate text-lg font-bold hover:underline">
|
||||
{m.artist.name}
|
||||
</Link>
|
||||
) : (
|
||||
<span className="truncate text-lg font-bold">{m.artist.name}</span>
|
||||
)}
|
||||
{m.in_library && <span className="text-sm text-muted-foreground">{t('discover.inLibrary')}</span>}
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{kind === 'wishlist' ? (
|
||||
<>
|
||||
<Button variant="outline" onClick={() => marks.removeFromWishlist.mutate(m.artist.id)}>
|
||||
{t('discover.marked.remove')}
|
||||
</Button>
|
||||
<Button variant="outline" onClick={() => marks.dismiss.mutate(m.artist.id)}>
|
||||
{t('discover.notInterested')}
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<Button variant="outline" onClick={() => marks.undismiss.mutate(m.artist.id)}>
|
||||
{t('discover.marked.interestedAgain')}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
|
||||
{failure ? (
|
||||
<p role="alert" className="text-sm text-destructive">
|
||||
{t(errorMessageKey(failure instanceof ApiError ? failure.code : 'unknown'))}
|
||||
</p>
|
||||
) : null}
|
||||
<LoadMore query={query} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Loading…
Reference in a new issue