From adf6ba3eb54b2a900e482f689a7550a9afb13cfc Mon Sep 17 00:00:00 2001 From: Maddy Guthridge Date: Mon, 2 Sep 2024 16:03:59 +1000 Subject: [PATCH] Implement editing for item page --- src/components/chip/ItemChipList.svelte | 6 +- src/routes/[group]/[item]/+page.svelte | 153 +++++++++++++++++++----- 2 files changed, 128 insertions(+), 31 deletions(-) diff --git a/src/components/chip/ItemChipList.svelte b/src/components/chip/ItemChipList.svelte index 2e944ce5..3a9731b9 100644 --- a/src/components/chip/ItemChipList.svelte +++ b/src/components/chip/ItemChipList.svelte @@ -93,6 +93,7 @@ const dispatch = createEventDispatcher<{ filter: FilterOptions, + click: { groupId: string, itemId: string } }>(); // Update filter status @@ -117,7 +118,10 @@ itemId={filterItem.itemId} selected={filterItem.selected} {link} - on:click={() => updateFilterStatus(outer, inner)} + on:click={() => { + updateFilterStatus(outer, inner); + dispatch('click', { groupId: filterItem.groupId, itemId: filterItem.itemId }); + }} /> {/each} diff --git a/src/routes/[group]/[item]/+page.svelte b/src/routes/[group]/[item]/+page.svelte index 75afc0a4..a7e4ecd5 100644 --- a/src/routes/[group]/[item]/+page.svelte +++ b/src/routes/[group]/[item]/+page.svelte @@ -6,17 +6,83 @@ import Paper from '$components/Paper.svelte'; import EditableMarkdown from '$components/markdown'; import api from '$endpoints'; - import consts from '$lib/consts'; - import { generateKeywords } from '$lib/seo'; + import consts from '$lib/consts'; + import { generateKeywords } from '$lib/seo'; + import EditControls from '$components/EditControls.svelte'; // import AsciinemaPlayer from "$components"; export let data: import('./$types').PageData; - $: groupData = data.globals.groups[data.groupId]; - $: itemData = data.globals.items[data.groupId][data.itemId]; + let groupData = data.globals.groups[data.groupId]; + let itemData = data.globals.items[data.groupId][data.itemId]; + + let editing = false; + + let readme = ''; + + let chipLinks = itemData.info.links.filter(([l]) => l.style === 'chip'); + let cardLinks = itemData.info.links.filter(([l]) => l.style === 'card'); + + function beginEditing() { + editing = true; + } + + async function finishEditing(save: boolean) { + editing = false; + if (save) { + itemData.readme = readme; + await api().group.withId(data.groupId).item.withId(data.itemId).readme.set(readme); + // Update other info + await api().group.withId(data.groupId).item.withId(data.itemId).info.set(itemData.info); + } + // Reset data + setupData(data.groupId, data.itemId); + } + + function setupData(groupId: string, itemId: string) { + editing = false; + groupData = data.globals.groups[groupId]; + itemData = data.globals.items[groupId][itemId]; + readme = itemData.readme; + chipLinks = itemData.info.links.filter(([l]) => l.style === 'chip'); + cardLinks = itemData.info.links.filter(([l]) => l.style === 'card'); + } + + function changeLinkTitle(linkedGroup: string, newTitle: string) { + const linkInfo = itemData.info.links.find(link => link[0].groupId === linkedGroup); + if (!linkInfo) { + // This shouldn't happen + throw Error(`changeLinkTitle: linkInfo not found for ${linkedGroup}`); + } + linkInfo[0].title = newTitle; + } + + async function toggleLink(linkedGroup: string, linkedItem: string) { + const linkInfo = itemData.info.links.find(link => link[0].groupId === linkedGroup); + if (!linkInfo) { + // This shouldn't happen + throw Error(`toggleLink: linkInfo not found for ${linkedGroup}/${linkedItem}`); + } + const newState = !linkInfo[1].includes(linkedItem); + if (newState) { + // Create link + await api().group.withId(data.groupId).item.withId(data.itemId).links.create(linkedGroup, linkedItem); + // Update state in browser to match + linkInfo[1].push(linkedItem); + } else { + // Unlink + await api().group.withId(data.groupId).item.withId(data.itemId).links.remove(linkedGroup, linkedItem); + linkInfo[1] = linkInfo[1].filter(item => item !== linkedItem); + } + // Force update chip and card links + chipLinks = [...chipLinks]; + cardLinks = [...cardLinks]; + } + + $: setupData(data.groupId, data.itemId); - + {itemData.info.name} - {groupData.info.name} - {data.globals.config.siteShortName} @@ -37,22 +103,23 @@ />
- - {#if itemData.info.banner} - - {/if} -
- - -
@@ -112,8 +190,10 @@