@@ -2,20 +2,50 @@ import {send} from 'xstate'
22// import {machine} from '../../extension'
33// import {cache} from '../../services/apollo'
44// import {editorDispatch} from '../../services/vscode'
5- // import * as CR from 'typings'
6- // import * as G from 'typings/graphql'
5+ import * as CR from 'typings'
6+ import * as G from 'typings/graphql'
77// import tutorialConfig from '../../services/apollo/queries/tutorialConfig'
88import editorActions from './editor'
99import contextActions from './context'
1010
1111export default {
12- newOrContinue : send ( ( context ) : 'NEW' | 'CONTINUE' => {
12+ newOrContinue : send ( ( context : CR . MachineContext ) : 'NEW' | 'CONTINUE' => {
1313 console . log ( 'new or continue' )
1414
1515 // TODO: verify that the user has an existing tutorial to continue
1616 const hasExistingTutorial : boolean = false
1717 return hasExistingTutorial ? 'CONTINUE' : 'NEW'
1818 } ) ,
19+ stepNext : send ( ( context : CR . MachineContext ) : CR . Action => {
20+ const { tutorial, position, progress} = context
21+ // TODO: protect against errors
22+ const steps : G . Step [ ] = tutorial . version
23+ . levels . find ( ( l : G . Level ) => l . id === position . levelId )
24+ . stages . find ( ( s : G . Stage ) => s . id === position . stageId )
25+ . steps
26+
27+ // TODO: verify not -1
28+ const stepIndex = steps . findIndex ( ( s : G . Step ) => s . id === position . stepId )
29+ const finalStep = stepIndex === steps . length - 1
30+ const stepComplete = progress . steps [ position . stepId ]
31+ // not final step, or final step but not complete
32+ const hasNextStep = ! finalStep || ! stepComplete
33+
34+ if ( hasNextStep ) {
35+ const nextStep = steps [ stepIndex + 1 ]
36+ return {
37+ type : 'LOAD_NEXT_STEP' ,
38+ payload : {
39+ step : nextStep
40+ }
41+ }
42+ } else {
43+ return {
44+ type : 'STAGE_COMPLETE'
45+ }
46+ }
47+
48+ } ) ,
1949 ...editorActions ,
2050 ...contextActions ,
2151}
0 commit comments