From 4bb9cbac93bea4811515ae9dd73eae5512c0577c Mon Sep 17 00:00:00 2001 From: Robert Goniszewski Date: Fri, 15 Nov 2024 11:28:00 +0100 Subject: [PATCH] refactor(bookmarks): improve metadata handling and type definitions Signed-off-by: Robert Goniszewski --- .../EditBookmarkForm/EditBookmarkForm.svelte | 75 +++++++++++-------- src/lib/stores/edit-bookmark.store.ts | 1 + src/lib/types/Bookmark.type.ts | 15 +--- src/lib/types/Metadata.type.ts | 16 ++-- src/lib/utils/get-metadata.ts | 17 ++--- 5 files changed, 64 insertions(+), 60 deletions(-) diff --git a/src/lib/components/EditBookmarkForm/EditBookmarkForm.svelte b/src/lib/components/EditBookmarkForm/EditBookmarkForm.svelte index 9612778..9b36ea6 100644 --- a/src/lib/components/EditBookmarkForm/EditBookmarkForm.svelte +++ b/src/lib/components/EditBookmarkForm/EditBookmarkForm.svelte @@ -8,7 +8,7 @@ import SvelteSelect from 'svelte-select'; import { writable, type Writable } from 'svelte/store'; import { invalidate } from '$app/navigation'; -import { editBookmarkStore } from '$lib/stores/edit-bookmark.store'; +import { editBookmarkCategoriesStore, editBookmarkStore } from '$lib/stores/edit-bookmark.store'; import { searchEngine } from '$lib/stores/search.store'; import type { Bookmark, BookmarkEdit } from '$lib/types/Bookmark.type'; import { updateBookmarkInSearchIndex } from '$lib/utils/search'; @@ -19,14 +19,20 @@ export let closeModal: () => void; let error = ''; const loading = writable(false); -const bookmark = writable | BookmarkEdit>(); +const bookmark = writable>(); $: $bookmark = { ...$editBookmarkStore }; -const categoryItems = $page.data.categories.map((c) => ({ - value: `${c.id}`, - label: c.name -})); +const categoryItems = [ + ...$page.data.categories.map((c) => ({ + value: `${c.id}`, + label: c.name + })), + ...$editBookmarkCategoriesStore.map((c) => ({ + value: c, + label: c + })) +]; const bookmarkTagsInput: Writable< | { @@ -37,7 +43,14 @@ const bookmarkTagsInput: Writable< | null > = writable(null); -$: $bookmarkTagsInput = $bookmark.tags?.map((t) => ({ value: `${t.id}`, label: t.name })) || null; +$: $bookmarkTagsInput = + (isImportedBookmark($bookmark) + ? $bookmark.bookmarkTags?.map((t) => ({ + value: t.value, + label: t.label + })) + : ($bookmark as Partial).tags?.map((t) => ({ value: `${t.id}`, label: t.name }))) || + null; const bookmarkTags = writable< { @@ -92,19 +105,28 @@ function handleTagsChange() { ]; } -function handleImportedBookmarkEdit() { - const formData = new FormData(form); - let rawData = Object.fromEntries(formData as any); - console.log('rawData', rawData); - delete rawData.tags; - editBookmarkStore.set({ - ...$bookmark, - ...rawData, - category: JSON.parse(rawData.category)?.label, - bookmarkTags: $bookmarkTags - }); - console.log('$editBookmarkStore', $editBookmarkStore); - closeModal(); +function isImportedBookmark( + bookmark: BookmarkEdit | Partial +): bookmark is BookmarkEdit & { imported: boolean } { + return 'imported' in bookmark; +} + +function handleSubmit() { + if (isImportedBookmark($bookmark) && $bookmark.imported) { + const formData = new FormData(form); + let rawData = Object.fromEntries(formData as any); + console.log('rawData', rawData); + delete rawData.tags; + editBookmarkStore.set({ + ...$bookmark, + ...rawData, + category: JSON.parse(rawData.category)?.label, + bookmarkTags: $bookmarkTags + }); + closeModal(); + } else { + form.submit(); + } } const onGetMetadata = _.debounce( @@ -215,13 +237,13 @@ const onGetMetadata = _.debounce(
- {#if $bookmark.category?.id || $bookmark.category?.name} + {#if typeof $bookmark.category === 'string' || $bookmark.category?.id}