Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions client/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const album: AlbumSummary = {
genres: [{ id: '1', name: 'Rock' }],
averageRating: 4.8,
reviewCount: 1240,
isInCrate: false,
};

function mockSearch(
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 @@ -22,6 +22,7 @@ const album: AlbumSummary = {
genres: [{ id: '1', name: 'Rock' }],
averageRating: 4.8,
reviewCount: 1240,
isInCrate: false,
};

function mockSearch(
Expand Down
7 changes: 7 additions & 0 deletions client/src/api/albumDetail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { buildSchema, validate } from 'graphql';
import { expect, it } from 'vitest';

import { ALBUM_DETAIL } from './albumDetail.js';
import { ADD_TO_CRATE, REMOVE_FROM_CRATE } from './crate.js';
import { DELETE_REVIEW, UPSERT_REVIEW } from './reviews.js';

it('validates the album-detail operation against the repository schema', () => {
Expand All @@ -14,3 +15,9 @@ it('validates review mutations against the repository schema', () => {
expect(validate(schema, UPSERT_REVIEW)).toEqual([]);
expect(validate(schema, DELETE_REVIEW)).toEqual([]);
});

it('validates crate mutations against the repository schema', () => {
const schema = buildSchema(schemaSource);
expect(validate(schema, ADD_TO_CRATE)).toEqual([]);
expect(validate(schema, REMOVE_FROM_CRATE)).toEqual([]);
});
2 changes: 2 additions & 0 deletions client/src/api/albumDetail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type AlbumDetail = {
}[];
averageRating: number | null;
reviewCount: number;
isInCrate: boolean;
reviews: {
edges: {
cursor: string;
Expand Down Expand Up @@ -61,6 +62,7 @@ export const ALBUM_DETAIL: TypedDocumentNode<AlbumDetailData, AlbumDetailVariabl
}
averageRating
reviewCount
isInCrate
myReview {
id
rating
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 @@ -28,6 +28,7 @@ function result(ids: string[], endCursor: string, hasNextPage = true): SearchAlb
releaseYear: null,
averageRating: null,
reviewCount: 0,
isInCrate: false,
},
})),
pageInfo: { endCursor, hasNextPage },
Expand Down
40 changes: 40 additions & 0 deletions client/src/api/crate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { gql } from '@apollo/client';
import type { TypedDocumentNode } from '@apollo/client';

export type AddToCrateData = {
addToCrate: { item: { id: string }; album: { id: string; isInCrate: boolean } };
};
export type RemoveFromCrateData = {
removeFromCrate: {
removedAlbumId: string;
album: { id: string; isInCrate: boolean };
};
};
export type CrateVariables = { albumId: string };

export const ADD_TO_CRATE: TypedDocumentNode<AddToCrateData, CrateVariables> = gql`
mutation AddToCrate($albumId: ID!) {
addToCrate(albumId: $albumId) {
item {
id
}
album {
id
isInCrate
}
}
}
`;

export const REMOVE_FROM_CRATE: TypedDocumentNode<RemoveFromCrateData, CrateVariables> =
gql`
mutation RemoveFromCrate($albumId: ID!) {
removeFromCrate(albumId: $albumId) {
removedAlbumId
album {
id
isInCrate
}
}
}
`;
2 changes: 2 additions & 0 deletions client/src/api/searchAlbums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface AlbumSummary {
genres: { id: string; name: string }[];
averageRating: number | null;
reviewCount: number;
isInCrate: boolean;
}

export interface AlbumConnection {
Expand Down Expand Up @@ -67,6 +68,7 @@ export const SEARCH_ALBUMS: TypedDocumentNode<SearchAlbumsData, SearchAlbumsVari
}
averageRating
reviewCount
isInCrate
}
}
totalCount
Expand Down
26 changes: 15 additions & 11 deletions client/src/components/AlbumCard.test.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { MockedProvider } from '@apollo/client/testing/react';
import { it, expect } from 'vitest';
import { render, screen } from '@testing-library/react';
import AlbumCard from './AlbumCard.js';

