@@ -17,7 +17,6 @@ const contextActions: ActionFunctionMap<T.MachineContext, T.MachineEvent> = {
1717//@ts -ignore
1818storeContinuedTutorial :assign ( {
1919tutorial :( context :T . MachineContext , event :T . MachineEvent ) => {
20- console . log ( 'storeContinuedTutorial' )
2120return event . payload . tutorial
2221} ,
2322progress :( context :T . MachineContext , event :T . MachineEvent ) => {
@@ -132,25 +131,27 @@ const contextActions: ActionFunctionMap<T.MachineContext, T.MachineEvent> = {
132131
133132const steps :G . Step [ ] = level . steps
134133
135- const stepIndex = steps . findIndex ( ( s :G . Step ) => s . id === position . stepId )
136- const stepComplete = progress . steps [ position . stepId ]
137- const finalStep = stepIndex > - 1 && stepIndex === steps . length - 1
138- const hasNextStep = ! finalStep && ! stepComplete
139-
140- // NEXT STEP
141- if ( hasNextStep ) {
142- const nextPosition = { ...position , stepId :steps [ stepIndex + 1 ] . id }
143- return { type :'NEXT_STEP' , payload :{ position :nextPosition } }
144- }
134+ if ( steps . length && position . stepId ) {
135+ const stepIndex = steps . findIndex ( ( s :G . Step ) => s . id === position . stepId )
136+ const stepComplete = progress . steps [ position . stepId ]
137+ const finalStep = stepIndex > - 1 && stepIndex === steps . length - 1
138+ const hasNextStep = ! finalStep && ! stepComplete
145139
146- // has next level?
140+ // NEXT STEP
141+ if ( hasNextStep ) {
142+ const nextPosition = { ...position , stepId :steps [ stepIndex + 1 ] . id }
143+ return { type :'NEXT_STEP' , payload :{ position :nextPosition } }
144+ }
147145
148- if ( ! context . tutorial ) {
149- const error = new Error ( 'Tutorial not found' )
150- onError ( error )
151- throw error
146+ // has next level?
147+ if ( ! context . tutorial ) {
148+ const error = new Error ( 'Tutorial not found' )
149+ onError ( error )
150+ throw error
151+ }
152152}
153153
154+ //@ts -ignore
154155const levels = context . tutorial . version . data . levels || [ ]
155156const levelIndex = levels . findIndex ( ( l :G . Level ) => l . id === position . levelId )
156157const finalLevel = levelIndex > - 1 && levelIndex === levels . length - 1
@@ -177,26 +178,27 @@ const contextActions: ActionFunctionMap<T.MachineContext, T.MachineEvent> = {
177178const level :G . Level = selectors . currentLevel ( context )
178179
179180const { steps} = level
180- // TODO verify not -1
181- const stepIndex = steps . findIndex ( ( s :G . Step ) => s . id === position . stepId )
182- const finalStep = stepIndex === steps . length - 1
183- const stepComplete = progress . steps [ position . stepId ]
184- // not final step, or final step but not complete
185- const hasNextStep = ! finalStep || ! stepComplete
186-
187- if ( hasNextStep ) {
188- const nextStep = steps [ stepIndex + 1 ]
189- return {
190- type :'LOAD_NEXT_STEP' ,
191- payload :{
192- step :nextStep ,
193- } ,
194- }
195- } else {
196- return {
197- type :'LEVEL_COMPLETE' ,
181+
182+ if ( steps . length && position . stepId ) {
183+ const stepIndex = steps . findIndex ( ( s :G . Step ) => s . id === position . stepId )
184+ const finalStep = stepIndex === steps . length - 1
185+ const stepComplete = progress . steps [ position . stepId ]
186+ // not final step, or final step but not complete
187+ const hasNextStep = ! finalStep || ! stepComplete
188+
189+ if ( hasNextStep ) {
190+ const nextStep = steps [ stepIndex + 1 ]
191+ return {
192+ type :'LOAD_NEXT_STEP' ,
193+ payload :{
194+ step :nextStep ,
195+ } ,
196+ }
198197}
199198}
199+ return {
200+ type :'LEVEL_COMPLETE' ,
201+ }
200202} ,
201203) ,
202204//@ts -ignore
@@ -223,6 +225,13 @@ const contextActions: ActionFunctionMap<T.MachineContext, T.MachineEvent> = {
223225return message
224226} ,
225227} ) ,
228+ //@ts -ignore
229+ checkEmptySteps :send ( ( context :T . MachineContext ) => {
230+ // no step id indicates no steps to complete
231+ return {
232+ type :context . position . stepId === null ?'START_COMPLETED_LEVEL' :'START_LEVEL' ,
233+ }
234+ } ) ,
226235}
227236
228237export default contextActions