Implement functionality to add shows to existing collections from suggestions
ci / build-backend (push) Successful in 1m40s
ci / build-frontend (push) Successful in 53s
ci / tests (push) Successful in 1m46s
ci / sonar (push) Successful in 6m47s

Added a new endpoint for adding shows to already assembled franchises based on the same suggestion key. Updated the CollectionSuggestion and CollectionSuggestionDto records to include a CollectionId for tracking existing collections. Enhanced the CollectionSuggestions component in the frontend to support this new functionality, allowing users to add missing parts to existing collections. Updated localization strings to reflect the new addition feature in both English and Russian, ensuring clarity for users. Adjusted tests to verify the correct behavior of the addition process and its integration with existing collection logic.
This commit is contained in:
Leonid Pershin
2026-07-28 14:49:27 +03:00
parent 6c85db2d60
commit ac41111fa0
12 changed files with 413 additions and 39 deletions
@@ -8,7 +8,7 @@ import { useApiError } from '@/shared/lib/use-api-error'
import { Badge } from '@/shared/ui/badge'
import { Button } from '@/shared/ui/button'
import { toast } from '@/shared/ui/toast-store'
import { createCollectionFromSuggestion, suggestCollections } from './api'
import { addShowsFromSuggestion, createCollectionFromSuggestion, suggestCollections } from './api'
/**
* Что стоит собрать во франшизу. Состав виден целиком до создания — по названиям иногда
@@ -31,13 +31,28 @@ export function CollectionSuggestions() {
queryFn: suggestCollections,
})
const invalidate = () => {
void queryClient.invalidateQueries({ queryKey: qk.collections.all })
void queryClient.invalidateQueries({ queryKey: qk.collections.suggestions })
void queryClient.invalidateQueries({ queryKey: qk.shows.all })
}
const create = useMutation({
mutationFn: (suggestion: CollectionSuggestionDto) =>
createCollectionFromSuggestion(suggestion.key),
onSuccess: () => {
toast.success(t('admin.collections.suggestions.created'))
void queryClient.invalidateQueries({ queryKey: qk.collections.all })
void queryClient.invalidateQueries({ queryKey: qk.shows.all })
invalidate()
},
onError,
})
/** Дополнение уже собранной франшизы: коллекцию сервер находит по тому же ключу предложения. */
const add = useMutation({
mutationFn: (suggestion: CollectionSuggestionDto) => addShowsFromSuggestion(suggestion.key),
onSuccess: (added) => {
toast.success(t('admin.collections.suggestions.added', { count: added }))
invalidate()
},
onError,
})
@@ -78,27 +93,49 @@ export function CollectionSuggestions() {
<li key={suggestion.key} className="flex flex-col gap-1 px-4 py-2">
<div className="flex flex-wrap items-center gap-2">
<span className="min-w-0 flex-1 truncate font-medium">{suggestion.name}</span>
{/* Дополнение и создание в одном списке: без пометки строки неотличимы, а
действия у них разные — одна заводит франшизу, другая правит собранную. */}
{suggestion.collectionId && (
<Badge variant="muted">
{t('admin.collections.suggestions.additionBadge')}
</Badge>
)}
<Badge variant={suggestion.source === 'Metadata' ? 'default' : 'muted'}>
{t(`admin.collections.suggestions.sources.${suggestion.source}`)}
</Badge>
<span className="text-muted-foreground">
{t('admin.collections.suggestions.parts', { count: suggestion.parts.length })}
</span>
{suggestion.alreadyExists ? (
<Badge variant="muted">{t('admin.collections.suggestions.exists')}</Badge>
) : (
{suggestion.collectionId ? (
<Button
size="sm"
variant="outline"
disabled={create.isPending}
onClick={() => create.mutate(suggestion)}
disabled={add.isPending}
onClick={() => add.mutate(suggestion)}
>
<Plus className="h-4 w-4" />
{t('common.create')}
{t('admin.collections.suggestions.add')}
</Button>
) : (
<>
{suggestion.alreadyExists ? (
<Badge variant="muted">{t('admin.collections.suggestions.exists')}</Badge>
) : (
<Button
size="sm"
variant="outline"
disabled={create.isPending}
onClick={() => create.mutate(suggestion)}
>
<Plus className="h-4 w-4" />
{t('common.create')}
</Button>
)}
</>
)}
</div>
{/* Состав в предлагаемом порядке: по нему и видно, промахнулась ли догадка. */}
{/* Состав в предлагаемом порядке: по нему и видно, промахнулась ли догадка.
У дополнения это только недостающие части — их и добавит кнопка. */}
<p className="text-xs text-muted-foreground">
{suggestion.parts
.map((part) => (part.year ? `${part.name} (${part.year})` : part.name))
@@ -61,6 +61,14 @@ export function createCollectionFromSuggestion(key: string) {
})
}
/** Дополняет уже собранную франшизу по тому же ключу предложения; возвращает, сколько добавлено. */
export function addShowsFromSuggestion(key: string) {
return apiRequest<number>('/admin/collections/suggestions/add', {
method: 'POST',
body: { key },
})
}
export function addCollectionShow(id: string, showId: string) {
return apiRequest<void>(`/admin/collections/${id}/shows`, { method: 'POST', body: { showId } })
}