File tree Expand file tree Collapse file tree 3 files changed +50
-0
lines changed Expand file tree Collapse file tree 3 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 3838 "react" : " ^16.9.0" ,
3939 "react-dom" : " ^16.9.0" ,
4040 "react-scripts" : " ^3.1.1" ,
41+ "reselect" : " ^4.0.0" ,
4142 "typescript" : " ^3.6.2" ,
4243 "xstate" : " ^4.6.7"
4344 },
Original file line number Diff line number Diff line change 1+ import { MachineContext } from 'typings'
2+ import * as G from 'typings/graphql'
3+ import { createSelector } from 'reselect'
4+
5+ export const currentLevel = ( { tutorial, position} : MachineContext ) : G . Level => {
6+ if ( ! tutorial ) {
7+ throw new Error ( 'Tutorial not found when selecting level' )
8+ }
9+ // merge in the updated position
10+ // sent with the test to ensure consistency
11+ const levels : G . Level [ ] = tutorial . version . levels
12+
13+ const level : G . Level | undefined = levels . find ( ( l : G . Level ) => l . id === position . levelId )
14+
15+ if ( ! level ) {
16+ throw new Error ( 'Level not found when selecting level' )
17+ }
18+ return level
19+ }
20+
21+ export const currentStage = ( context : MachineContext ) : G . Stage => createSelector (
22+ currentLevel ,
23+ ( level : G . Level ) : G . Stage => {
24+ const stages : G . Stage [ ] = level . stages
25+ const stage : G . Stage | undefined = stages . find ( ( s : G . Stage ) => s . id === context . position . stageId )
26+ if ( ! stage ) {
27+ throw new Error ( 'No Stage found' )
28+ }
29+ return stage
30+ }
31+ ) ( context )
32+
33+ export const currentStep = ( context : MachineContext ) : G . Step => createSelector (
34+ currentStage ,
35+ ( stage : G . Stage ) : G . Step => {
36+ const steps : G . Step [ ] = stage . steps
37+ const step : G . Step | undefined = steps . find ( ( s : G . Step ) => s . id === context . position . stepId )
38+ if ( ! step ) {
39+ throw new Error ( 'No Step found' )
40+ }
41+ return step
42+ }
43+ ) ( context )
44+
You can’t perform that action at this time.
0 commit comments