Further improved the dynamic action area renderering

This commit is contained in:
Samuel Andert 2023-07-31 11:37:14 +02:00
parent 028614a6e0
commit 6092199fe5

View File

@ -5,6 +5,7 @@
import { TreeSchema } from '$lib/types/TreeSchema'; import { TreeSchema } from '$lib/types/TreeSchema';
import { writable, get } from 'svelte/store'; import { writable, get } from 'svelte/store';
import { createUser } from './userService'; import { createUser } from './userService';
import { derived } from 'svelte/store';
const initialFormData = { name: '', age: '' }; const initialFormData = { name: '', age: '' };
@ -150,27 +151,29 @@
const { state, send } = useMachine(stateMachine); const { state, send } = useMachine(stateMachine);
$: { $: {
// Reactively update the form validation status based on the errors
isFormValid.set(Object.keys(get(errors)).length === 0); isFormValid.set(Object.keys(get(errors)).length === 0);
} }
const possibleActions = derived(state, ($state) =>
Object.keys(stateMachine.states[$state.value]?.on || {})
);
</script> </script>
<main> <main class="flex items-center justify-center w-full h-full">
<div class="w-full max-w-lg">
<h1 class="text-2xl"> <h1 class="text-2xl">
{$state.value in stateMachine.states && stateMachine.states[$state.value].meta {$state.value in stateMachine.states && stateMachine.states[$state.value].meta
? stateMachine.states[$state.value].meta.title ? stateMachine.states[$state.value].meta.title
: 'Unknown state'} : 'Unknown state'}
</h1> </h1>
<p> <p>
{$state.value in stateMachine.states ? stateMachine.states[$state.value].meta.description : ''} {$state.value in stateMachine.states
? stateMachine.states[$state.value].meta.description
: ''}
</p> </p>
{#if $state.value === 'start'} {#if $state.value === 'start'}Welcome{:else if $state.value === 'name'}
<button class="px-4 py-2 mt-4 text-white bg-blue-500 rounded" on:click={() => send('NEXT')}> <form on:submit|preventDefault={handleSubmit} class="w-full max-w-lg">
Next
</button>
{:else if $state.value === 'name'}
<form on:submit|preventDefault={handleSubmit} class="w-full max-w-md">
<div class="mb-4"> <div class="mb-4">
{#if $errors.name} {#if $errors.name}
<span class="block mb-2 font-semibold text-red-500">{$errors.name}</span> <span class="block mb-2 font-semibold text-red-500">{$errors.name}</span>
@ -191,17 +194,9 @@
{...$state.context.constraints} {...$state.context.constraints}
/> />
</div> </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> </form>
{:else if $state.value === 'email'} {:else if $state.value === 'email'}
<form on:submit|preventDefault={handleSubmit} class="w-full max-w-md"> <form on:submit|preventDefault={handleSubmit} class="w-full max-w-lg">
<div class="mb-4"> <div class="mb-4">
{#if $errors.email} {#if $errors.email}
<span class="block mb-2 font-semibold text-red-500">{$errors.email}</span> <span class="block mb-2 font-semibold text-red-500">{$errors.email}</span>
@ -222,55 +217,57 @@
{...$state.context.constraints} {...$state.context.constraints}
/> />
</div> </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.email}
on:click={() => send('NEXT')}
>
Next
</button>
</form> </form>
{:else if $state.value === 'summary'} {:else if $state.value === 'summary'}
<p>Name: {$form.name}</p> <p>Name: {$form.name}</p>
<p>Email: {$form.email}</p> <p>Email: {$form.email}</p>
<button
class="px-4 py-2 mt-4 text-white bg-blue-500 rounded"
on:click={() => {
console.log('Submit button clicked');
send('SUBMIT');
}}
>
Submit
</button>
{:else if $state.value === 'submitting'} {:else if $state.value === 'submitting'}
<!-- You can optionally add a spinner or some visual indication here --> Add spinner here
{:else if $state.value === 'success'} {:else if $state.value === 'success'}
<p>Thank you for your submission!</p> <p>Thank you for your submission!</p>
<button class="px-4 py-2 mt-4 text-white bg-green-500 rounded" on:click={() => send('RESTART')}>
Start Again
</button>
{:else if $state.value === 'failure'} {:else if $state.value === 'failure'}
<p class="text-red-500"> <p class="text-red-500">
Error: {$state.context.error?.message || 'An unknown error occurred.'} Error: {$state.context.error?.message || 'An unknown error occurred.'}
</p> </p>
<button class="px-4 py-2 mt-4 text-white bg-red-500 rounded" on:click={() => send('RESTART')}>
Try Again
</button>
{/if} {/if}
</main> {#if possibleActions}
<div class="flex justify-between">
<!-- this is our new button --> {#each $possibleActions as action (action)}
<div> {#if action === 'BACK'}
{#if $state.value in stateMachine.states && stateMachine.states[$state.value].on.NEXT}
<button <button
class="px-4 py-2 mt-4 text-white bg-blue-500 rounded" class="px-4 py-2 mt-4 text-white bg-blue-500 rounded"
on:click={() => send('NEXT')} on:click={() => send(action)}
disabled={$errors[$state.value]} >
{action}
</button>
{/if}
{/each}
{#each $possibleActions as action (action)}
{#if action !== 'NEXT' && action !== 'BACK'}
<button
class="px-4 py-2 mx-auto mt-4 text-white bg-blue-500 rounded"
on:click={() => send(action)}
>
{action}
</button>
{/if}
{/each}
{#each $possibleActions as action (action)}
{#if action === 'NEXT'}
<button
class="px-4 py-2 mt-4 text-white bg-blue-500 rounded"
on:click={() => send(action)}
disabled={$errors[$state.value] || $state.context.constraints[$state.value]}
> >
{$state.value in stateMachine.states {$state.value in stateMachine.states
? stateMachine.states[$state.value].meta.buttonLabel ? stateMachine.states[$state.value].meta.buttonLabel
: ''} : ''}
</button> </button>
{/if} {/if}
</div> {/each}
</div>
{/if}
</div>
</main>