small refactoring of oForm machine

This commit is contained in:
Samuel Andert
2023-08-01 17:59:17 +02:00
parent 1716cb41f7
commit 48903fbe7d
4 changed files with 64 additions and 3 deletions

View File

@ -4,6 +4,8 @@
import { afterUpdate } from 'svelte';
import { writable } from 'svelte/store';
import { RangeSlider, SlideToggle } from '@skeletonlabs/skeleton';
import { interpret } from 'xstate';
import { formMachine } from './machines/formMachine';
export let store;
export let services;
@ -32,6 +34,14 @@
const successMessage = writable<string | null>(null);
// Create a service to interpret the machine
const validationService = interpret(formMachine).start();
// Subscribe to state changes
validationService.subscribe((state) => {
$store.isValidated = state.context.isValidated;
});
// Update the isValidated property of the store whenever the errors object changes
$: {
$store.isValidated = !(
@ -41,6 +51,13 @@
$errors.age ||
$errors.favoriteFood
);
// Send events to the state machine based on the validation status
if ($store.isValidated) {
validationService.send('VALIDATE');
} else {
validationService.send('INVALIDATE');
}
}
async function handleSubmit() {