mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
fix(packing): apply templates into the active list tab (#1581)
This commit is contained in:
committed by
GitHub
parent
2e05126c6a
commit
5e686eac06
@@ -13,13 +13,14 @@ interface Template {
|
||||
|
||||
interface ApplyTemplateButtonProps {
|
||||
tripId: number
|
||||
visibility: 'common' | 'personal'
|
||||
style: React.CSSProperties
|
||||
className?: string
|
||||
}
|
||||
|
||||
// Dropdown-Button um ein Packing-Template auf den aktuellen Trip anzuwenden.
|
||||
// Rendert nichts wenn keine Templates existieren.
|
||||
export default function ApplyTemplateButton({ tripId, style, className }: ApplyTemplateButtonProps): React.ReactElement | null {
|
||||
export default function ApplyTemplateButton({ tripId, visibility, style, className }: ApplyTemplateButtonProps): React.ReactElement | null {
|
||||
const [templates, setTemplates] = useState<Template[]>([])
|
||||
const [open, setOpen] = useState(false)
|
||||
const [applying, setApplying] = useState(false)
|
||||
@@ -43,7 +44,7 @@ export default function ApplyTemplateButton({ tripId, style, className }: ApplyT
|
||||
const handleApply = async (templateId: number) => {
|
||||
setApplying(true)
|
||||
try {
|
||||
const data = await packingApi.applyTemplate(tripId, templateId)
|
||||
const data = await packingApi.applyTemplate(tripId, templateId, visibility)
|
||||
useTripStore.setState(s => ({ packingItems: [...s.packingItems, ...(data.items || [])] }))
|
||||
toast.success(t('packing.templateApplied', { count: data.count }))
|
||||
setOpen(false)
|
||||
|
||||
@@ -1159,6 +1159,48 @@ describe('PackingListPanel', () => {
|
||||
await waitFor(() => expect(applyCalled).toBe(true));
|
||||
});
|
||||
|
||||
// #1565: the template used to always land in the shared pool, whichever tab was open.
|
||||
it('FE-COMP-PACKING-061a: applying a template from "My List" sends the personal view', async () => {
|
||||
const user = userEvent.setup();
|
||||
let applyBody: Record<string, unknown> | null = null;
|
||||
server.use(
|
||||
http.get('/api/trips/:id/packing/templates', () =>
|
||||
HttpResponse.json({ templates: [{ id: 5, name: 'Beach Trip', item_count: 12 }] })
|
||||
),
|
||||
http.post('/api/trips/1/packing/apply-template/5', async ({ request }) => {
|
||||
applyBody = (await request.json()) as Record<string, unknown>;
|
||||
return HttpResponse.json({ items: [], count: 12 });
|
||||
})
|
||||
);
|
||||
render(<PackingListPanel tripId={1} items={[]} />);
|
||||
|
||||
await user.click(await screen.findByText('My list'));
|
||||
await user.click(await screen.findByText('Apply template'));
|
||||
await user.click(await screen.findByText('Beach Trip'));
|
||||
|
||||
await waitFor(() => expect(applyBody).toEqual({ visibility: 'personal' }));
|
||||
});
|
||||
|
||||
it('FE-COMP-PACKING-061b: applying a template from the shared tab sends the common view', async () => {
|
||||
const user = userEvent.setup();
|
||||
let applyBody: Record<string, unknown> | null = null;
|
||||
server.use(
|
||||
http.get('/api/trips/:id/packing/templates', () =>
|
||||
HttpResponse.json({ templates: [{ id: 5, name: 'Beach Trip', item_count: 12 }] })
|
||||
),
|
||||
http.post('/api/trips/1/packing/apply-template/5', async ({ request }) => {
|
||||
applyBody = (await request.json()) as Record<string, unknown>;
|
||||
return HttpResponse.json({ items: [], count: 12 });
|
||||
})
|
||||
);
|
||||
render(<PackingListPanel tripId={1} items={[]} />);
|
||||
|
||||
await user.click(await screen.findByText('Apply template'));
|
||||
await user.click(await screen.findByText('Beach Trip'));
|
||||
|
||||
await waitFor(() => expect(applyBody).toEqual({ visibility: 'common' }));
|
||||
});
|
||||
|
||||
it('FE-COMP-PACKING-062: handleBulkImport calls import API and closes modal', async () => {
|
||||
const user = userEvent.setup();
|
||||
let importBody: Record<string, unknown> | null = null;
|
||||
|
||||
@@ -33,6 +33,9 @@ export interface PackingListPanelProps {
|
||||
clearCheckedSignal?: number
|
||||
saveTemplateSignal?: number
|
||||
inlineHeader?: boolean
|
||||
// Lifted so an out-of-panel Apply Template button knows the active view (#1565).
|
||||
view?: 'common' | 'personal'
|
||||
onViewChange?: (view: 'common' | 'personal') => void
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,11 +45,13 @@ export interface PackingListPanelProps {
|
||||
* sections below render header, filters, the grouped list, the bag sidebar/
|
||||
* modal and the import dialog.
|
||||
*/
|
||||
export function usePackingList({ tripId, items, openImportSignal = 0, clearCheckedSignal = 0, saveTemplateSignal = 0, inlineHeader = true }: PackingListPanelProps) {
|
||||
export function usePackingList({ tripId, items, openImportSignal = 0, clearCheckedSignal = 0, saveTemplateSignal = 0, inlineHeader = true, view: viewProp, onViewChange }: PackingListPanelProps) {
|
||||
const [filter, setFilter] = useState('alle') // 'alle' | 'offen' | 'erledigt'
|
||||
// Three-tier sharing (#858): 'common' = the group pool (where existing items
|
||||
// live — non-breaking), 'personal' = my own list (private + shared-to-me).
|
||||
const [view, setView] = useState<'common' | 'personal'>('common')
|
||||
const [ownView, setOwnView] = useState<'common' | 'personal'>('common')
|
||||
const view = viewProp ?? ownView
|
||||
const setView = onViewChange ?? setOwnView
|
||||
const [addingCategory, setAddingCategory] = useState(false)
|
||||
const [newCatName, setNewCatName] = useState('')
|
||||
const { addPackingItem, updatePackingItem, deletePackingItem, togglePackingItem, reorderPackingItems,
|
||||
@@ -307,7 +312,7 @@ export function usePackingList({ tripId, items, openImportSignal = 0, clearCheck
|
||||
const handleApplyTemplate = async (templateId: number) => {
|
||||
setApplyingTemplate(true)
|
||||
try {
|
||||
const data = await packingApi.applyTemplate(tripId, templateId)
|
||||
const data = await packingApi.applyTemplate(tripId, templateId, view)
|
||||
useTripStore.setState(s => ({ packingItems: [...s.packingItems, ...(data.items || [])] }))
|
||||
toast.success(t('packing.templateApplied', { count: data.count }))
|
||||
setShowTemplateDropdown(false)
|
||||
|
||||
Reference in New Issue
Block a user