Updated Composite to render a FallBack Component, when no visual component has been rendered.

This commit is contained in:
Samuel Andert 2023-08-02 12:37:45 +02:00
parent 7f57036e94
commit e4cb7df353
3 changed files with 24 additions and 27 deletions

View File

@ -16,7 +16,7 @@
children: [ children: [
{ {
id: 'child', id: 'child',
component: 'LearnReady', // component: 'LearnReady',
slot: 'left', slot: 'left',
store: { store: {
xstate: 'NOTREADY' xstate: 'NOTREADY'

View File

@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import { onDestroy } from 'svelte'; import { onDestroy } from 'svelte';
import Composite from './Composite.svelte'; import Composite from './Composite.svelte';
import FallBack from './FallBack.svelte';
import components from '$lib/core/componentLoader'; import components from '$lib/core/componentLoader';
import services from '$lib/core/servicesLoader'; import services from '$lib/core/servicesLoader';
import { dataStore } from '$lib/core/dataLoader'; import { dataStore } from '$lib/core/dataLoader';
@ -19,7 +20,7 @@
layout?: ICompositeLayout; layout?: ICompositeLayout;
id: string; id: string;
slot?: string; slot?: string;
component: string; component?: string;
services?: string[]; services?: string[];
map?: Record<string, string>; map?: Record<string, string>;
store?: Record<string, any>; store?: Record<string, any>;
@ -157,7 +158,7 @@
const module = await components[componentName](); const module = await components[componentName]();
return module.default; return module.default;
} }
return null; return FallBack;
} }
async function loadService(serviceName: string) { async function loadService(serviceName: string) {
@ -169,7 +170,8 @@
} }
async function loadComponentAndService(component: IComposite) { async function loadComponentAndService(component: IComposite) {
return await getComponent(component.component); const componentName = component.component || 'FallBack';
return await getComponent(componentName);
} }
</script> </script>
@ -179,17 +181,14 @@
> >
{#if composite?.servicesLoaded} {#if composite?.servicesLoaded}
{#await loadComponentAndService(composite) then Component} {#await loadComponentAndService(composite) then Component}
{#if Component}
<svelte:component <svelte:component
this={Component} this={Component}
id={composite.id} id={composite.id}
store={getCompositeStore(composite.id)} store={getCompositeStore(composite.id)}
services={loadedServices} services={loadedServices}
/> />
{/if}
{/await} {/await}
{/if} {/if}
{#if composite?.children} {#if composite?.children}
{#each composite.children as child (child.id)} {#each composite.children as child (child.id)}
<div <div
@ -198,7 +197,6 @@
> >
{#if child.servicesLoaded} {#if child.servicesLoaded}
{#await loadComponentAndService(child) then ChildComponent} {#await loadComponentAndService(child) then ChildComponent}
{#if ChildComponent}
<svelte:component <svelte:component
this={ChildComponent} this={ChildComponent}
id={child.id} id={child.id}
@ -208,12 +206,7 @@
{#if child.children && child.children.length} {#if child.children && child.children.length}
<Composite composite={child} /> <Composite composite={child} />
{/if} {/if}
{:else}
<p>Component {child.component} not found.</p>
{/if}
{/await} {/await}
{:else}
<p>Loading services for child {child.id}...</p>
{/if} {/if}
</div> </div>
{/each} {/each}

View File

@ -0,0 +1,4 @@
<script>
export let id;
console.log(`FallBack component rendered with id ${id}`);
</script>