it('handles nullable summary fields without inventing a rating or year', () => {
window.history.replaceState({}, '', '/?q=Abbey&sort=TITLE_ASC');
render(
<AlbumCard
album={{
id: '1',
title: 'Abbey Road',
artists: [{ id: '1', name: 'The Beatles' }],
genres: [{ id: '1', name: 'Rock' }],
releaseYear: null,
averageRating: null,
reviewCount: 0,
}}
/>,
<MockedProvider>
<AlbumCard
album={{
id: '1',
title: 'Abbey Road',
artists: [{ id: '1', name: 'The Beatles' }],
genres: [{ id: '1', name: 'Rock' }],
releaseYear: null,
averageRating: null,
reviewCount: 0,
isInCrate: false,
}}
/>
</MockedProvider>,
);
expect(screen.getByText('Year unknown')).toBeInTheDocument();
expect(screen.getByText('Not yet rated')).toBeInTheDocument();
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/AlbumCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import './AlbumCard.css';

import type { AlbumSummary } from '../api/searchAlbums.js';
import CrateButton from './CrateButton.js';

export default function AlbumCard({ album }: { album: AlbumSummary }) {
const detailHref = `${import.meta.env.BASE_URL}album/${album.id}${window.location.search}`;
Expand All @@ -13,6 +14,7 @@ export default function AlbumCard({ album }: { album: AlbumSummary }) {
</h3>
<p className="artist">{album.artists.map((artist) => artist.name).join(', ')}</p>
<p className="release-year">{album.releaseYear ?? 'Year unknown'}</p>
<CrateButton albumId={album.id} isInCrate={album.isInCrate} />
<ul className="genre-tags" aria-label="Genres">
{album.genres.map((genre) => (
<li key={genre.id}>{genre.name}</li>
Expand Down
10 changes: 10 additions & 0 deletions client/src/components/CrateButton.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.crate-button-wrap {
display: grid;
justify-items: start;
gap: 0.35rem;
margin-top: 0.5rem;
}
.crate-button__error {
color: var(--danger, #a32626);
font-size: 0.85rem;
}
88 changes: 88 additions & 0 deletions client/src/components/CrateButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { gql } from '@apollo/client';
import { MockedProvider } from '@apollo/client/testing/react';
import { useQuery } from '@apollo/client/react';
import { fireEvent, render, screen } from '@testing-library/react';
import { expect, it } from 'vitest';

import { ADD_TO_CRATE, REMOVE_FROM_CRATE } from '../api/crate.js';
import CrateButton from './CrateButton.js';

const GET_ALBUM_CRATE_STATE = gql`
query AlbumCrateState($id: ID!) {
album(id: $id) {
id
isInCrate
}
}
`;

function CrateViews() {
const { data } = useQuery<{
album: { id: string; isInCrate: boolean } | null;
}>(GET_ALBUM_CRATE_STATE, { variables: { id: '1' } });
if (!data?.album) return <p role="status">Loading</p>;

return (
<>
<div aria-label="Album list">
<CrateButton albumId={data.album.id} isInCrate={data.album.isInCrate} />
</div>
<div aria-label="Album details">
<CrateButton albumId={data.album.id} isInCrate={data.album.isInCrate} />
</div>
</>
);
}

it('keeps list and detail crate controls in sync after add and remove', async () => {
render(
<MockedProvider
mocks={[
{
request: { query: GET_ALBUM_CRATE_STATE, variables: { id: '1' } },
result: { data: { album: { __typename: 'Album', id: '1', isInCrate: false } } },
},
{
request: { query: ADD_TO_CRATE, variables: { albumId: '1' } },
result: {
data: {
addToCrate: {
item: { __typename: 'CrateItem', id: '1' },
album: { __typename: 'Album', id: '1', isInCrate: true },
},
},
},
},
{
request: { query: REMOVE_FROM_CRATE, variables: { albumId: '1' } },
result: {
data: {
removeFromCrate: {
removedAlbumId: '1',
album: { __typename: 'Album', id: '1', isInCrate: false },
},
},
},
},
]}
>
<CrateViews />
</MockedProvider>,
);

const buttons = await screen.findAllByRole('button', { name: 'Add to crate' });
fireEvent.click(buttons[0]!);

const addedButtons = await screen.findAllByRole('button', {
name: 'Remove from crate',
});
expect(addedButtons).toHaveLength(2);
expect(addedButtons[0]).toHaveAttribute('aria-pressed', 'true');
expect(addedButtons[1]).toHaveAttribute('aria-pressed', 'true');

fireEvent.click(addedButtons[1]!);
const removedButtons = await screen.findAllByRole('button', { name: 'Add to crate' });
expect(removedButtons).toHaveLength(2);
expect(removedButtons[0]).toHaveAttribute('aria-pressed', 'false');
expect(removedButtons[1]).toHaveAttribute('aria-pressed', 'false');
});
62 changes: 62 additions & 0 deletions client/src/components/CrateButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import './CrateButton.css';

import { useState } from 'react';
import { useMutation } from '@apollo/client/react';

import { ADD_TO_CRATE, REMOVE_FROM_CRATE } from '../api/crate.js';

type Props = { albumId: string; isInCrate: boolean };

export default function CrateButton({ albumId, isInCrate }: Props) {
const [error, setError] = useState<string | null>(null);
const [addToCrate, addState] = useMutation(ADD_TO_CRATE, {
update(cache, { data }) {
const savedAlbum = data?.addToCrate.album;
if (!savedAlbum) return;
const cacheId = cache.identify({ __typename: 'Album', id: savedAlbum.id });
if (cacheId) cache.modify({ id: cacheId, fields: { isInCrate: () => true } });
},
});
const [removeFromCrate, removeState] = useMutation(REMOVE_FROM_CRATE, {
update(cache, { data }) {
const savedAlbum = data?.removeFromCrate.album;
if (!savedAlbum) return;
const cacheId = cache.identify({ __typename: 'Album', id: savedAlbum.id });
if (cacheId) cache.modify({ id: cacheId, fields: { isInCrate: () => false } });
},
});
const busy = addState.loading || removeState.loading;

async function toggle() {
setError(null);
try {
if (isInCrate) {
await removeFromCrate({ variables: { albumId } });
} else {
await addToCrate({ variables: { albumId } });
}
} catch (caught) {
setError(caught instanceof Error ? caught.message : 'Could not update your crate.');
}
}

return (
<span className="crate-button-wrap">
<button
className="secondary-button crate-button"
type="button"
aria-pressed={isInCrate}
aria-label={isInCrate ? 'Remove from crate' : 'Add to crate'}
onClick={() => void toggle()}
disabled={busy}
>
{busy ? 'Updating…' : isInCrate ? 'In your crate' : 'Add to crate'}
</button>
{error && (
<span className="crate-button__error" role="alert">
{error}
</span>
)}
</span>
);
}
1 change: 1 addition & 0 deletions client/src/pages/AlbumDetailPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const detail = {
],
averageRating: 4.5,
reviewCount: 1,
isInCrate: false,
myReview: null,
reviews: {
edges: [
Expand Down
2 changes: 2 additions & 0 deletions client/src/pages/AlbumDetailPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useQuery } from '@apollo/client/react';

import { ALBUM_DETAIL } from '../api/albumDetail.js';
import ReviewForm from '../components/ReviewForm.js';
import CrateButton from '../components/CrateButton.js';

const REVIEW_PAGE_SIZE = 10;

Expand Down Expand Up @@ -95,6 +96,7 @@ export default function AlbumDetailPage({ id }: { id: string }) {
{album.artists.map((artist) => artist.name).join(', ')}
</p>
<p className="muted">{album.releaseYear ?? 'Release year unknown'}</p>
<CrateButton albumId={album.id} isInCrate={album.isInCrate} />
<ul className="genre-tags" aria-label="Genres">
{album.genres.map((genre) => (
<li key={genre.id}>{genre.name}</li>
Expand Down
4 changes: 4 additions & 0 deletions client/src/pagination.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function page(
releaseYear: null,
averageRating: null,
reviewCount: 0,
isInCrate: false,
},
})),
totalCount: 40,
Expand Down Expand Up @@ -272,6 +273,9 @@ describe('cursor pagination', () => {
await user.tab();
expect(screen.getByRole('link', { name: 'Album 1' })).toHaveFocus();

await user.tab();
expect(screen.getByRole('button', { name: 'Add to crate' })).toHaveFocus();

await user.tab();
expect(button).toHaveFocus();

Expand Down
1 change: 1 addition & 0 deletions docs/AI-USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ the part that matters most.
| 2026-10-23 | rachelks | Codex (ChatGPT) | Assisted with implementing and testing prefix search (#55), performance testing with 100,000 albums, and updating documentation. |
| 2026-10-24 | rachelks | Codex (ChatGPT) | Assisted with implementing and testing cursor pagination, infinite scroll, Load more, Apollo cache handling, and accessibility improvements (#14). |
| 2026-10-25 | rachelks | ChatGPT | Assisted with researching real album datasets, evaluating which data source best fit the project, and planning issues #63 and #64 to implement real data. |
| 2026-10-26 | robertky | Codex (ChatGPT) | Assisted me when I implemented #16: idempotent add/remove mutations, per-request crate-state loading, and accessible list/detail toggles. I reviewed the implementation and test results before using it. |

## Notes

Expand Down
Loading