-
Notifications
You must be signed in to change notification settings - Fork 0
feat: #16 add personal crate controls #67
Merged
+393
−12
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } | ||
| } | ||
| `; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| .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; | ||
| } | ||
| .crate-button--saved { | ||
| background: var(--accent); | ||
| color: var(--accent-text); | ||
| } | ||
| .crate-button--saved:hover { | ||
| background: var(--accent-hover); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| 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) { | ||
| console.error('Could not update crate:', caught); | ||
| setError('Could not update your crate. Please try again.'); | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <span className="crate-button-wrap"> | ||
| <button | ||
| className={`secondary-button crate-button${isInCrate ? ' crate-button--saved' : ''}`} | ||
| type="button" | ||
| aria-pressed={isInCrate} | ||
| aria-label={isInCrate ? 'Remove from crate' : 'Add to crate'} | ||
| aria-busy={busy} | ||
| onClick={() => void toggle()} | ||
| disabled={busy} | ||
| > | ||
| {isInCrate ? 'In your crate' : 'Add to crate'} | ||
| </button> | ||
| {error && ( | ||
| <span className="crate-button__error" role="alert"> | ||
| {error} | ||
| </span> | ||
| )} | ||
| </span> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.