abstracting the xstate towards a more generic flow
This commit is contained in:
parent
7cd7506e1d
commit
61cb8c8bc3
@ -36,13 +36,26 @@
|
|||||||
initial: 'start',
|
initial: 'start',
|
||||||
context: {
|
context: {
|
||||||
name: '',
|
name: '',
|
||||||
email: ''
|
email: '',
|
||||||
|
constraints: {} // <- New addition
|
||||||
},
|
},
|
||||||
states: {
|
states: {
|
||||||
start: {
|
start: {
|
||||||
|
meta: {
|
||||||
|
title: 'Welcome!',
|
||||||
|
description: 'Start your registration process by clicking next.'
|
||||||
|
},
|
||||||
on: { NEXT: 'nameInput' }
|
on: { NEXT: 'nameInput' }
|
||||||
},
|
},
|
||||||
nameInput: {
|
nameInput: {
|
||||||
|
meta: {
|
||||||
|
title: 'Name Input',
|
||||||
|
description: 'Please enter your name.',
|
||||||
|
fieldLabel: 'Name'
|
||||||
|
},
|
||||||
|
entry: assign({
|
||||||
|
constraints: (context) => constraints.name || {}
|
||||||
|
}),
|
||||||
on: {
|
on: {
|
||||||
NEXT: {
|
NEXT: {
|
||||||
target: 'emailInput',
|
target: 'emailInput',
|
||||||
@ -52,6 +65,14 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
emailInput: {
|
emailInput: {
|
||||||
|
meta: {
|
||||||
|
title: 'Email Input',
|
||||||
|
description: 'Please enter your email address.',
|
||||||
|
fieldLabel: 'Email'
|
||||||
|
},
|
||||||
|
entry: assign({
|
||||||
|
constraints: (context) => constraints.email || {}
|
||||||
|
}),
|
||||||
on: {
|
on: {
|
||||||
NEXT: {
|
NEXT: {
|
||||||
target: 'summary',
|
target: 'summary',
|
||||||
@ -61,19 +82,45 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
summary: {
|
summary: {
|
||||||
|
meta: {
|
||||||
|
title: 'Summary',
|
||||||
|
description: 'Review your details before submission.'
|
||||||
|
},
|
||||||
on: {
|
on: {
|
||||||
SUBMIT: 'submitting'
|
SUBMIT: 'submitting'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
submitting: {
|
submitting: {
|
||||||
|
meta: {
|
||||||
|
title: 'Submitting...'
|
||||||
|
},
|
||||||
invoke: {
|
invoke: {
|
||||||
src: 'createUserService',
|
src: 'createUserService',
|
||||||
onDone: 'success',
|
onDone: 'success',
|
||||||
onError: 'failure'
|
onError: {
|
||||||
|
target: 'failure',
|
||||||
|
actions: assign({
|
||||||
|
error: (context, event) => event.data
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
success: {},
|
success: {
|
||||||
failure: {}
|
meta: {
|
||||||
|
title: 'Success'
|
||||||
|
},
|
||||||
|
on: {
|
||||||
|
RESTART: 'start'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
failure: {
|
||||||
|
meta: {
|
||||||
|
title: 'Submission Failed'
|
||||||
|
},
|
||||||
|
on: {
|
||||||
|
RESTART: 'start'
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -86,7 +133,10 @@
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
services: {
|
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 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
|
<h1 class="text-2xl">
|
||||||
|
{$state.value in stateMachine.states && stateMachine.states[$state.value].meta
|
||||||
|
? stateMachine.states[$state.value].meta.title
|
||||||
|
: 'Unknown state'}
|
||||||
|
</h1>
|
||||||
|
<p>
|
||||||
|
{$state.value in stateMachine.states ? stateMachine.states[$state.value].meta.description : ''}
|
||||||
|
</p>
|
||||||
|
|
||||||
{#if $state.value === 'start'}
|
{#if $state.value === 'start'}
|
||||||
<!-- Step 1 -->
|
<button class="px-4 py-2 mt-4 text-white bg-blue-500 rounded" on:click={() => send('NEXT')}>
|
||||||
<div>
|
Next
|
||||||
<h1 class="text-2xl">Step 1 - Start</h1>
|
</button>
|
||||||
<button class="px-4 py-2 mt-4 text-white bg-blue-500 rounded" on:click={() => send('NEXT')}>
|
{:else if $state.value === 'nameInput'}
|
||||||
|
<form on:submit|preventDefault={handleSubmit} class="w-full max-w-md">
|
||||||
|
<div class="mb-4">
|
||||||
|
{#if $errors.name}
|
||||||
|
<span class="block mb-2 font-semibold text-red-500">{$errors.name}</span>
|
||||||
|
{:else}
|
||||||
|
<label for="name" class="block mb-2 font-semibold text-white"
|
||||||
|
>{$state.value in stateMachine.states
|
||||||
|
? stateMachine.states[$state.value].meta.fieldLabel
|
||||||
|
: ''}</label
|
||||||
|
>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<input
|
||||||
|
name="name"
|
||||||
|
type="text"
|
||||||
|
class="w-full px-3 py-2 bg-transparent border-gray-100 rounded-md border-1 ring-0 ring-white focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||||
|
bind:value={$form.name}
|
||||||
|
aria-invalid={$errors.name ? 'true' : undefined}
|
||||||
|
{...$state.context.constraints}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
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"
|
||||||
|
disabled={$errors.name}
|
||||||
|
on:click={() => send('NEXT')}
|
||||||
|
>
|
||||||
Next
|
Next
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</form>
|
||||||
{:else if $state.value === 'nameInput'}
|
|
||||||
<!-- Step 2 -->
|
|
||||||
<div>
|
|
||||||
<h1 class="text-2xl">Step 2 - Name Input</h1>
|
|
||||||
<form on:submit|preventDefault={handleSubmit} class="w-full max-w-md">
|
|
||||||
<div class="mb-4">
|
|
||||||
{#if $errors.name}
|
|
||||||
<span class="block mb-2 font-semibold text-red-500">{$errors.name}</span>
|
|
||||||
{:else}
|
|
||||||
<label for="name" class="block mb-2 font-semibold text-white">Name</label>
|
|
||||||
{/if}
|
|
||||||
|
|
||||||
<input
|
|
||||||
name="name"
|
|
||||||
type="text"
|
|
||||||
class="w-full px-3 py-2 bg-transparent border-gray-100 rounded-md border-1 ring-0 ring-white focus:outline-none focus:ring-2 focus:ring-blue-500"
|
|
||||||
bind:value={$form.name}
|
|
||||||
aria-invalid={$errors.name ? 'true' : undefined}
|
|
||||||
{...constraints.name}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<button
|
|
||||||
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"
|
|
||||||
disabled={$errors.name}
|
|
||||||
on:click={() => send('NEXT')}
|
|
||||||
>
|
|
||||||
Next
|
|
||||||
</button>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
{:else if $state.value === 'emailInput'}
|
{:else if $state.value === 'emailInput'}
|
||||||
<div>
|
<form on:submit|preventDefault={handleSubmit} class="w-full max-w-md">
|
||||||
<h1 class="text-2xl">Step 3 - Email Input</h1>
|
<div class="mb-4">
|
||||||
<form on:submit|preventDefault={handleSubmit} class="w-full max-w-md">
|
{#if $errors.email}
|
||||||
<div class="mb-4">
|
<span class="block mb-2 font-semibold text-red-500">{$errors.email}</span>
|
||||||
{#if $errors.email}
|
{:else}
|
||||||
<span class="block mb-2 font-semibold text-red-500">{$errors.email}</span>
|
<label for="email" class="block mb-2 font-semibold text-white"
|
||||||
{:else}
|
>{$state.value in stateMachine.states
|
||||||
<label for="email" class="block mb-2 font-semibold text-white">Email</label>
|
? stateMachine.states[$state.value].meta.fieldLabel
|
||||||
{/if}
|
: ''}</label
|
||||||
|
>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<input
|
<input
|
||||||
name="email"
|
name="email"
|
||||||
type="email"
|
type="email"
|
||||||
class="w-full px-3 py-2 bg-transparent border-gray-100 rounded-md border-1 ring-0 ring-white focus:outline-none focus:ring-2 focus:ring-blue-500"
|
class="w-full px-3 py-2 bg-transparent border-gray-100 rounded-md border-1 ring-0 ring-white focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||||
bind:value={$form.email}
|
bind:value={$form.email}
|
||||||
aria-invalid={$errors.email ? 'true' : undefined}
|
aria-invalid={$errors.email ? 'true' : undefined}
|
||||||
{...constraints.email}
|
{...$state.context.constraints}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<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={$errors.email}
|
disabled={$errors.email}
|
||||||
on:click={() => send('NEXT')}
|
on:click={() => send('NEXT')}
|
||||||
>
|
>
|
||||||
Next
|
Next
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Add a Summary Section -->
|
|
||||||
{:else if $state.value === 'summary'}
|
{:else if $state.value === 'summary'}
|
||||||
<div>
|
<p>Name: {$form.name}</p>
|
||||||
<h1 class="text-2xl">Summary</h1>
|
<p>Email: {$form.email}</p>
|
||||||
<p>Name: {$form.name}</p>
|
<button
|
||||||
<p>Email: {$form.email}</p>
|
class="px-4 py-2 mt-4 text-white bg-blue-500 rounded"
|
||||||
<button class="px-4 py-2 mt-4 text-white bg-blue-500 rounded" on:click={() => send('SUBMIT')}>
|
on:click={() => {
|
||||||
Submit
|
console.log('Submit button clicked');
|
||||||
</button>
|
send('SUBMIT');
|
||||||
</div>
|
}}
|
||||||
|
>
|
||||||
|
Submit
|
||||||
|
</button>
|
||||||
{:else if $state.value === 'submitting'}
|
{:else if $state.value === 'submitting'}
|
||||||
<div>
|
<!-- You can optionally add a spinner or some visual indication here -->
|
||||||
<h1 class="text-2xl">Submitting...</h1>
|
|
||||||
</div>
|
|
||||||
{:else if $state.value === 'success'}
|
{:else if $state.value === 'success'}
|
||||||
<div>
|
<p>Thank you for your submission!</p>
|
||||||
<h1 class="text-2xl">User created successfully!</h1>
|
<button class="px-4 py-2 mt-4 text-white bg-green-500 rounded" on:click={() => send('RESTART')}>
|
||||||
<!-- You can add a button to reset the form or navigate to another page -->
|
Start Again
|
||||||
</div>
|
</button>
|
||||||
{:else if $state.value === 'failure'}
|
{:else if $state.value === 'failure'}
|
||||||
<div>
|
<p class="text-red-500">
|
||||||
<h1 class="text-2xl">Failed to create user. Please try again.</h1>
|
Error: {$state.context.error?.message || 'An unknown error occurred.'}
|
||||||
<button class="px-4 py-2 mt-4 text-white bg-red-500 rounded" on:click={() => send('SUBMIT')}>
|
</p>
|
||||||
Retry
|
<button class="px-4 py-2 mt-4 text-white bg-red-500 rounded" on:click={() => send('RESTART')}>
|
||||||
</button>
|
Try Again
|
||||||
</div>
|
</button>
|
||||||
{/if}
|
{/if}
|
||||||
</main>
|
</main>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user