Skip to content
Merged
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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Repository for team IT2810-H26-T19 in IT2810-H26.

**Crate** is an album catalog where users can search for music, sort and page
through the results, open an album for detail, rate and review it, and add it to
a personal wantlist ("crate"). Results can be filtered by genre, decade and
whether an album has reviews.
a personal wantlist ("crate"). Results can be filtered by genre, decade, artist
country and whether an album has reviews.

IT2810 (NTNU) Project 2. Group T19: `erikhfj`, `robertky`, `evenkkl`, `rachelks`.

Expand Down Expand Up @@ -176,7 +176,9 @@ pnpm db:import:musicbrainz
```

The fetch script identifies itself with a User-Agent and spaces requests to
respect MusicBrainz's rate limit. Review the generated snapshot before
respect MusicBrainz's rate limit. It looks up country for each unique credited
artist and stores the MusicBrainz artist ID with the country code. Artists with
no country remain unassigned. Review the generated snapshot before
committing it. MusicBrainz core data is CC0 1.0; genre associations are
user-contributed tag data under CC BY-NC-SA 3.0. The snapshot retains that
distinction and the MusicBrainz release-group IDs for attribution and
Expand Down Expand Up @@ -319,7 +321,7 @@ The two `AlbumDetailPage` rows represent the separate chunks for the album detai

## Implementation status

**Search and filtering:** Users can search for albums by title or artist using Search or Enter. Results can be sorted by relevance, title, release year or average rating, and filtered by genre, decade or whether an album has reviews. Search terms, sorting and filters are preserved in the URL.
**Search and filtering:** Users can search for albums by title or artist using Search or Enter. Results can be sorted by relevance, title, release year or average rating, and filtered by genre, decade, artist country or whether an album has reviews. Search terms, sorting and filters are preserved in the URL.

**Results and pagination:** Searches initially display up to 20 albums. Additional results load automatically as the user scrolls, up to 60 albums. Load more is also available before this limit; further pages require the button. The interface provides loading, error and no-results feedback.

Expand Down
3 changes: 3 additions & 0 deletions client/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function mockSearch(
totalCount: albums.length,
genreFacets: [],
decadeFacets: [],
countryFacets: [],
reviewedCount: 0,
pageInfo: { hasNextPage: false, endCursor: null },
},
Expand Down Expand Up @@ -339,6 +340,7 @@ describe('App search', () => {
totalCount: 0,
genreFacets: [],
decadeFacets: [],
countryFacets: [],
reviewedCount: 0,
pageInfo: { hasNextPage: false, endCursor: null },
},
Expand Down Expand Up @@ -366,6 +368,7 @@ describe('App search', () => {
totalCount: 1,
genreFacets: [],
decadeFacets: [],
countryFacets: [],
reviewedCount: 0,
pageInfo: { hasNextPage: false, endCursor: '1' },
},
Expand Down
3 changes: 3 additions & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ function DiscoveryPage() {
filters={{
genres: search.filters.genres,
decades: search.filters.decades,
countries: search.filters.countries,
reviewed: search.filters.reviewed,
genreFacets: search.facets.genres,
decadeFacets: search.facets.decades,
countryFacets: search.facets.countries,
reviewedCount: search.facets.reviewedCount,
onGenresChange: search.setGenres,
onDecadesChange: search.setDecades,
onCountriesChange: search.setCountries,
onReviewedChange: search.setReviewed,
onClear: search.clearFilters,
}}
Expand Down
1 change: 1 addition & 0 deletions client/src/a11y.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function mockSearch(
totalCount: albums.length,
genreFacets: [],
decadeFacets: [],
countryFacets: [],
reviewedCount: 0,
pageInfo: { hasNextPage: false, endCursor: null },
},
Expand Down
1 change: 1 addition & 0 deletions client/src/api/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function result(ids: string[], endCursor: string, hasNextPage = true): SearchAlb
totalCount: 4,
genreFacets: [],
decadeFacets: [],
countryFacets: [],
reviewedCount: 0,
},
};
Expand Down
6 changes: 6 additions & 0 deletions client/src/api/searchAlbums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type AlbumConnection = SearchAlbumsQuery['searchAlbums'];
export type AlbumSummary = AlbumConnection['edges'][number]['node'];
export type GenreFacet = AlbumConnection['genreFacets'][number];
export type DecadeFacet = AlbumConnection['decadeFacets'][number];
export type CountryFacet = AlbumConnection['countryFacets'][number];
export type SearchAlbumsData = SearchAlbumsQuery;
export type SearchAlbumsVariables = SearchAlbumsQueryVariables;

Expand Down Expand Up @@ -68,6 +69,11 @@ export const SEARCH_ALBUMS = graphql(`
decade
count
}
countryFacets {
countryCode
countryName
count
}
reviewedCount
pageInfo {
hasNextPage
Expand Down
30 changes: 29 additions & 1 deletion client/src/components/FilterPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function renderPanel(over: Partial<Props> = {}, expanded = true) {
const props: Props = {
genres: [],
decades: [],
countries: [],
reviewed: false,
genreFacets: [
{ genre: { id: '4', name: 'Rock' }, count: 7 },
Expand All @@ -19,9 +20,11 @@ function renderPanel(over: Partial<Props> = {}, expanded = true) {
{ decade: 1970, count: 3 },
{ decade: 1990, count: 0 },
],
countryFacets: [{ countryCode: 'GB', countryName: 'United Kingdom', count: 2 }],
reviewedCount: 2,
onGenresChange: vi.fn(),
onDecadesChange: vi.fn(),
onCountriesChange: vi.fn(),
onReviewedChange: vi.fn(),
onClear: vi.fn(),
...over,
Expand All @@ -36,6 +39,9 @@ describe('FilterPanel', () => {
renderPanel();
expect(screen.getByRole('checkbox', { name: 'Rock 7 albums' })).toBeInTheDocument();
expect(screen.getByRole('checkbox', { name: '1970s 3 albums' })).toBeInTheDocument();
expect(
screen.getByRole('checkbox', { name: 'United Kingdom 2 albums' }),
).toBeInTheDocument();
expect(
screen.getByRole('checkbox', { name: 'Has reviews 2 albums' }),
).toBeInTheDocument();
Expand All @@ -48,6 +54,23 @@ describe('FilterPanel', () => {
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: /United Kingdom/ }));
expect(props.onCountriesChange).toHaveBeenCalledWith(['GB']);
fireEvent.click(screen.getByRole('checkbox', { name: /Has reviews/ }));
expect(props.onReviewedChange).toHaveBeenCalledWith(true);
});

it('keeps Clear filters inactive when nothing is selected', () => {
renderPanel();
expect(screen.getByRole('button', { name: 'Clear filters' })).toHaveAttribute(
Expand Down Expand Up @@ -207,7 +230,12 @@ describe('FilterPanel', () => {
await user.keyboard('{Enter}');
await user.tab();
expect(screen.getByRole('button', { name: 'Clear filters' })).toHaveFocus();
for (const name of ['Rock 7 albums', 'Has reviews 2 albums', '1970s 3 albums']) {
for (const name of [
'Rock 7 albums',
'Has reviews 2 albums',
'United Kingdom 2 albums',
'1970s 3 albums',
]) {
await user.tab();
expect(screen.getByRole('checkbox', { name })).toHaveFocus();
}
Expand Down
61 changes: 57 additions & 4 deletions client/src/components/FilterPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import './FilterPanel.css';

import { useEffect, useRef, useState, type ReactNode } from 'react';

import type { DecadeFacet, GenreFacet } from '../api/searchAlbums.js';
import type { CountryFacet, DecadeFacet, GenreFacet } from '../api/searchAlbums.js';

interface Props {
children?: ReactNode;
genres: string[];
decades: number[];
countries: string[];
reviewed: boolean;
genreFacets: GenreFacet[];
decadeFacets: DecadeFacet[];
countryFacets: CountryFacet[];
reviewedCount: number;
onGenresChange: (genres: string[]) => void;
onDecadesChange: (decades: number[]) => void;
onCountriesChange: (countries: string[]) => void;
onReviewedChange: (reviewed: boolean) => void;
onClear: () => void;
}
Expand All @@ -28,6 +31,8 @@ function toggle<T>(list: T[], value: T): T[] {
* Filter checkboxes with the server's facet counts. Genres combine with AND, so
* a genre's count is what ticking it would leave. Decades combine with OR, so a
* decade's count is how many albums it has with the other filters applied.
* Country filters match any credited artist; country counts exclude their own
* dimension, and a collaboration can count under more than one country.
* Options that would give zero results are hidden, so the panel never leads to
* an empty page. A selected option always stays, so it can be unticked.
*
Expand All @@ -43,9 +48,15 @@ export default function FilterPanel(props: Props) {
const decades = props.decadeFacets
.filter(({ decade, count }) => count > 0 || props.decades.includes(decade))
.sort((a, b) => b.decade - a.decade);
const countries = props.countryFacets.filter(
({ countryCode, count }) => count > 0 || props.countries.includes(countryCode),
);
const showReviewed = props.reviewedCount > 0 || props.reviewed;
const activeCount =
props.genres.length + props.decades.length + (props.reviewed ? 1 : 0);
props.genres.length +
props.decades.length +
props.countries.length +
(props.reviewed ? 1 : 0);
const active = activeCount > 0;
const activeFilters = [
...props.genres.map((id) => {
Expand All @@ -66,6 +77,20 @@ export default function FilterPanel(props: Props) {
remove: () =>
props.onDecadesChange(props.decades.filter((value) => value !== decade)),
})),
...props.countries.map((countryCode) => {
const name =
props.countryFacets.find((facet) => facet.countryCode === countryCode)
?.countryName ?? countryCode;
return {
key: 'country-' + countryCode,
label: name,
removeLabel: 'Remove artist country filter: ' + name,
remove: () =>
props.onCountriesChange(
props.countries.filter((value) => value !== countryCode),
),
};
}),
...(props.reviewed
? [
{
Expand Down Expand Up @@ -148,7 +173,7 @@ export default function FilterPanel(props: Props) {
</button>
</div>
<div className="filter-groups">
{(genres.length > 0 || showReviewed) && (
{(genres.length > 0 || countries.length > 0 || showReviewed) && (
<div className="filter-column">
{genres.length > 0 && (
<fieldset className="filter-group--genres">
Expand Down Expand Up @@ -219,6 +244,34 @@ export default function FilterPanel(props: Props) {
</div>
</fieldset>
)}
{countries.length > 0 && (
<fieldset className="filter-group--countries">
<legend>Artist country</legend>
<div className="filter-list">
{countries.map(({ countryCode, countryName, count }) => (
<label key={countryCode}>
<input
type="checkbox"
checked={props.countries.includes(countryCode)}
onChange={(event) => {
if (
document.activeElement === event.currentTarget &&
!event.currentTarget.checked &&
count === 0
)
toggleRef.current?.focus();
props.onCountriesChange(toggle(props.countries, countryCode));
}}
/>
<span>{countryName}</span>{' '}
<span className="filter-count">
{count} <span className="visually-hidden">albums</span>
</span>
</label>
))}
</div>
</fieldset>
)}
</div>
)}
{decades.length > 0 && (
Expand Down Expand Up @@ -249,7 +302,7 @@ export default function FilterPanel(props: Props) {
</div>
</fieldset>
)}
{!genres.length && !decades.length && !showReviewed && (
{!genres.length && !decades.length && !countries.length && !showReviewed && (
<p className="filter-panel-message">
No filter options are available for these results.
</p>
Expand Down
Loading