Skip to content

Commit

Permalink
ensure wizard.data is always set
Browse files Browse the repository at this point in the history
  • Loading branch information
kenkunz committed Oct 11, 2024
1 parent f5405bc commit 0686789
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/routes/wizard/deposit/payment/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
6 changes: 3 additions & 3 deletions src/routes/wizard/redeem/deposit-status/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/routes/wizard/redeem/shares-redemption/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -149,7 +149,7 @@
}
});
redemption.restore($wizard.data?.redemptionState);
redemption.restore($wizard.data.redemptionState);
$: wizard.updateData({ redemptionState: $redemption });
</script>

Expand Down
2 changes: 1 addition & 1 deletion src/routes/wizard/redeem/success/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
28 changes: 14 additions & 14 deletions src/routes/wizard/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>;
completed?: Set<string>;
};
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<typeof wizardValueSchema>;

export type Step = {
slug: string;
Expand All @@ -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);
Expand All @@ -47,31 +50,28 @@ function setSession(data: WizardValue) {

const { set, update, ...baseStore }: Writable<WizardValue> = writable(getSession());

function init(slug: string, returnTo: string, data: any = {}) {
const completed: Set<string> = new Set();
set({ slug, returnTo, data, completed });
function init(slug: string, returnTo: string, data: Record<string, any> = {}) {
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;
});
}

function updateData(data: any) {
update(($wizard) => {
if (!$wizard.data) throw Error('wizard not initialized');
$wizard.data = { ...$wizard.data, ...data };
return $wizard;
});
}

// Clear the store and sessionStorage data
function reset() {
set({});
set(wizardValueSchema.parse({}));
storage?.removeItem(storageKey);
}

Expand Down

0 comments on commit 0686789

Please sign in to comment.