Skip to content

Commit

Permalink
Unify committing of info updates
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Jan 13, 2025
1 parent f52b7ec commit 1a02d96
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
17 changes: 10 additions & 7 deletions src/routes/[...item]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import EditableMarkdown from '$components/markdown';
import api from '$endpoints';
import consts from '$lib/consts';
import DelayedUpdater from '$lib/delayedUpdate';
import { generateKeywords } from '$lib/seo';
import type { ItemInfo } from '$lib/server/data/item';
import { itemFileUrl } from '$lib/urls';
Expand All @@ -26,10 +27,9 @@
let editing = $state(false);
async function commitInfoChanges(newInfo: ItemInfo) {
await api().item(data.itemId).info.put(newInfo);
thisItem.info = newInfo;
}
let infoUpdater = new DelayedUpdater(async (info: ItemInfo) => {
await api().item(data.itemId).info.put(info);
}, consts.EDIT_COMMIT_HESITATION);
</script>

<svelte:head>
Expand Down Expand Up @@ -67,13 +67,16 @@
loggedIn={data.loggedIn}
{editing}
onbegin={() => (editing = true)}
onfinish={() => (editing = false)}
onfinish={() => {
editing = false;
infoUpdater.commit();
}}
/>
{#if editing}
<MainDataEdit
itemId={data.itemId}
bind:item={thisItem}
onchange={commitInfoChanges}
onchange={(newInfo) => infoUpdater.update(newInfo)}
/>

<ItemFilesEdit itemId={data.itemId} bind:files={thisItem.ls} />
Expand Down Expand Up @@ -110,7 +113,7 @@
<NewSection
oncreate={(newSection) => {
thisItem.info.sections.push(newSection);
void commitInfoChanges(thisItem.info);
infoUpdater.update(thisItem.info);
}}
/>
{/if}
Expand Down
22 changes: 7 additions & 15 deletions src/routes/[...item]/ItemInfoEdit.svelte
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
<script lang="ts">
import FilePicker from '$components/pickers/FilePicker.svelte';
import consts from '$lib/consts';
import DelayedUpdater from '$lib/delayedUpdate';
import type { ItemId } from '$lib/itemId';
import type { ItemData, ItemInfo } from '$lib/server/data/item';
import { itemFileUrl } from '$lib/urls';
type Props = {
itemId: ItemId;
item: ItemData;
onchange: (info: ItemInfo) => Promise<any>;
onchange: (info: ItemInfo) => void;
};
let { item = $bindable(), itemId, onchange }: Props = $props();
let isRootPage = $derived(itemId.length === 0);
let updater = new DelayedUpdater(async (info: ItemInfo) => {
if (info.shortName === '') {
info.shortName = null;
}
await onchange(info);
}, consts.EDIT_COMMIT_HESITATION);
</script>

<form onsubmit={(e) => e.preventDefault()}>
Expand All @@ -30,7 +22,7 @@
type="text"
placeholder={consts.APP_NAME}
bind:value={item.info.name}
oninput={() => updater.update(item.info)}
oninput={() => onchange(item.info)}
required
/>
<p>
Expand All @@ -45,7 +37,7 @@
type="text"
placeholder={item.info.name}
bind:value={item.info.shortName}
oninput={() => updater.update(item.info)}
oninput={() => onchange(item.info)}
/>
<p>
{#if isRootPage}
Expand All @@ -59,15 +51,15 @@
type="text"
placeholder="A concise description."
bind:value={item.info.description}
oninput={() => updater.update(item.info)}
oninput={() => onchange(item.info)}
required
/>
<p>A concise description of the item, shown on links to this page.</p>
<h2>Color</h2>
<input
type="color"
bind:value={item.info.color}
oninput={() => updater.update(item.info)}
oninput={() => onchange(item.info)}
required
/>
<p>The theme color of the item is shown in the background of the page.</p>
Expand All @@ -76,7 +68,7 @@
<FilePicker
files={item.ls}
bind:selected={item.info.banner}
onchange={() => updater.update(item.info)}
onchange={() => onchange(item.info)}
/>
<!-- Banner image -->
{#if item.info.banner}
Expand All @@ -93,7 +85,7 @@
<FilePicker
files={item.ls}
bind:selected={item.info.icon}
onchange={() => updater.update(item.info)}
onchange={() => onchange(item.info)}
/>
<!-- Banner image -->
{#if item.info.icon}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/[...item]/sections/Package.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
});
</script>

<a href={url}>
<a href={url} target="_blank">
<i class={icon}></i>
<b>{label ?? `Install using ${providerName}`}</b>
<CopyButton text={command} hint="Copy install command">
Expand Down
2 changes: 1 addition & 1 deletion src/routes/[...item]/sections/Repo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
</script>

{#if !editing}
<a href={url}>
<a href={url} target="_blank">
<i class={icon}></i>
<b>{displayLabel}</b>
{#await starCount}
Expand Down

0 comments on commit 1a02d96

Please sign in to comment.