Skip to content

Commit

Permalink
feat: set button disabled when handling click event
Browse files Browse the repository at this point in the history
  • Loading branch information
drunkg00se committed Oct 14, 2024
1 parent 0563a29 commit b10d551
Showing 1 changed file with 82 additions and 12 deletions.
94 changes: 82 additions & 12 deletions src/lib/components/Modal/Config/Panels/DownloadHistory.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script lang="ts">
import { historyDb } from '@/lib/db';
import { FileButton } from '@skeletonlabs/skeleton';
import { FileButton, ProgressRadial } from '@skeletonlabs/skeleton';
import t from '@/lib/lang';
import { useHistoryBackup } from '../useHistoryBackup';
import configStore from '../store';
import { HistoryBackupInterval } from '@/lib/config';
import { logger } from '@/lib/logger';
import { writable } from 'svelte/store';
export let bg = 'bg-white/30 dark:bg-black/15';
export let border = 'divide-y-[1px] *:border-surface-300-600-token';
Expand All @@ -21,7 +22,20 @@
export let sectionSpace = `space-y-4`;
export let sectionTitle = 'font-bold';
const { importJSON, exportAsJSON, exportAsCSV } = useHistoryBackup();
function usePendingButton<T extends (...args: any[]) => any>(fn: T) {
const store = writable(false);
const wrapFn = async (...args: Parameters<T>): Promise<Awaited<ReturnType<T>>> => {
store.set(true);
const result = await fn(...args);
store.set(false);
return result;
};
return {
store,
wrapFn
};
}
async function importFromJSON(evt: Event) {
const file = (evt.currentTarget as HTMLInputElement).files?.[0];
Expand All @@ -40,6 +54,13 @@
if (!isConfirm) return;
return historyDb.clear();
}
const { importJSON, exportAsJSON, exportAsCSV } = useHistoryBackup();
const { store: importPending, wrapFn: wrapImportFromJSON } = usePendingButton(importFromJSON);
const { store: clearPending, wrapFn: wrapClearDb } = usePendingButton(clearDb);
const { store: exportJsonPending, wrapFn: wrapExportAsJSON } = usePendingButton(exportAsJSON);
const { store: exportCsvPending, wrapFn: wrapExportAsCSV } = usePendingButton(exportAsCSV);
</script>

<div class={sectionSpace}>
Expand Down Expand Up @@ -71,15 +92,39 @@
<ul class={ulClasses}>
<li>
<p class="flex-auto">{t('setting.history.options.export_as_json')}</p>
<button class="btn variant-filled" on:click={exportAsJSON}
>{t('setting.history.button.export')}</button
<button
disabled={$exportJsonPending}
class="btn variant-filled"
on:click={wrapExportAsJSON}
>
{#if $exportJsonPending}
<ProgressRadial
stroke={80}
width="w-5"
meter="stroke-primary-500"
track="stroke-primary-500/30"
/>
{/if}
<span>
{t('setting.history.button.export')}
</span>
</button>
</li>
<li>
<p class="flex-auto">{t('setting.history.options.export_as_csv')}</p>
<button class="btn variant-filled" on:click={exportAsCSV}
>{t('setting.history.button.export')}</button
>
<button disabled={$exportCsvPending} class="btn variant-filled" on:click={wrapExportAsCSV}>
{#if $exportCsvPending}
<ProgressRadial
stroke={80}
width="w-5"
meter="stroke-primary-500"
track="stroke-primary-500/30"
/>
{/if}
<span>
{t('setting.history.button.export')}
</span>
</button>
</li>
</ul>
</section>
Expand All @@ -89,9 +134,24 @@
<ul class={ulClasses}>
<li>
<p class="flex-auto">{t('setting.history.options.import_json')}</p>
<FileButton name="import-file" accept=".json" on:change={importFromJSON}
>{t('setting.history.button.import')}</FileButton
<FileButton
bind:disabled={$importPending}
name="import-file"
accept=".json"
on:change={wrapImportFromJSON}
>
{#if $importPending}
<ProgressRadial
stroke={80}
width="w-5"
meter="stroke-primary-500"
track="stroke-primary-500/30"
/>
{/if}
<span>
{t('setting.history.button.import')}
</span>
</FileButton>
</li>
</ul>
</section>
Expand All @@ -101,9 +161,19 @@
<ul class={ulClasses}>
<li>
<p class="flex-auto">{t('setting.history.options.clear_history')}</p>
<button class="btn variant-filled" on:click={clearDb}
>{t('setting.history.button.clear')}</button
>
<button disabled={$clearPending} class="btn variant-filled" on:click={wrapClearDb}>
{#if $clearPending}
<ProgressRadial
stroke={80}
width="w-5"
meter="stroke-primary-500"
track="stroke-primary-500/30"
/>
{/if}
<span>
{t('setting.history.button.clear')}
</span>
</button>
</li>
</ul>
</section>
Expand Down

0 comments on commit b10d551

Please sign in to comment.