import { hierarchyMachine } from '$lib/@iioioo_utils/State/hierarchy.machine'; import type { ChoiceModel, FormModel, IArray, IFieldBase, TextModel } from '$lib/Models'; import { createMachine, assign, send, sendParent, spawn } from 'xstate'; // CONTEXT export type ArrayContext = { field: IArray; hierarchyMachine: any; } export type ArrayState = { value: 'building'; context: ArrayContext } | { value: 'submitted'; context: ArrayContext }; export const arrayMachine = createMachine( { tsTypes: {} as import("./array.machine.typegen").Typegen0, schema: { context: {} as ArrayContext, events: {} as { type: 'SUBMIT_ARRAY'; item: any } | { type: 'HIERARCHY_SUBMITTED', data: any } }, context: { field: null, hierarchyMachine: null }, id: 'arrayMachine', initial: 'building', states: { building: { entry: [ 'assignHierarchyMachine' ], on: { SUBMIT_ARRAY: 'submitted', HIERARCHY_SUBMITTED: { actions: [ (c,e) => console.log("IN ARRAY BUILDING: ", c, e), 'assignNewItem', 'notifyParent', 'assignHierarchyMachine' ] } } }, submitted: { entry: [ 'notifyParent' ], type: 'final' } } }, { actions: { assignHierarchyMachine: assign({ hierarchyMachine: (context, event) => { return spawn( hierarchyMachine.withContext({ ...hierarchyMachine.context, data: context.field.classExample(), // publishDraftResult: true, hasParent: true }), { name: 'array_hierarchyMachineId', sync: true } ) }, }), assignNewItem: assign({ field: (context, event) => { console.log("ASSIGN NEW ITEM IN ARRAY MACHINE: ", context.field, event.data); const newField = context.field.wrangleResult( event.data ); context.field.value = [ ...context.field.value, newField ]; return context.field; } }), notifyParent: sendParent( ( context, event ) => { console.log("notify parent in array machine: ", context, event); return { type: 'CHANGE', value: context.field }; }) } });