8000 add level/stage/step selectors · jordanliu/coderoad-vscode@3f88cf1 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3f88cf1

Browse files
committed
add level/stage/step selectors
1 parent f971eb0 commit 3f88cf1

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

web-app/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web-app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
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
},
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+

0 commit comments

Comments
 (0)
0