feat(04-03): install Svelecte and add tag support to load/actions
- Install svelecte package for tag input with autocomplete - Import tagRepository in +page.server.ts - Attach tags to entries in load function alongside images - Return allTags for autocomplete suggestions - Add updateTags action to update entry tags via JSON payload
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { PageServerLoad, Actions } from './$types';
|
||||
import { fail } from '@sveltejs/kit';
|
||||
import { entryRepository, imageRepository } from '$lib/server/db/repository';
|
||||
import { entryRepository, imageRepository, tagRepository } from '$lib/server/db/repository';
|
||||
import {
|
||||
saveOriginal,
|
||||
saveThumbnail,
|
||||
@@ -13,14 +13,19 @@ export const load: PageServerLoad = async ({ url }) => {
|
||||
const showCompleted = url.searchParams.get('showCompleted') === 'true';
|
||||
const entries = entryRepository.getOrdered({ showCompleted });
|
||||
|
||||
// Attach images to each entry
|
||||
const entriesWithImages = entries.map((entry) => ({
|
||||
// Attach images AND tags to each entry
|
||||
const entriesWithData = entries.map((entry) => ({
|
||||
...entry,
|
||||
images: imageRepository.getByEntryId(entry.id)
|
||||
images: imageRepository.getByEntryId(entry.id),
|
||||
tags: tagRepository.getByEntryId(entry.id)
|
||||
}));
|
||||
|
||||
// Get all tags for autocomplete
|
||||
const allTags = tagRepository.getAll();
|
||||
|
||||
return {
|
||||
entries: entriesWithImages,
|
||||
entries: entriesWithData,
|
||||
allTags,
|
||||
showCompleted
|
||||
};
|
||||
};
|
||||
@@ -260,5 +265,28 @@ export const actions: Actions = {
|
||||
entryRepository.update(id, { dueDate: dueDate || null });
|
||||
|
||||
return { success: true };
|
||||
},
|
||||
|
||||
updateTags: async ({ request }) => {
|
||||
const formData = await request.formData();
|
||||
const id = formData.get('id')?.toString();
|
||||
const tagsJson = formData.get('tags')?.toString() || '[]';
|
||||
|
||||
if (!id) {
|
||||
return fail(400, { error: 'Entry ID is required' });
|
||||
}
|
||||
|
||||
const existing = entryRepository.getById(id);
|
||||
if (!existing) {
|
||||
return fail(404, { error: 'Entry not found' });
|
||||
}
|
||||
|
||||
try {
|
||||
const tagNames = JSON.parse(tagsJson) as string[];
|
||||
tagRepository.updateEntryTags(id, tagNames);
|
||||
return { success: true };
|
||||
} catch {
|
||||
return fail(400, { error: 'Invalid tags format' });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user