Skip to content

Commit ccd088b

Browse files
committed
fix loading tutorial position
1 parent 13a4499 commit ccd088b

File tree

5 files changed

+13
-26
lines changed

5 files changed

+13
-26
lines changed

src/channel/context.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ class Context {
1717
this.position = new Position()
1818
this.progress = new Progress()
1919
}
20-
setTutorial(workspaceState: vscode.Memento, tutorial: G.Tutorial) {
20+
public setTutorial = async (workspaceState: vscode.Memento, tutorial: G.Tutorial): Promise<{progress: CR.Progress, position: CR.Position}> => {
2121
this.tutorial.set(tutorial)
22-
this.progress.setTutorial(workspaceState, tutorial)
22+
const progress: CR.Progress = await this.progress.setTutorial(workspaceState, tutorial)
23+
const position: CR.Position = this.position.setPositionFromProgress(tutorial, progress)
24+
return {progress, position}
2325
}
2426
}
2527

src/channel/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,14 @@ class Channel implements Channel {
4949
}
5050

5151
// set tutorial
52-
const progress: CR.Progress = await this.context.progress.setTutorial(this.workspaceState, tutorial)
52+
const {position, progress} = await this.context.setTutorial(this.workspaceState, tutorial)
5353

5454
if (progress.complete) {
5555
// tutorial is already complete
5656
this.send({type: 'NEW_TUTORIAL'})
5757
return
5858
}
5959

60-
const position = this.context.position.get()
61-
6260
// communicate to client the tutorial & stepProgress state
6361
this.send({type: 'CONTINUE_TUTORIAL', payload: {tutorial, progress, position}})
6462

src/channel/state/Position.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ class Position {
3030

3131
const lastLevelIndex: number | undefined = levels.findIndex((l: G.Level) => progress.levels[l.id])
3232
// TODO: consider all levels complete as progress.complete
33-
if (lastLevelIndex < 0 && lastLevelIndex < levels.length) {
33+
if (lastLevelIndex >= levels.length) {
3434
throw new Error('Error setting progress level')
3535
}
3636
const currentLevel: G.Level = levels[lastLevelIndex + 1]
3737

3838
const {stages} = currentLevel
3939

4040
const lastStageIndex: number | undefined = stages.findIndex((s: G.Stage) => progress.stages[s.id])
41-
if (lastStageIndex < 0 && lastStageIndex < stages.length) {
41+
if (lastStageIndex >= stages.length) {
4242
throw new Error('Error setting progress stage')
4343
}
4444
const currentStage: G.Stage = stages[lastStageIndex + 1]
4545

4646
const {steps} = currentStage
4747

4848
const lastStepIndex: number | undefined = steps.findIndex((s: G.Step) => progress.steps[s.id])
49-
if (lastStepIndex < 0 && lastStepIndex < steps.length) {
49+
if (lastStepIndex >= steps.length) {
5050
throw new Error('Error setting progress step')
5151
}
5252
const currentStep: G.Step = steps[lastStepIndex + 1]

src/channel/state/Progress.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as vscode from 'vscode'
44

55
import Storage from '../../services/storage'
66

7-
const defaultValue = {
7+
const defaultValue: CR.Progress = {
88
levels: {},
99
stages: {},
1010
steps: {},
@@ -24,7 +24,7 @@ class Progress {
2424
storage: workspaceState,
2525
defaultValue,
2626
})// set value from storage
27-
this.value = await this.storage.get()
27+
this.value = await this.storage.get() || defaultValue
2828
return this.value
2929
}
3030
public get = () => {

web-app/src/containers/Tutorial/StagePage/index.tsx

+3-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react'
22
import * as CR from 'typings'
33
import * as G from 'typings/graphql'
4+
import * as selectors from '../../../services/selectors'
45

56
import Stage from './Stage'
67

@@ -10,23 +11,9 @@ interface PageProps {
1011
}
1112

1213
const StageSummaryPageContainer = (props: PageProps) => {
13-
const { tutorial, position, progress } = props.context
14+
const { position, progress } = props.context
1415

15-
if (!tutorial) {
16-
throw new Error('Tutorial not found in StageSummaryPageContainer')
17-
}
18-
19-
const level: G.Level | undefined = tutorial.version.levels.find((l: G.Level) => l.id === position.levelId)
20-
21-
if (!level) {
22-
throw new Error('Level not found in StageSummaryPageContainer')
23-
}
24-
25-
const stage: G.Stage | undefined = level.stages.find((s: G.Stage) => s.id === position.stageId)
26-
27-
if (!stage) {
28-
throw new Error('Stage not found in StageSummaryPageContainer')
29-
}
16+
const stage: G.Stage = selectors.currentStage(props.context)
3017

3118
const onContinue = (): void => {
3219
props.send({

0 commit comments

Comments
 (0)