Refactored composer component interface to one single $me store

This commit is contained in:
Samuel Andert 2023-08-07 11:54:27 +02:00
parent 1a8bb64ff2
commit d36a94badc
5 changed files with 35 additions and 47 deletions

View File

@ -1,26 +1,28 @@
<script> <script>
export let me; export let me;
export let data;
let childData; let composerBob;
$: if (me.do) { $: {
childData = me.do.core.subscribeData('ComposerBob'); if ($me.do.core) {
composerBob = $me.do.core.subscribeData('ComposerBob');
}
} }
$: { $: {
if ($childData && $childData.state) { if ($composerBob.state) {
me.do.machine.send($childData.state); $me.do.machine.send($composerBob.state);
} }
} }
// Call testAlert
</script> </script>
<div class="p-8 border-2 border-blue-500"> <div class="p-8 border-2 border-blue-500">
<p>My ID is: {me.id}</p> <p>My ID is: {$me.id}</p>
My state is: {$data.state} My state is: {$me.state}
<div <div
class="p-2 border-2" class="p-2 border-2"
style="background-color: {$data.state}; border-radius: 50%; width: 50px; height: 50px;" style="background-color: {$me.state}; border-radius: 50%; width: 50px; height: 50px;"
/> />
</div> </div>

View File

@ -1,18 +1,17 @@
<script> <script>
export let me; export let me;
export let data;
const handleButton = () => { const handleButton = () => {
if (me && me.do && me.do.machine) { if ($me.do.machine) {
me.do.machine.send('TOGGLE'); $me.do.machine.send('TOGGLE');
} }
}; };
</script> </script>
<div class="p-8 border-2 border-yellow-500"> <div class="p-8 border-2 border-yellow-500">
<p>My ID is: {me.id}</p> <p>My ID is: {$me.id}</p>
My state is: {$data.state} My state is: {$me.state}
<button <button
class="px-4 py-2 font-bold text-white bg-blue-500 rounded hover:bg-blue-700" class="px-4 py-2 font-bold text-white bg-blue-500 rounded hover:bg-blue-700"
on:click={handleButton}>Switch</button on:click={handleButton}>Switch</button

View File

@ -1,7 +1,6 @@
<script> <script>
import { createMessage } from '$lib/services/messages/messages'; import { createMessage } from '$lib/services/messages/messages';
export let me; export let me;
export let data;
function sendMessage() { function sendMessage() {
const randomNumber = Math.floor(Math.random() * 100) + 1; const randomNumber = Math.floor(Math.random() * 100) + 1;
@ -16,11 +15,11 @@
<div class="flex flex-col h-screen border-2 border-green-500"> <div class="flex flex-col h-screen border-2 border-green-500">
<section class="p-8 h-96"> <section class="p-8 h-96">
<p>My ID is: {me.id}</p> <p>My ID is: {$me.id}</p>
My state is: {$data.state} My state is: {$me.state}
{#if $data.store.todos} {#if $me.store.todos}
{#each $data.store.todos as todo} {#each $me.store.todos as todo}
<p>{todo.text}</p> <p>{todo.text}</p>
{/each} {/each}
{/if} {/if}
@ -32,8 +31,8 @@
Send Send
</button> </button>
<section class="flex-grow p-8 overflow-y-auto"> <section class="flex-grow p-8 overflow-y-auto">
{#if $data.store.messages} {#if $me.store.messages}
{#each $data.store.messages as message} {#each $me.store.messages as message}
<p>{message.text}</p> <p>{message.text}</p>
{/each} {/each}
{/if} {/if}

View File

@ -5,7 +5,6 @@
import { writable } from 'svelte/store'; import { writable } from 'svelte/store';
import { RangeSlider, SlideToggle } from '@skeletonlabs/skeleton'; import { RangeSlider, SlideToggle } from '@skeletonlabs/skeleton';
export let data;
export let me; export let me;
const initialFormData = { const initialFormData = {
@ -33,9 +32,9 @@
$: { $: {
// Send events to the state machine based on the validation status // Send events to the state machine based on the validation status
if (!($errors.name || $errors.email || $errors.about || $errors.age || $errors.favoriteFood)) { if (!($errors.name || $errors.email || $errors.about || $errors.age || $errors.favoriteFood)) {
me.do.machine.send('VALIDATE'); $me.do.machine.send('VALIDATE');
} else { } else {
me.do.machine.send('INVALIDATE'); $me.do.machine.send('INVALIDATE');
} }
} }
@ -71,7 +70,7 @@
</script> </script>
<div class="w-full p-6 border-2 border-red-500"> <div class="w-full p-6 border-2 border-red-500">
My state is: {$data.state} My state is: {$me.state}
{#if $successMessage} {#if $successMessage}
<aside class="w-full max-w-md p-4 alert variant-ghost" id="message-container"> <aside class="w-full max-w-md p-4 alert variant-ghost" id="message-container">
<div class="alert-message"> <div class="alert-message">
@ -154,7 +153,7 @@
<button <button
type="submit" type="submit"
class="w-full px-4 py-2 mt-4 text-white bg-blue-500 rounded-md hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500" class="w-full px-4 py-2 mt-4 text-white bg-blue-500 rounded-md hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500"
disabled={$data.state === 'notValidated'} disabled={$me.state === 'notValidated'}
> >
Submit Submit
</button> </button>

View File

@ -60,20 +60,19 @@
subscribeAndMapQueries(component.id, component.data.map); subscribeAndMapQueries(component.id, component.data.map);
} }
} }
getComposerStore(component.id).update((storeValue) => ({
if (component.children) { ...storeValue,
component.children.forEach(loadComponentAndInitializeState);
}
// Load component and service
const componentName = component.component || 'FallBack';
component.me = {
id: component.id, id: component.id,
do: { do: {
core: coreServices, core: coreServices,
machine: component.machineService || null machine: component.machineService || null
} }
}; }));
if (component.children) {
component.children.forEach(loadComponentAndInitializeState);
}
const componentName = component.component || 'FallBack';
return await getComponent(componentName); return await getComponent(componentName);
} }
@ -128,12 +127,7 @@
style={layoutStyle} style={layoutStyle}
> >
{#await loadComponentAndInitializeState(composer) then Component} {#await loadComponentAndInitializeState(composer) then Component}
<svelte:component <svelte:component this={Component} id={composer.id} me={getComposerStore(composer.id)} />
this={Component}
id={composer.id}
data={getComposerStore(composer.id)}
me={composer.me}
/>
{/await} {/await}
{#if composer?.children} {#if composer?.children}
{#each composer.children as child (child.id)} {#each composer.children as child (child.id)}
@ -142,12 +136,7 @@
style={`grid-area: ${child.slot}`} style={`grid-area: ${child.slot}`}
> >
{#await loadComponentAndInitializeState(child) then ChildComponent} {#await loadComponentAndInitializeState(child) then ChildComponent}
<svelte:component <svelte:component this={ChildComponent} id={child.id} me={getComposerStore(child.id)} />
this={ChildComponent}
id={child.id}
data={getComposerStore(child.id)}
me={child.me}
/>
{#if child.children && child.children.length} {#if child.children && child.children.length}
<Composer composer={child} /> <Composer composer={child} />
{/if} {/if}