From 53e65bd09ae89ab424dc06848dc42918e7cbbd1a Mon Sep 17 00:00:00 2001 From: Zachary Knight Date: Mon, 23 Sep 2024 19:24:45 +0000 Subject: [PATCH 1/3] feat: fix editing mods, specifically tags --- src/lib/components/mods/ModForm.svelte | 16 +++++++++------- src/lib/components/utils/TagList.svelte | 12 +++++++----- src/routes/mod/[modId]/+page.svelte | 4 +++- src/routes/mod/[modId]/edit/+page.svelte | 19 +++++++------------ src/routes/mod/[modId]/edit/+page.ts | 14 ++++++++++++-- 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/lib/components/mods/ModForm.svelte b/src/lib/components/mods/ModForm.svelte index a62f7e055..92383e6a5 100644 --- a/src/lib/components/mods/ModForm.svelte +++ b/src/lib/components/mods/ModForm.svelte @@ -12,6 +12,7 @@ import ModCompatibility from '$lib/components/mods/compatibility/ModCompatibilityEdit.svelte'; import { getTranslate } from '@tolgee/svelte'; import { SlideToggle } from '@skeletonlabs/skeleton'; + import { onMount } from 'svelte'; export const { t } = getTranslate(); @@ -45,14 +46,15 @@ onSubmit: (submitted: ModData) => onSubmit(trimNonSchema(submitted, modSchema)) }); - let tags = []; - $: { - const anyData = $data; - if (anyData.tags) { - tags = anyData.tags; - delete anyData.tags; - } + let tags = $data.tags; + const computeTags = () => { $data.tagIDs = tags.map((tag) => tag.id); + }; + + onMount(computeTags); + + $: if (tags) { + computeTags(); } // The GQL type NewMod does not have a compatibility field. diff --git a/src/lib/components/utils/TagList.svelte b/src/lib/components/utils/TagList.svelte index ac88464da..00af1eeb6 100644 --- a/src/lib/components/utils/TagList.svelte +++ b/src/lib/components/utils/TagList.svelte @@ -3,7 +3,7 @@ import type { Tag } from '$lib/generated/graphql'; import { queryStore, getContextClient } from '@urql/svelte'; import { Autocomplete, type AutocompleteOption, InputChip, type PopupSettings, popup } from '@skeletonlabs/skeleton'; - import { onMount } from 'svelte'; + import { afterUpdate, onMount } from 'svelte'; import TagDisplay from './TagDisplay.svelte'; const client = getContextClient(); @@ -32,11 +32,11 @@ target: 'popupAutocomplete', placement: 'bottom-start' }; - let tagList = []; - const loadTagList = () => (tagList = tags.map((t: Tag) => t.name)); + const loadTagList = tags ? () => (tagList = tags.map((t: Tag) => t.name)) : () => {}; onMount(loadTagList); + afterUpdate(loadTagList); const addTag = (tag: AutocompleteOption) => { const realTag = $getAllTags.data?.getTags?.find((t) => t.id == tag.value); @@ -55,7 +55,9 @@ const removeTag = (label: string) => { const idx = tags.findIndex((t) => t.name === label); tags = [...tags.slice(0, idx), ...tags.slice(idx + 1)]; - + if (tags.length == null) { + tags = []; + } loadTagList(); }; @@ -64,7 +66,7 @@
{#if !editable} - {#if tags.length > 0} + {#if tags && tags.length > 0}
{#each tags as tag} diff --git a/src/routes/mod/[modId]/+page.svelte b/src/routes/mod/[modId]/+page.svelte index 14b42f9c6..53869231f 100644 --- a/src/routes/mod/[modId]/+page.svelte +++ b/src/routes/mod/[modId]/+page.svelte @@ -101,7 +101,9 @@

{$mod.data.mod.name}

{#if canUserEdit} -