From e12f6bcf05326bbb8b9c4d76cc343616029d62cd Mon Sep 17 00:00:00 2001 From: Samuel Andert Date: Mon, 24 Jul 2023 15:00:05 +0200 Subject: [PATCH] feat(components): Initialize component state and enable property mapping - Ensure every component has an initialized state, defaulting to empty. - Implement property mapping to synchronize states between parent and child components. --- src/lib/components/Composite.svelte | 64 +++++++++++++++++-- .../components/HelloEarth/HelloEarth.svelte | 11 +++- src/lib/components/{ => Wallet}/Wallet.svelte | 14 ++-- src/lib/services/authWithGoogle.ts | 0 src/lib/stores/componentStores.ts | 16 +++++ src/routes/+page.svelte | 20 ++++-- 6 files changed, 106 insertions(+), 19 deletions(-) rename src/lib/components/{ => Wallet}/Wallet.svelte (52%) create mode 100644 src/lib/services/authWithGoogle.ts create mode 100644 src/lib/stores/componentStores.ts diff --git a/src/lib/components/Composite.svelte b/src/lib/components/Composite.svelte index e8faa79..2e6a442 100644 --- a/src/lib/components/Composite.svelte +++ b/src/lib/components/Composite.svelte @@ -1,17 +1,67 @@ + +
{#each componentsData.children as component (component.id)} {#await getComponent(component.componentName) then Component} {#if Component}
- + {#if component.actions}
diff --git a/src/lib/components/HelloEarth/HelloEarth.svelte b/src/lib/components/HelloEarth/HelloEarth.svelte index 1bb08dd..f9ef1e3 100644 --- a/src/lib/components/HelloEarth/HelloEarth.svelte +++ b/src/lib/components/HelloEarth/HelloEarth.svelte @@ -1 +1,10 @@ -
Hello Earth
+ + +
Hello Earth {JSON.stringify($store)}
diff --git a/src/lib/components/Wallet.svelte b/src/lib/components/Wallet/Wallet.svelte similarity index 52% rename from src/lib/components/Wallet.svelte rename to src/lib/components/Wallet/Wallet.svelte index ded63eb..60b03cb 100644 --- a/src/lib/components/Wallet.svelte +++ b/src/lib/components/Wallet/Wallet.svelte @@ -2,16 +2,16 @@ import { onMount } from 'svelte'; import { connectWallet } from '$lib/services/wallet/wallet'; + import { getComponentStore } from '$lib/stores/componentStores.ts'; + + // please abstract this. + export let id; + const store = getComponentStore(id); + let pkpWallet = null; - const pkpPubKey = - '046da3ba67065fd1e2726242ca01cd4601524893f4aa4b0042578fa6cbec28fa8c9a28eb9f7893932fc09717edc9e1db57e157a21eed346247c1db5a722a01f571'; - - // Specify your RPC URL here - const rpcURL = 'https://rpc.gnosischain.com/'; - onMount(async () => { - pkpWallet = await connectWallet(pkpPubKey, rpcURL); + pkpWallet = await connectWallet($store.pkpPubKey, $store.rpcURL); }); diff --git a/src/lib/services/authWithGoogle.ts b/src/lib/services/authWithGoogle.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/lib/stores/componentStores.ts b/src/lib/stores/componentStores.ts new file mode 100644 index 0000000..3c3e44d --- /dev/null +++ b/src/lib/stores/componentStores.ts @@ -0,0 +1,16 @@ +import { writable } from 'svelte/store'; + +const componentStores = new Map(); + +// Create or retrieve a component store +export function createComponentStore(componentId, initialState = {}) { + if (!componentStores.has(componentId)) { + componentStores.set(componentId, writable(initialState)); + } + return componentStores.get(componentId); +} + +// Get component store or create a default empty one if not exists +export function getComponentStore(componentId) { + return componentStores.get(componentId) || createComponentStore(componentId); +} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index b4f8df5..7ccb60f 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -11,14 +11,24 @@ `, children: [ { - id: 1, + id: 'login1', componentName: 'Login', - props: {}, - slot: 'login', children: [ { - id: 5, - componentName: 'Wallet' + id: 'wallet1', + componentName: 'Wallet', + state: { + rpcURL: 'https://rpc.gnosischain.com/', + pkpPubKey: + '046da3ba67065fd1e2726242ca01cd4601524893f4aa4b0042578fa6cbec28fa8c9a28eb9f7893932fc09717edc9e1db57e157a21eed346247c1db5a722a01f571' + }, + children: [ + { + id: 'HelloEarth1', + componentName: 'HelloEarth', + map: { pkp: 'wallet1.pkpPubKey', rpcURL: 'wallet1.rpcURL' } + } + ] } ] },