1
0

fix: dispatch ADD_START, correlate preview/lookup responses with the current attempt

This commit is contained in:
2026-08-29 22:54:00 +02:00
parent c76d936f1e
commit b9ba4f0ca6
4 changed files with 85 additions and 33 deletions

View File

@@ -30,7 +30,7 @@ export default function ScanPage() {
dispatch({ type: 'CANDIDATES', code, candidates })
}
} catch (err) {
dispatch({ type: 'ERROR', kind: toErrorKind(err), code })
dispatch({ type: 'ERROR', kind: toErrorKind(err), code, source: 'lookup' })
}
})()
}, [])
@@ -42,9 +42,9 @@ export default function ScanPage() {
void (async () => {
try {
const preview = await api.getReleasePreview(candidateId)
dispatch({ type: 'PREVIEW', preview })
dispatch({ type: 'PREVIEW', preview, candidateId })
} catch (err) {
dispatch({ type: 'ERROR', kind: toErrorKind(err), code: null })
dispatch({ type: 'ERROR', kind: toErrorKind(err), code: null, source: 'preview', candidateId })
}
})()
}, [state])
@@ -52,6 +52,7 @@ export default function ScanPage() {
const add = useCallback(() => {
if (state.phase !== 'confirm' || !state.preview || state.adding) return
const { candidate, code, matchAlbumId } = state
dispatch({ type: 'ADD_START' })
void (async () => {
try {
const item = await api.addToCollection({

View File

@@ -24,9 +24,9 @@ export type ScanAction =
| { type: 'DETECT'; code: string }
| { type: 'CANDIDATES'; code: string; candidates: Candidate[] }
| { type: 'NOT_FOUND'; code: string }
| { type: 'ERROR'; kind: ScanErrorKind; code: string | null }
| { type: 'ERROR'; kind: ScanErrorKind; code: string | null; source: 'lookup' | 'preview'; candidateId?: number }
| { type: 'SELECT'; candidate: Candidate }
| { type: 'PREVIEW'; preview: ReleasePreview }
| { type: 'PREVIEW'; preview: ReleasePreview; candidateId: number }
| { type: 'SET_MATCH'; albumId: number }
| { type: 'CLEAR_MATCH' }
| { type: 'ADD_START' }
@@ -43,7 +43,10 @@ export function scanReducer(state: ScanState, action: ScanAction): ScanState {
case 'NOT_FOUND':
return state.phase === 'looking' ? { phase: 'error', kind: 'not_found', code: action.code } : state
case 'ERROR':
return state.phase === 'looking' || state.phase === 'candidates' || state.phase === 'confirm'
if (action.source === 'lookup') {
return state.phase === 'looking' ? { phase: 'error', kind: action.kind, code: action.code } : state
}
return state.phase === 'confirm' && state.candidate.id === action.candidateId
? { phase: 'error', kind: action.kind, code: action.code }
: state
case 'SELECT':
@@ -59,7 +62,9 @@ export function scanReducer(state: ScanState, action: ScanAction): ScanState {
}
: state
case 'PREVIEW':
return state.phase === 'confirm' ? { ...state, preview: action.preview } : state
return state.phase === 'confirm' && state.candidate.id === action.candidateId
? { ...state, preview: action.preview }
: state
case 'SET_MATCH':
return state.phase === 'confirm' ? { ...state, matchAlbumId: action.albumId } : state
case 'CLEAR_MATCH':