@@ -2,6 +2,7 @@ import {assign, send} from 'xstate'
22import * as G from 'typings/graphql'
33import * as CR from 'typings'
44import * as storage from '../storage'
5+ import * as selectors from '../../selectors'
56
67export default {
78setTutorial :assign ( {
@@ -39,19 +40,11 @@ export default {
3940//@ts -ignore
4041updateStepPosition :assign ( {
4142position :( context :CR . MachineContext , event :CR . MachineEvent ) :CR . Position => {
42- const { tutorial, position} = context
43-
44- if ( ! tutorial ) {
45- throw new Error ( 'Tutorial not found when updating step position' )
46- }
43+ const { position} = context
4744// merge in the updated position
4845// sent with the test to ensure consistency
49- //@ts -ignore
50- const steps :G . Step [ ] = context . tutorial . version
51- . levels . find ( ( l :G . Level ) => l . id === position . levelId )
52- . stages . find ( ( s :G . Stage ) => s . id === position . stageId )
53- . steps
54-
46+ const stage :G . Stage = selectors . currentStage ( context )
47+ const steps :G . Step [ ] = stage . steps
5548
5649// final step but not completed
5750if ( steps [ steps . length - 1 ] . id === position . stepId ) {
@@ -74,18 +67,11 @@ export default {
7467} ) ,
7568//@ts -ignore
7669updateStagePosition :assign ( {
77- position :( context :CR . MachineContext , event : CR . MachineEvent ) :CR . Position => {
78- const { tutorial , position} = context
70+ position :( context :CR . MachineContext ) :CR . Position => {
71+ const { position} = context
7972
80- if ( ! tutorial ) {
81- throw new Error ( 'Tutorial not found when updating stage position' )
82- }
83- // merge in the updated position
84- // sent with the test to ensure consistency
85- //@ts -ignore
86- const stages :G . Stage [ ] = tutorial . version
87- . levels . find ( ( l :G . Level ) => l . id === position . levelId )
88- . stages
73+ const level :G . Level = selectors . currentLevel ( context )
74+ const stages :G . Stage [ ] = level . stages
8975
9076const stageIndex = stages . findIndex ( ( s :G . Stage ) => s . id === position . stageId )
9177const stage :G . Stage = stages [ stageIndex + 1 ]
@@ -103,15 +89,12 @@ export default {
10389} ) ,
10490//@ts -ignore
10591updateLevelPosition :assign ( {
106- position :( context :CR . MachineContext , event :CR . MachineEvent ) :CR . Position => {
107- const { tutorial, position} = context
108-
109- if ( ! tutorial ) {
110- throw new Error ( 'Tutorial not found when updating level position' )
111- }
92+ position :( context :CR . MachineContext ) :CR . Position => {
93+ const { position} = context
94+ const version = selectors . currentVersion ( context )
11295// merge in the updated position
11396// sent with the test to ensure consistency
114- const levels :G . Level [ ] = tutorial . version . levels
97+ const levels :G . Level [ ] = version . levels
11598
11699const levelIndex = levels . findIndex ( ( l :G . Level ) => l . id === position . levelId )
117100const level :G . Level = levels [ levelIndex + 1 ]
@@ -164,23 +147,12 @@ export default {
164147}
165148} ) ,
166149loadNext :send ( ( context :CR . MachineContext ) :CR . Action => {
167- const { tutorial , position, progress} = context
150+ const { position, progress} = context
168151
169- if ( ! tutorial ) {
170- throw new Error ( 'No tutorial found for loading next step' )
171- }
152+ const version = selectors . currentVersion ( context )
153+ const level = selectors . currentLevel ( context )
154+ const stage = selectors . currentStage ( context )
172155
173- // has next step?
174- const levels :G . Level [ ] = tutorial . version . levels
175- const level :G . Level | undefined = levels . find ( ( l :G . Level ) => l . id === position . levelId )
176- if ( ! level ) {
177- throw new Error ( 'No Level found' )
178- }
179- const stages :G . Stage [ ] = level . stages
180- const stage :G . Stage | undefined = stages . find ( ( s :G . Stage ) => s . id === position . stageId )
181- if ( ! stage ) {
182- throw new Error ( 'No Stage found' )
183- }
184156const steps :G . Step [ ] = stage . steps
185157
186158const stepIndex = steps . findIndex ( ( s :G . Step ) => s . id === position . stepId )
@@ -197,6 +169,7 @@ export default {
197169
198170// has next stage?
199171
172+ const { stages} = level
200173const stageIndex = stages . findIndex ( ( s :G . Stage ) => s . id === position . stageId )
201174const finalStage = ( stageIndex > - 1 && stageIndex === stages . length - 1 )
202175const hasNextStage = ( ! finalStage )
@@ -214,6 +187,7 @@ export default {
214187
215188// has next level?
216189
190+ const { levels} = version
217191const levelIndex = levels . findIndex ( ( l :G . Level ) => l . id === position . levelId )
218192const finalLevel = ( levelIndex > - 1 && levelIndex === levels . length - 1 )
219193const hasNextLevel = ( ! finalLevel )
@@ -234,19 +208,11 @@ export default {
234208return { type :'COMPLETED' }
235209} ) ,
236210stepNext :send ( ( context :CR . MachineContext ) :CR . Action => {
237- const { tutorial, position, progress} = context
238-
239- if ( ! tutorial || ! tutorial . version ) {
240- throw new Error ( 'No tutorial found when loading next step' )
241- }
211+ const { position, progress} = context
242212
243- // TODO: protect against errors
244- //@ts -ignore
245- const steps :G . Step [ ] = tutorial . version
246- . levels . find ( ( l :G . Level ) => l . id === position . levelId )
247- . stages . find ( ( s :G . Stage ) => s . id === position . stageId )
248- . steps
213+ const stage :G . Stage = selectors . currentStage ( context )
249214
215+ const { steps} = stage
250216// TODO: verify not -1
251217const stepIndex = steps . findIndex ( ( s :G . Step ) => s . id === position . stepId )
252218const finalStep = stepIndex === steps . length - 1