Last active
August 2, 2022 05:09
-
-
Save eiberham/ac50a61eb82fe7196bb754c602a44086 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const steps = [ | |
{ name : 'hospital', required : true }, | |
{ name : 'building', required : true }, | |
{ name : 'floor', required : true } | |
] | |
const check = () => { | |
console.log('check called') | |
const userId = 'user-id' | |
return steps.reduce( ( acc, val ) => { | |
const step = JSON.parse(JSON.parse(localStorage.getItem( `core.wizard/${userId}/${val.name}` ))) | |
if ( !step ) return acc | |
return { | |
...acc, | |
[val.name] : ( context, event ) => { | |
return { | |
...context[val.name], | |
done : step?.done || false | |
} | |
} | |
} | |
}, {} ) | |
/*return { | |
hospital: (cxt, evt) => ({...cxt.hospital, done: true }), | |
building: (cxt, evt) => ({...cxt.building, done: true }) | |
}*/ | |
} | |
const wizard = Machine( { | |
id : 'wizard', | |
initial : 'start', | |
context : { | |
error : undefined, | |
hospital : { done : false, required : true }, | |
building : { done : false, required : true }, | |
floor : { done : false, required : true }, | |
}, | |
states : { | |
start : { | |
on : { | |
START : { | |
target : 'start' | |
} | |
}, | |
invoke : { | |
id : 'get-last-step', | |
src : ( context, event ) => { | |
return new Promise( ( resolve, reject ) => { | |
try { | |
// gets the step where the user had left the setup e.g hospital | |
resolve( 'hospital' ) | |
} catch ( e ) { | |
reject( e ) | |
} | |
} ) | |
}, | |
onDone : [ | |
{ | |
cond : ( _, event ) => event.data === 'hospital', | |
target : 'hospital', | |
actions : 'update' | |
} | |
], | |
onError : { | |
target : 'start' | |
} | |
} | |
}, | |
hospital : { | |
on : { | |
NEXT : { | |
target : 'building', | |
}, | |
START : { | |
target : 'start' | |
}, | |
}, | |
}, | |
building : { | |
on : { | |
PREV : { | |
target : 'hospital', | |
}, | |
NEXT : { | |
target : 'floor', | |
} | |
}, | |
entry : 'update' | |
}, | |
floor : { | |
on : { | |
PREV : { | |
target : 'building', | |
}, | |
NEXT : { | |
target : 'end' | |
} | |
}, | |
entry : 'update' | |
}, | |
end : { | |
type : 'final' | |
} | |
} | |
}, { | |
actions : { | |
update : assign( check() ), | |
// update: (cxt, evt) => console.log('update triggered') | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment