11import { assign , send } from 'xstate'
22import * as G from 'typings/graphql'
33import * as CR from 'typings'
4- import * as storage from '../storage'
54import * as selectors from '../../selectors'
65
76export default {
7+ continueTutorial : ( context : CR . MachineContext , event : CR . MachineEvent ) => {
8+
9+ const { tutorial, stepProgress} = event . data . payload
10+
11+ const progress : CR . Progress = {
12+ steps : stepProgress ,
13+ stages : { } ,
14+ levels : { } ,
15+ complete : false
16+ }
17+
18+ const position : CR . Position = {
19+ stepId : '' ,
20+ stageId : '' ,
21+ levelId : '' ,
22+ }
23+
24+ // calculate progress from tutorial & stepProgress
25+ for ( const level of tutorial . version . levels ) {
26+ for ( const stage of level . stages ) {
27+ // set stage progress
28+ const stageComplete : boolean = stage . steps . every ( ( step : G . Step ) => {
29+ return stepProgress [ step . id ]
30+ } )
31+ if ( stageComplete ) {
32+ progress . stages [ stage . id ] = true
33+ } else if ( ! position . stageId . length ) {
34+ // set stage amd step position
35+ position . stageId = stage . id
36+ position . stepId = stage . steps . find ( ( step : G . Step ) => ! stepProgress [ step . id ] ) . id
37+ }
38+ }
39+ // set level progress
40+ const levelComplete : boolean = level . stages . every ( ( stage : G . Stage ) => {
41+ return progress . stages [ stage . id ]
42+ } )
43+ if ( levelComplete ) {
44+ progress . levels [ level . id ] = true
45+ } else if ( ! position . levelId . length ) {
46+ position . levelId = level . id
47+ }
48+ }
49+ // set tutorial progress
50+ progress . complete = tutorial . version . levels . every ( ( level : G . Level ) => {
51+ return progress . levels [ level . id ]
52+ } )
53+
54+ return assign ( {
55+ tutorial,
56+ progress,
57+ position,
58+ } )
59+ } ,
860 setTutorial : assign ( {
961 tutorial : ( context : CR . MachineContext , event : CR . MachineEvent ) : any => {
1062 const { tutorial} = event . payload
11- storage . tutorial . set ( tutorial )
1263 return tutorial
1364 } ,
1465 } ) ,
15- continueTutorial : assign ( {
16- tutorial : ( context : CR . MachineContext , event : CR . MachineEvent ) => event . data . payload . tutorial ,
17- progress : ( context : CR . MachineContext , event : CR . MachineEvent ) => event . data . payload . progress ,
18- position : ( context : CR . MachineContext , event : CR . MachineEvent ) => event . data . payload . position ,
19- } ) ,
2066 // @ts -ignore
2167 initPosition : assign ( {
2268 position : ( context : CR . MachineContext , event : CR . MachineEvent ) : CR . Position => {
2369 const position : CR . Position = selectors . initialPosition ( event . payload )
24- storage . position . set ( position )
2570 return position
2671 } ,
2772 } ) ,
@@ -51,8 +96,6 @@ export default {
5196 stepId : step . id
5297 }
5398
54- storage . position . set ( nextPosition )
55-
5699 return nextPosition
57100 } ,
58101 } ) ,
@@ -73,8 +116,6 @@ export default {
73116 stepId : stage . steps [ 0 ] . id ,
74117 }
75118
76- storage . position . set ( nextPosition )
77-
78119 return nextPosition
79120 } ,
80121 } ) ,
@@ -96,8 +137,6 @@ export default {
96137 stepId : level . stages [ 0 ] . steps [ 0 ] . id ,
97138 }
98139
99- storage . position . set ( nextPosition )
100-
101140 return nextPosition
102141 } ,
103142 } ) ,
@@ -111,8 +150,6 @@ export default {
111150
112151 currentProgress . steps [ stepId ] = true
113152
114- storage . progress . set ( currentProgress )
115-
116153 return currentProgress
117154 } ,
118155 } ) ,
@@ -126,8 +163,6 @@ export default {
126163
127164 progress . stages [ stageId ] = true
128165
129- storage . progress . set ( progress )
130-
131166 return progress
132167 } ,
133168 } ) ,
@@ -228,17 +263,14 @@ export default {
228263 } ) ,
229264 reset : assign ( {
230265 tutorial ( ) {
231- storage . tutorial . set ( null )
232266 return null
233267 } ,
234268 progress ( ) : CR . Progress {
235269 const progress : CR . Progress = selectors . defaultProgress ( )
236- storage . progress . set ( progress )
237270 return progress
238271 } ,
239272 position ( ) : CR . Position {
240273 const position : CR . Position = selectors . defaultPosition ( )
241- storage . position . set ( position )
242274 return position
243275 }
244276 } )
0 commit comments