import { hierarchyMachine } from '$lib/@iioioo_utils/State/hierarchy.machine'; import type { IFieldBase, INest } from '$lib/Models'; import { createMachine, assign, send, sendParent, spawn } from 'xstate'; // CONTEXT export type NestContext = { hierarchyMachine: any; field: INest; fieldValueMap: { [key:string]: any }; potentialParent: any; potentialChildren: any[]; } export type NestState = { value: 'general'; context: NestContext } | { value: 'inSelection'; context: NestContext }; const rootId = 'rootId'; export const nestMachine = /** @xstate-layout N4IgpgJg5mDOIC5QDs4BcCyBDAxgCwEtUA6IgtArAGwIC8ioBiRUABwHtZyD3kWQAHogBMAdgCcxABwBGUVIAsC0QAYVMgGxTxAGhABPERoXEAzKICsUqaYXjRG009EBfF3tSxMuQiRioAJ2pGAEkAFQBRDAB9AGUIgBkIgGFIgBF+Di4KXn4hBFElYnExFVUZUw0LBTE9QwQZFUkFCxLhDVFhKQtWxzcPdGx8IjBSZFiwKjAcHORQyJj4pNSIjKQQLO5c9fyW4lFTexUqlWcbUTrEFpNhYXEqxvFrCzEFfpBPb2GSIgmpmZ4cwAqgA5ACCsViIQA4iDogAFADykRBYRCYISCLBACUIqjomCQWloskABIhBJpXEgzKcLZ8HYiCTSOSKZRqTTaS4IQ6ScytU53LoycQyd6fIa+Ua-SbTWaMMFpYnwnF4sIk8mU6nRXEJMFoxEg2Lk+GxWnZQF5RCmU7FeSnQ41GQtdrcjTOlkqKwSUSWU7icWDHwjMZ-OWAxigiFQ2EI5Fq9GYskUtZsOmzK0IPlSU52JrifONC4GETiExNZT2OwvOxyQNeSUjRi4gBiuON0RbIUSaTN602GcZCAUMmExFrOfkcic1gU3IAtKZisJjipZGvGrJ2hY3O4PuwIHB+BLgz9kNxqHQGOb6ZnTDJx6cJB1RJphBZRxZuUXiFoPz0SjUHNhFMesvilYh-DAIIqBvQdQHySdiHfZRjBUEdTG6GQ3TkaQ1y9BQbUqcQnDAxszzDAFtjTC1qMERALA0Yga20UxqisbQNFdEsEHdUQ8JzaoiI0EjhDI08wDgy0h3nD13XuCwmjUAs1xI7lbF-LCak6UwunuANdyAA */ createMachine( { tsTypes: {} as import('./nest2.machine.typegen').Typegen0, schema: { context: {} as NestContext, events: {} as | { type: 'REFRESH_FIELDS'; fieldMap: any } | { type: 'ITEM_SELECTED'; item: any } | { type: 'ASSIGN_POTENTIAL_PARENT'; item: any } | { type: 'ASSIGN_POTENTIAL_CHILD'; item: any } | { type: 'UNASSIGN_POTENTIAL_PARENT_AND_CHILDREN'; item: any } | { type: 'UNASSIGN_POTENTIAL_CHILD'; item: any } | { type: 'ADD_PARENT_CHILDREN_RELATIONSHIPS' } | { type: 'REMOVE_PARENT_CHILD_RELATIONSHIP' } | { type: 'REMOVE_ALL_PARENT_CHILD_RELATIONSHIPS' } | { type: 'REMOVE_NODE_FROM_GRAPH' } // | { type: 'REFRESH_DATA', data: any[] } }, context: { hierarchyMachine: null, field: null, fieldValueMap: {}, potentialParent: null, potentialChildren: [] }, id: 'nestMachine', initial: 'initializing', states: { initializing: { entry: ['assignHierarchyMachine'], always: { target: 'general' } }, general: { on: { ITEM_SELECTED: { actions: 'assignPotentialParent', target: 'inSelection' } } }, inSelection: { on: { ITEM_SELECTED: { actions: [ 'assignPotentialChild' ] }, UNASSIGN_POTENTIAL_PARENT_AND_CHILDREN: { target: 'general', actions: [ 'unassignPotentialParentAndChildren' ] }, UNASSIGN_POTENTIAL_CHILD: { actions: [ 'unassignPotentialChild' ] }, ADD_PARENT_CHILDREN_RELATIONSHIPS: { actions: [ 'addParentChildrenRelationships', 'unassignPotentialParentAndChildren', 'notifyHierarchyMachine', 'notifyParent' ], target: 'general' } } } }, on: { REFRESH_FIELDS: { actions: ['refreshFields', 'assignHierarchyMachine'] } } }, { actions: { assignHierarchyMachine: assign({ hierarchyMachine: (context, event) => { return spawn( hierarchyMachine.withContext({ ...hierarchyMachine.context, data: context.field.value, publishDraftResult: true, hasParent: true, notifyParentEventType: 'STREAM_NEST_RESULT' }), { name: 'streamNestMachine', sync: true } ) } } ), // broadcastFields: assign({ // field: (context, event) => { // console.log("running after you: ", context.field, event); // // return { ...context.field, value: [ ...context.field.value, event.fieldMap ] } // return context.field; // } // }), refreshFields: assign((context, event) => { // return { ...context.field, value: [ ...context.field.value, event.fieldMap ] } Object.values(event.fieldMap).forEach((val: IFieldBase) => { val.parentId = !!context.fieldValueMap[ val.id ] ? context.fieldValueMap[ val.id ].parentId : ''; }); context.fieldValueMap = event.fieldMap; context.field.value = Object.values(context.fieldValueMap); console.log("running after you: ", context, event); return context; }), // refreshHierarchy: send('REFRESH_DATA', { to: (context, event) => context.hierarchyMachine }), assignPotentialParent: assign({ potentialParent: (context, event) => event.item }), unassignPotentialParentAndChildren: assign({ potentialParent: () => null, potentialChildren: () => [] }), assignPotentialChild: assign({ potentialChildren: (context, event) => [ ...context.potentialChildren, event.item ] }), unassignPotentialChild: assign({ potentialChildren: (context, event) => context.potentialChildren.filter(n => n.id !== event.item.id) }), addParentChildrenRelationships: assign((context, event) => { const newFieldValueMap = context.field.value.reduce((acc, curr) => ({ ...acc, [ curr.id ]: curr }), {}); const fieldValueMap = { ...newFieldValueMap, ...context.fieldValueMap }; context.potentialChildren.forEach(child => { child.parentId = context.potentialParent.id; fieldValueMap[ child.id ] = child; }); context.field.value = Object.values(fieldValueMap); context.fieldValueMap = fieldValueMap; return context; }), // assign({ // field: (context, event) => { // context.potentialChildren.forEach(child => { // child.parentId = context.potentialParent.id; // }); // return context.field; // } // field: (context, event) => { // context.field.value = context.potentialChildren.map(child => { // child.parentId = context.potentialParent.id; // return child; // }); // return context.field; // } // }), notifyHierarchyMachine: send({ type: 'REFRESH_DATA' }, { to: (context, event) => context.hierarchyMachine }), notifyParent: sendParent( ( context, event ) => { console.log("notify parent in nest machine: ", context, event); return { type: 'CHANGE', value: context.field }; }) } });