Skip to content

Commit 923ea37

Browse files
committed
replace stepNext guards with actions
1 parent 95f6f73 commit 923ea37

File tree

4 files changed

+44
-58
lines changed

4 files changed

+44
-58
lines changed

web-app/src/components/Router/index.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import debuggerWrapper from '../Debugger/debuggerWrapper'
88
import channel from '../../services/channel'
99
import messageBusReceiver from '../../services/channel/receiver'
1010
import actions from '../../services/state/actions'
11-
import guards from '../../services/state/guards'
1211

1312

1413
interface Props {
@@ -24,7 +23,6 @@ interface CloneElementProps {
2423
const Router = ({ children }: Props): React.ReactElement<CloneElementProps>|null => {
2524
const [state, send] = useMachine(machine, {
2625
actions,
27-
guards,
2826
interpreterOptions: {
2927
logger: console.log.bind('XSTATE:')
3028
}

web-app/src/services/state/actions/index.ts

+33-3
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,50 @@ import {send} from 'xstate'
22
// import {machine} from '../../extension'
33
// import {cache} from '../../services/apollo'
44
// import {editorDispatch} from '../../services/vscode'
5-
// import * as CR from 'typings'
6-
// import * as G from 'typings/graphql'
5+
import * as CR from 'typings'
6+
import * as G from 'typings/graphql'
77
// import tutorialConfig from '../../services/apollo/queries/tutorialConfig'
88
import editorActions from './editor'
99
import contextActions from './context'
1010

1111
export default {
12-
newOrContinue: send((context): 'NEW' | 'CONTINUE' => {
12+
newOrContinue: send((context: CR.MachineContext): 'NEW' | 'CONTINUE' => {
1313
console.log('new or continue')
1414

1515
// TODO: verify that the user has an existing tutorial to continue
1616
const hasExistingTutorial: boolean = false
1717
return hasExistingTutorial ? 'CONTINUE' : 'NEW'
1818
}),
19+
stepNext: send((context: CR.MachineContext): CR.Action => {
20+
const {tutorial, position, progress} = context
21+
// TODO: protect against errors
22+
const steps: G.Step[] = tutorial.version
23+
.levels.find((l: G.Level) => l.id === position.levelId)
24+
.stages.find((s: G.Stage) => s.id === position.stageId)
25+
.steps
26+
27+
// TODO: verify not -1
28+
const stepIndex = steps.findIndex((s: G.Step) => s.id === position.stepId)
29+
const finalStep = stepIndex === steps.length - 1
30+
const stepComplete = progress.steps[position.stepId]
31+
// not final step, or final step but not complete
32+
const hasNextStep = !finalStep || !stepComplete
33+
34+
if (hasNextStep) {
35+
const nextStep = steps[stepIndex + 1]
36+
return {
37+
type: 'LOAD_NEXT_STEP',
38+
payload: {
39+
step: nextStep
40+
}
41+
}
42+
} else {
43+
return {
44+
type: 'STAGE_COMPLETE'
45+
}
46+
}
47+
48+
}),
1949
...editorActions,
2050
...contextActions,
2151
}

web-app/src/services/state/guards/index.ts

-40
This file was deleted.

web-app/src/services/state/machine.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,17 @@ export const machine = Machine<CR.MachineContext, CR.MachineStateSchema, CR.Mach
138138
},
139139
},
140140
StepNext: {
141-
after: {
142-
0: [
143-
{
144-
target: 'Normal',
145-
cond: 'hasNextStep',
146-
actions: ['loadStep'],
147-
},
148-
{
149-
target: 'StageComplete',
150-
actions: ['updateStageProgress']
151-
},
152-
],
153-
},
141+
onEntry: ['stepNext'],
142+
on: {
143+
LOAD_NEXT_STEP: {
144+
target: 'Normal',
145+
actions: ['loadStep']
146+
},
147+
STAGE_COMPLETE: {
148+
target: "StageComplete",
149+
actions: ['updateStageProgress']
150+
}
151+
}
154152
},
155153
StageComplete: {
156154
on: {

0 commit comments

Comments
 (0)