From 61cb8c8bc3eb6beaee304ae46a408067c1a8e03a Mon Sep 17 00:00:00 2001 From: Samuel Andert Date: Sun, 30 Jul 2023 12:40:00 +0200 Subject: [PATCH] abstracting the xstate towards a more generic flow --- src/lib/components/examples/Recipies.svelte | 239 ++++++++++++-------- 1 file changed, 147 insertions(+), 92 deletions(-) diff --git a/src/lib/components/examples/Recipies.svelte b/src/lib/components/examples/Recipies.svelte index ae73e44..7383344 100644 --- a/src/lib/components/examples/Recipies.svelte +++ b/src/lib/components/examples/Recipies.svelte @@ -36,13 +36,26 @@ initial: 'start', context: { name: '', - email: '' + email: '', + constraints: {} // <- New addition }, states: { start: { + meta: { + title: 'Welcome!', + description: 'Start your registration process by clicking next.' + }, on: { NEXT: 'nameInput' } }, nameInput: { + meta: { + title: 'Name Input', + description: 'Please enter your name.', + fieldLabel: 'Name' + }, + entry: assign({ + constraints: (context) => constraints.name || {} + }), on: { NEXT: { target: 'emailInput', @@ -52,6 +65,14 @@ } }, emailInput: { + meta: { + title: 'Email Input', + description: 'Please enter your email address.', + fieldLabel: 'Email' + }, + entry: assign({ + constraints: (context) => constraints.email || {} + }), on: { NEXT: { target: 'summary', @@ -61,19 +82,45 @@ } }, summary: { + meta: { + title: 'Summary', + description: 'Review your details before submission.' + }, on: { SUBMIT: 'submitting' } }, submitting: { + meta: { + title: 'Submitting...' + }, invoke: { src: 'createUserService', onDone: 'success', - onError: 'failure' + onError: { + target: 'failure', + actions: assign({ + error: (context, event) => event.data + }) + } } }, - success: {}, - failure: {} + success: { + meta: { + title: 'Success' + }, + on: { + RESTART: 'start' + } + }, + failure: { + meta: { + title: 'Submission Failed' + }, + on: { + RESTART: 'start' + } + } } }, { @@ -86,7 +133,10 @@ }) }, services: { - createUserService: (context) => createUser(context.name, context.email) + createUserService: (context) => { + console.log('Attempting to create user...'); + return createUser(context.name, context.email); + } } } ); @@ -100,101 +150,106 @@
+

+ {$state.value in stateMachine.states && stateMachine.states[$state.value].meta + ? stateMachine.states[$state.value].meta.title + : 'Unknown state'} +

+

+ {$state.value in stateMachine.states ? stateMachine.states[$state.value].meta.description : ''} +

+ {#if $state.value === 'start'} - -
-

Step 1 - Start

- + {:else if $state.value === 'nameInput'} +
+
+ {#if $errors.name} + {$errors.name} + {:else} + + {/if} + + +
+ -
- {:else if $state.value === 'nameInput'} - -
-

Step 2 - Name Input

- -
- {#if $errors.name} - {$errors.name} - {:else} - - {/if} - - -
- - -
+ {:else if $state.value === 'emailInput'} -
-

Step 3 - Email Input

-
-
- {#if $errors.email} - {$errors.email} - {:else} - - {/if} + +
+ {#if $errors.email} + {$errors.email} + {:else} + + {/if} - -
- - -
- - + +
+ + {:else if $state.value === 'summary'} -
-

Summary

-

Name: {$form.name}

-

Email: {$form.email}

- -
+

Name: {$form.name}

+

Email: {$form.email}

+ {:else if $state.value === 'submitting'} -
-

Submitting...

-
+ {:else if $state.value === 'success'} -
-

User created successfully!

- -
+

Thank you for your submission!

+ {:else if $state.value === 'failure'} -
-

Failed to create user. Please try again.

- -
+

+ Error: {$state.context.error?.message || 'An unknown error occurred.'} +

+ {/if}