Skip to content

Commit

Permalink
Finalize making providers asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Oct 18, 2023
1 parent 093c073 commit 3a28bea
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 38 deletions.
58 changes: 28 additions & 30 deletions src/lib/admin/Crud/CrudEdit.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@
export let operation: CrudOperation<object>;
export let requestParameters: KeyValueObject = {};
let defaultData: undefined | null | Record<string, unknown> = crud.options.stateProvider.provide(
let defaultData: Promise<undefined | null | Record<string, unknown>> = crud.options.stateProvider.provide(
operation,
requestParameters
);
let mounted = false;
onMount(() => {
if (!defaultData) {
onMount(async () => {
const data = await defaultData;
if (!data) {
defaultData = crud.options.stateProvider.provide(operation, requestParameters);
}
mounted = true;
});
function onSubmitData(event: CustomEvent<Record<string, unknown>>) {
Expand All @@ -38,32 +36,32 @@
}
</script>

{#if !defaultData}
{#if mounted}
{#await defaultData}
<FormGroup><TextInputSkeleton /></FormGroup>
<FormGroup><CheckboxSkeleton /></FormGroup>
<FormGroup><TextAreaSkeleton /></FormGroup>
<FormGroup><ButtonSkeleton /></FormGroup>
{:then}
{#if !defaultData}
<InlineNotification kind="error">
{$_('error.crud.entity.not_found')}
</InlineNotification>
{:else}
<FormGroup><TextInputSkeleton /></FormGroup>
<FormGroup><CheckboxSkeleton /></FormGroup>
<FormGroup><TextAreaSkeleton /></FormGroup>
<FormGroup><ButtonSkeleton /></FormGroup>
<CrudForm
{operation}
{defaultData}
on:click
on:keydown
on:mouseover
on:mouseenter
on:mouseleave
on:submit
on:submitData
on:submitData={onSubmitData}
>
<svelte:fragment slot="form-header">
<h2>{$_(operation.label, { values: { name: $_(crud.options.label.singular) } })}</h2>
</svelte:fragment>
</CrudForm>
{/if}
{:else}
<CrudForm
{operation}
{defaultData}
on:click
on:keydown
on:mouseover
on:mouseenter
on:mouseleave
on:submit
on:submitData
on:submitData={onSubmitData}
>
<svelte:fragment slot="form-header">
<h2>{$_(operation.label, { values: { name: $_(crud.options.label.singular) } })}</h2>
</svelte:fragment>
</CrudForm>
{/if}
{/await}
27 changes: 19 additions & 8 deletions src/lib/admin/Crud/CrudList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import DataTable from '$lib/admin/DataTable/DataTable.svelte';
import { _ } from 'svelte-i18n';
import {onMount} from "svelte";
export let crud: CrudDefinition<unknown>;
export let operation: CrudOperation<unknown>;
Expand All @@ -30,20 +31,30 @@
throw new Error(`No StateProvider was given to the "${crud.name}" CRUD.`);
}
const rows: Promise<unknown> = crud.options.stateProvider.provide(operation, requestParameters)
.then((r: unknown) => {
console.info(r);
if (r && !Array.isArray(r)) {
function getResults() {
return crud.options.stateProvider.provide(operation, requestParameters).then((results) => {
console.info(results);
if (results && !Array.isArray(results)) {
throw new Error(
'CrudList expected state provider to return an array, current result is non-empty and not an array.'
'CrudList expected state provider to return an array, current result is non-empty and not an array.'
);
}
if (!r || !r?.length) {
r = [createEmptyRow(operation)];
if (!results || !results?.length) {
results = [createEmptyRow(operation)];
}
return r;
return results;
});
}
let rows: Promise<unknown> = getResults();
onMount(async () => {
const data = await rows;
if (!data) {
rows = getResults();
}
});
let globalActions: Array<Action> = [];
if (operation instanceof List<unknown> || operation.options?.globalActions?.length) {
Expand Down

0 comments on commit 3a28bea

Please sign in to comment.