From 0686789602b91a37759d6bb68f5fabefb0f84ad1 Mon Sep 17 00:00:00 2001 From: Ken Kunz Date: Fri, 11 Oct 2024 18:03:59 -0500 Subject: [PATCH] ensure wizard.data is always set --- .../wizard/deposit/payment/+page.svelte | 2 +- .../wizard/redeem/deposit-status/+page.svelte | 6 ++-- .../redeem/shares-redemption/+page.svelte | 4 +-- src/routes/wizard/redeem/success/+page.svelte | 2 +- src/routes/wizard/store.ts | 28 +++++++++---------- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/routes/wizard/deposit/payment/+page.svelte b/src/routes/wizard/deposit/payment/+page.svelte index 9995f62de..d6fbe9771 100644 --- a/src/routes/wizard/deposit/payment/+page.svelte +++ b/src/routes/wizard/deposit/payment/+page.svelte @@ -300,7 +300,7 @@ }); afterNavigate(() => { - const { state, ...rest } = $wizard.data?.paymentSnapshot ?? {}; + const { state, ...rest } = $wizard.data.paymentSnapshot ?? {}; ({ paymentValue, sharePrice, approvalTxId, paymentTxId, error } = rest); payment.restore(state); }); diff --git a/src/routes/wizard/redeem/deposit-status/+page.svelte b/src/routes/wizard/redeem/deposit-status/+page.svelte index 72c82c2a3..27cd4ae0b 100644 --- a/src/routes/wizard/redeem/deposit-status/+page.svelte +++ b/src/routes/wizard/redeem/deposit-status/+page.svelte @@ -8,12 +8,12 @@ import { getLogoUrl } from '$lib/helpers/assets'; $: ({ address, chain } = $wallet); - $: ({ chainId, contracts, nativeCurrency, vaultShares } = $wizard.data!); + $: ({ chainId, contracts, nativeCurrency, vaultShares } = $wizard.data); $: chainCurrency = chain?.nativeCurrency.symbol; $: depositStatusComplete = - 'denominationToken' in $wizard.data! && - 'vaultNetValue' in $wizard.data! && + 'denominationToken' in $wizard.data && + 'vaultNetValue' in $wizard.data && nativeCurrency?.value > 0n && vaultShares?.value > 0n; diff --git a/src/routes/wizard/redeem/shares-redemption/+page.svelte b/src/routes/wizard/redeem/shares-redemption/+page.svelte index 9c765e525..e71d8acfc 100644 --- a/src/routes/wizard/redeem/shares-redemption/+page.svelte +++ b/src/routes/wizard/redeem/shares-redemption/+page.svelte @@ -15,7 +15,7 @@ import { formatNumber } from '$lib/helpers/formatters'; import { getLogoUrl } from '$lib/helpers/assets'; - const { chainId, contracts, vaultShares, vaultNetValue } = $wizard.data! as { + const { chainId, contracts, vaultShares, vaultNetValue } = $wizard.data as { chainId: ConfiguredChainId; contracts: EnzymeSmartContracts; vaultShares: GetTokenBalanceReturnType; @@ -149,7 +149,7 @@ } }); - redemption.restore($wizard.data?.redemptionState); + redemption.restore($wizard.data.redemptionState); $: wizard.updateData({ redemptionState: $redemption }); diff --git a/src/routes/wizard/redeem/success/+page.svelte b/src/routes/wizard/redeem/success/+page.svelte index 2c188107e..e56f8534b 100644 --- a/src/routes/wizard/redeem/success/+page.svelte +++ b/src/routes/wizard/redeem/success/+page.svelte @@ -8,7 +8,7 @@ export let data; const { receivedAssets } = data; - const { strategyName, shares } = $wizard.data!; + const { strategyName, shares } = $wizard.data; function sharesWithLabel(value: number) { const label = value === 1 ? 'share' : 'shares'; diff --git a/src/routes/wizard/store.ts b/src/routes/wizard/store.ts index 1b1c43c29..40552fc2c 100644 --- a/src/routes/wizard/store.ts +++ b/src/routes/wizard/store.ts @@ -15,13 +15,16 @@ import { browser } from '$app/environment'; import { derived, writable, type Writable } from 'svelte/store'; import { stringify, parse } from 'devalue'; +import { z } from 'zod'; -export type WizardValue = { - slug?: string; - returnTo?: string; - data?: Record; - completed?: Set; -}; +const wizardValueSchema = z.object({ + slug: z.string().optional(), + returnTo: z.string().optional(), + data: z.record(z.any()).default({}), + completed: z.set(z.string()).default(new Set()) +}); + +export type WizardValue = z.infer; export type Step = { slug: string; @@ -33,8 +36,8 @@ const storageKey = 'ts:wizard'; function getSession() { try { - const serialized = storage?.getItem(storageKey); - return serialized ? parse(serialized) : {}; + const raw = storage?.getItem(storageKey); + return wizardValueSchema.parse(raw ? parse(raw) : {}); } catch (e) { console.error('Error deserializing wizard data from sessionStorage.'); console.error(e); @@ -47,15 +50,13 @@ function setSession(data: WizardValue) { const { set, update, ...baseStore }: Writable = writable(getSession()); -function init(slug: string, returnTo: string, data: any = {}) { - const completed: Set = new Set(); - set({ slug, returnTo, data, completed }); +function init(slug: string, returnTo: string, data: Record = {}) { + set(wizardValueSchema.parse({ slug, returnTo, data })); } function toggleComplete(step: string, completed = true) { const action = completed ? 'add' : 'delete'; update(($wizard) => { - if (!$wizard.completed) throw Error('wizard not initialized'); $wizard.completed[action](step); return $wizard; }); @@ -63,7 +64,6 @@ function toggleComplete(step: string, completed = true) { function updateData(data: any) { update(($wizard) => { - if (!$wizard.data) throw Error('wizard not initialized'); $wizard.data = { ...$wizard.data, ...data }; return $wizard; }); @@ -71,7 +71,7 @@ function updateData(data: any) { // Clear the store and sessionStorage data function reset() { - set({}); + set(wizardValueSchema.parse({})); storage?.removeItem(storageKey); }