Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ The frontend supports searching by album title or artist, committed on Search or

### Backend search behavior

The API uses PostgreSQL full-text search for album titles and artist names, with prefix matching to support partial words (e.g. `radi` matches `Radiohead`). It supports genre and decade filters, relevance/title/release-year sorting, facet counts and cursor-based pagination. These capabilities are available through GraphQL but are not all exposed in the frontend yet.
The API uses PostgreSQL full-text search for album titles and artist names, with prefix matching to support partial words (e.g. `radi` matches `Radiohead`). It supports genre, decade and has-reviews filters, relevance/title/release-year sorting, facet counts and cursor-based pagination. The search page has a filter panel with these counts; sorting and paging controls are not exposed in the frontend yet.

## Try the application

Expand Down
10 changes: 10 additions & 0 deletions client/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
border-top: 1px solid var(--border);
padding-top: 1.25rem;
}
.catalogue-layout.with-filters {
display: grid;
grid-template-columns: 220px minmax(0, 1fr);
gap: 2rem;
align-items: start;
}
.site-footer {
text-align: center;
border-top: 1px solid var(--border);
Expand All @@ -71,6 +77,10 @@
color: var(--muted);
}
@media (max-width: 700px) {
.catalogue-layout.with-filters {
grid-template-columns: minmax(0, 1fr);
gap: 1rem;
}
#main {
padding-inline: 1rem;
}
Expand Down
37 changes: 37 additions & 0 deletions client/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ function mockSearch(
searchAlbums: {
edges: albums.map((node) => ({ cursor: node.id, node })),
totalCount: albums.length,
genreFacets: [],
decadeFacets: [],
reviewedCount: 0,
pageInfo: { hasNextPage: false, endCursor: null },
},
},
Expand Down Expand Up @@ -94,6 +97,9 @@ describe('App search', () => {
searchAlbums: {
edges: [],
totalCount: 0,
genreFacets: [],
decadeFacets: [],
reviewedCount: 0,
pageInfo: { hasNextPage: false, endCursor: null },
},
},
Expand All @@ -118,6 +124,9 @@ describe('App search', () => {
searchAlbums: {
edges: [{ cursor: '1', node: album }],
totalCount: 1,
genreFacets: [],
decadeFacets: [],
reviewedCount: 0,
pageInfo: { hasNextPage: false, endCursor: '1' },
},
},
Expand Down Expand Up @@ -179,6 +188,34 @@ describe('App search', () => {
expect(window.location.search).toBe('?q=Abbey');
});

it('filters by a genre from its facet and restarts on the first page', async () => {
const rock = { genre: { id: '1', name: 'Rock' }, count: 1 };
const unfiltered = mockSearch('Abbey');
const filtered = mockSearch('Abbey');
filtered.request.variables = {
input: { query: 'Abbey', sort: 'RELEVANCE', genreIds: ['1'] },
first: 20,
after: null,
};
for (const mock of [unfiltered, filtered]) {
const data = (mock.result as { data: SearchAlbumsData }).data;
data.searchAlbums.genreFacets = [rock];
}
renderApp([unfiltered, filtered]);
submitSearch('Abbey');

const checkbox = await screen.findByRole('checkbox', { name: 'Rock 1 albums' });
fireEvent.click(checkbox);

expect(window.location.search).toBe('?q=Abbey&genres=1');
await waitFor(() =>
expect(screen.getByRole('checkbox', { name: 'Rock 1 albums' })).toBeChecked(),
);
expect(
await screen.findByRole('article', { name: 'Abbey Road' }),
).toBeInTheDocument();
});

it('returns to an empty discovery page from the main navigation', () => {
window.history.replaceState({}, '', '/?q=Abbey&sort=TITLE_ASC');
renderApp();
Expand Down
20 changes: 19 additions & 1 deletion client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import SearchForm from './components/SearchForm.js';
import ResultsLayout from './components/ResultsLayout.js';
import FilterPanel from './components/FilterPanel.js';
import { lazy, Suspense } from 'react';

import { useAlbumSearch } from './hooks/useAlbumSearch.js';
Expand Down Expand Up @@ -59,7 +60,24 @@ function DiscoveryPage() {
<p className="tagline">Find something worth listening to.</p>
<SearchForm query={search.query} onSearch={search.search} />
</section>
<div id="catalogue" className="catalogue-layout">
<div
id="catalogue"
className={search.hasQuery ? 'catalogue-layout with-filters' : 'catalogue-layout'}
>
{search.hasQuery && (
<FilterPanel
genres={search.filters.genres}
decades={search.filters.decades}
reviewed={search.filters.reviewed}
genreFacets={search.facets.genres}
decadeFacets={search.facets.decades}
reviewedCount={search.facets.reviewedCount}
onGenresChange={search.setGenres}
onDecadesChange={search.setDecades}
onReviewedChange={search.setReviewed}
onClear={search.clearFilters}
/>
)}
<ResultsLayout
albums={search.albums}
totalCount={search.totalCount}
Expand Down
3 changes: 3 additions & 0 deletions client/src/a11y.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ function mockSearch(
searchAlbums: {
edges: albums.map((node) => ({ cursor: node.id, node })),
totalCount: albums.length,
genreFacets: [],
decadeFacets: [],
reviewedCount: 0,
pageInfo: { hasNextPage: false, endCursor: null },
},
},
Expand Down
3 changes: 3 additions & 0 deletions client/src/api/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ function result(ids: string[], endCursor: string, hasNextPage = true): SearchAlb
})),
pageInfo: { endCursor, hasNextPage },
totalCount: 4,
genreFacets: [],
decadeFacets: [],
reviewedCount: 0,
},
};
}
Expand Down
28 changes: 27 additions & 1 deletion client/src/api/searchAlbums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface AlbumSearchInput {
query: string;
genreIds?: string[] | null;
decades?: number[] | null;
hasReviews?: boolean | null;
sort: AlbumSort;
}

Expand All @@ -33,6 +34,19 @@ export interface AlbumConnection {
edges: { cursor: string; node: AlbumSummary }[];
pageInfo: { hasNextPage: boolean; endCursor: string | null };
totalCount: number;
genreFacets: GenreFacet[];
decadeFacets: DecadeFacet[];
reviewedCount: number;
}

export interface GenreFacet {
genre: { id: string; name: string };
count: number;
}

export interface DecadeFacet {
decade: number;
count: number;
}

export interface SearchAlbumsData {
Expand All @@ -46,7 +60,7 @@ export interface SearchAlbumsVariables {
}

// gql parses the operation; TypedDocumentNode describes its variables and selected result fields.
// Only the search-page projection is requested; details and facets belong to later work.
// Only the search-page projection is requested; details belong to the album page.
export const SEARCH_ALBUMS: TypedDocumentNode<SearchAlbumsData, SearchAlbumsVariables> =
gql`
query SearchAlbums($input: AlbumSearchInput!, $first: Int!, $after: String) {
Expand All @@ -70,6 +84,18 @@ export const SEARCH_ALBUMS: TypedDocumentNode<SearchAlbumsData, SearchAlbumsVari
}
}
totalCount
genreFacets {
genre {
id
name
}
count
}
decadeFacets {
decade
count
}
reviewedCount
pageInfo {
hasNextPage
endCursor
Expand Down
38 changes: 38 additions & 0 deletions client/src/components/FilterPanel.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.filter-panel h2 {
margin: 0 0 0.75rem;
font-size: 1.3rem;
letter-spacing: -0.025em;
}
.filter-panel fieldset {
border: 0;
border-top: 1px solid var(--border);
margin: 0;
padding: 0.75rem 0 1rem;
}
.filter-panel legend {
padding: 0;
font-size: 0.75rem;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.1em;
color: var(--muted);
}
.filter-panel label {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.25rem 0;
}
.filter-count {
margin-left: auto;
font-size: 0.8rem;
color: var(--chip-text);
background: var(--chip-bg);
border-radius: 999px;
padding: 0 0.5rem;
}
.clear-filters {
background: var(--surface);
color: var(--text);
border: 1px solid var(--border-strong);
}
81 changes: 81 additions & 0 deletions client/src/components/FilterPanel.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
import axe from 'axe-core';
import FilterPanel from './FilterPanel.js';

type Props = Parameters<typeof FilterPanel>[0];

function renderPanel(over: Partial<Props> = {}) {
const props: Props = {
genres: [],
decades: [],
reviewed: false,
genreFacets: [
{ genre: { id: '4', name: 'Rock' }, count: 7 },
{ genre: { id: '2', name: 'Jazz' }, count: 0 },
],
decadeFacets: [
{ decade: 1970, count: 3 },
{ decade: 1990, count: 0 },
],
reviewedCount: 2,
onGenresChange: vi.fn(),
onDecadesChange: vi.fn(),
onReviewedChange: vi.fn(),
onClear: vi.fn(),
...over,
};
return { props, ...render(<FilterPanel {...props} />) };
}

describe('FilterPanel', () => {
it('shows each option with its count', () => {
renderPanel();
expect(screen.getByRole('checkbox', { name: 'Rock 7 albums' })).toBeInTheDocument();
expect(screen.getByRole('checkbox', { name: '1970s 3 albums' })).toBeInTheDocument();
expect(
screen.getByRole('checkbox', { name: 'Has reviews 2 albums' }),
).toBeInTheDocument();
});

it('hides options that would return nothing', () => {
renderPanel({ reviewedCount: 0 });
expect(screen.queryByRole('checkbox', { name: /Jazz/ })).not.toBeInTheDocument();
expect(screen.queryByRole('checkbox', { name: /1990s/ })).not.toBeInTheDocument();
expect(screen.queryByRole('group', { name: 'Reviews' })).not.toBeInTheDocument();
});

it('keeps a selected option visible so it can be unticked', () => {
renderPanel({ decades: [1990] });
expect(screen.getByRole('checkbox', { name: /1990s/ })).toBeChecked();
});

it('adds and removes values from the selection', () => {
const { props } = renderPanel({ genres: ['4'] });
fireEvent.click(screen.getByRole('checkbox', { name: /Rock/ }));
expect(props.onGenresChange).toHaveBeenCalledWith([]);
fireEvent.click(screen.getByRole('checkbox', { name: /1970s/ }));
expect(props.onDecadesChange).toHaveBeenCalledWith([1970]);
fireEvent.click(screen.getByRole('checkbox', { name: /Has reviews/ }));
expect(props.onReviewedChange).toHaveBeenCalledWith(true);
});

it('offers Clear filters only while a filter is active', () => {
renderPanel();
expect(screen.queryByRole('button', { name: 'Clear filters' })).toBeNull();
});

it('clears every filter', () => {
const { props } = renderPanel({ reviewed: true });
fireEvent.click(screen.getByRole('button', { name: 'Clear filters' }));
expect(props.onClear).toHaveBeenCalled();
});

it('has no axe violations', async () => {
const { container } = renderPanel({ genres: ['4'] });
const results = await axe.run(container, {
rules: { 'color-contrast': { enabled: false } },
});
expect(results.violations.map((violation) => violation.id)).toEqual([]);
});
});
Loading