Skip to content

Fix/update to api #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove stages from files
  • Loading branch information
ShMcK committed Oct 14, 2019
commit 38944277067c726dde5f24a2e942723d83445f17
14 changes: 2 additions & 12 deletions src/channel/state/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as G from 'typings/graphql'

const defaultValue: CR.Position = {
levelId: '',
stageId: '',
stepId: '',
}

Expand Down Expand Up @@ -31,7 +30,7 @@ class Position {
return this.value
}

const {levels} = tutorial.version
const {levels} = tutorial.version.data

const lastLevelIndex: number | undefined = levels.findIndex((l: G.Level) => !progress.levels[l.id])
// TODO: consider all levels complete as progress.complete
Expand All @@ -40,15 +39,7 @@ class Position {
}
const currentLevel: G.Level = levels[lastLevelIndex]

const {stages} = currentLevel

const lastStageIndex: number | undefined = stages.findIndex((s: G.Stage) => !progress.stages[s.id])
if (lastStageIndex >= stages.length) {
throw new Error('Error setting progress stage')
}
const currentStage: G.Stage = stages[lastStageIndex]

const {steps} = currentStage
const {steps} = currentLevel

const lastStepIndex: number | undefined = steps.findIndex((s: G.Step) => !progress.steps[s.id])
if (lastStepIndex >= steps.length) {
Expand All @@ -60,7 +51,6 @@ class Position {

this.value = {
levelId: currentLevel.id,
stageId: currentStage.id,
stepId: currentStep.id,
}
return this.value
Expand Down
21 changes: 9 additions & 12 deletions typings/context.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import * as CR from './index'

export interface Step extends Exclude<CR.TutorialStep, 'actions'> {
status: {
complete: boolean
active: boolean
}
interface ProgressStatus {
active: boolean
complete: boolean
}

export interface ReceivedEvent {
data: CR.Action
export interface Step extends Exclude<CR.TutorialStep, 'actions'> {
status: ProgressStatus
}

export interface StageStepStatus {
active: boolean
complete: boolean
export interface ReceivedEvent {
data: CR.Action
}

export interface StageWithStatus extends CR.TutorialStage {
status: StageStepStatus
export interface LevelWithStatus extends CR.TutorialLevel {
status: ProgressStatus
}
23 changes: 2 additions & 21 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ import Storage from '../src/services/storage'
import * as G from './graphql'

export interface TutorialLevel {
stageList: string[]
content: {
title: string
text: string
}
actions?: {
setup: TutorialAction
}
}

export interface TutorialStage {
stepList: string[]
content: {
title: string
Expand Down Expand Up @@ -54,9 +43,6 @@ export interface TutorialData {
levels: {
[levelId: string]: TutorialLevel
}
stages: {
[stageId: string]: TutorialStage
}
steps: {
[stepId: string]: TutorialStep
}
Expand Down Expand Up @@ -90,9 +76,6 @@ export interface Progress {
levels: {
[levelId: string]: boolean
}
stages: {
[stageId: string]: boolean
}
steps: {
[stepId: string]: boolean
}
Expand All @@ -106,7 +89,6 @@ export interface StepProgress {
// current tutorial position
export interface Position {
levelId: string
stageId: string
stepId: string
complete?: boolean
}
Expand Down Expand Up @@ -154,16 +136,15 @@ export interface MachineStateSchema {
Initialize: {}
Summary: {}
LoadNext: {}
Level: {}
Stage: {
Level: {
states: {
Load: {}
Normal: {}
TestRunning: {}
TestPass: {}
TestFail: {}
StepNext: {}
StageComplete: {}
LevelComplete: {}
}
}
Completed: {}
Expand Down
2 changes: 1 addition & 1 deletion web-app/src/containers/Tutorial/LevelPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const LevelSummaryPageContainer = (props: ContainerProps) => {
throw new Error('Tutorial not found in LevelSummaryPageContainer')
}

const level: G.Level | undefined = tutorial.version.levels.find((l: G.Level) => l.id === position.levelId)
const level: G.Level | undefined = tutorial.version.data.levels.find((l: G.Level) => l.id === position.levelId)

if (!level) {
throw new Error('Level not found in LevelSummaryPageContainer')
Expand Down
6 changes: 2 additions & 4 deletions web-app/src/services/selectors/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ import * as tutorial from './tutorial'

export const defaultPosition = () => ({
levelId: '',
stageId: '',
stepId: ''
})

export const initialPosition = createSelector(
tutorial.currentVersion,
(version: G.TutorialVersion) => {
const position: CR.Position = {
levelId: version.levels[0].id,
stageId: version.levels[0].stages[0].id,
stepId: version.levels[0].stages[0].steps[0].id,
levelId: version.data.levels[0].id,
stepId: version.data.levels[0].steps[0].id,
}
return position
}
Expand Down
1 change: 0 additions & 1 deletion web-app/src/services/selectors/progress.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export const defaultProgress = () => ({
levels: {},
stages: {},
steps: {},
complete: false
})
20 changes: 4 additions & 16 deletions web-app/src/services/selectors/tutorial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const currentLevel = (context: MachineContext): G.Level => createSelector
(version: G.TutorialVersion): G.Level => {
// merge in the updated position
// sent with the test to ensure consistency
const levels: G.Level[] = version.levels
const levels: G.Level[] = version.data.levels

const level: G.Level | undefined = levels.find((l: G.Level) => l.id === context.position.levelId)

Expand All @@ -33,22 +33,10 @@ export const currentLevel = (context: MachineContext): G.Level => createSelector
return level
})(context)

export const currentStage = (context: MachineContext): G.Stage => createSelector(
currentLevel,
(level: G.Level): G.Stage => {
const stages: G.Stage[] = level.stages
const stage: G.Stage | undefined = stages.find((s: G.Stage) => s.id === context.position.stageId)
if (!stage) {
throw new Error('No Stage found')
}
return stage
}
)(context)

export const currentStep = (context: MachineContext): G.Step => createSelector(
currentStage,
(stage: G.Stage): G.Step => {
const steps: G.Step[] = stage.steps
currentLevel,
(level: G.Level): G.Step => {
const steps: G.Step[] = level.steps
const step: G.Step | undefined = steps.find((s: G.Step) => s.id === context.position.stepId)
if (!step) {
throw new Error('No Step found')
Expand Down
71 changes: 8 additions & 63 deletions web-app/src/services/state/actions/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
return event.payload.tutorial
},
progress: (): CR.Progress => {
return {levels: {}, stages: {}, steps: {}, complete: false}
return {levels: {}, steps: {}, complete: false}
}
}),
initTutorial: assign({
Expand Down Expand Up @@ -74,41 +74,20 @@ export default {
},
}),
// @ts-ignore
updateStagePosition: assign({
position: (context: CR.MachineContext): CR.Position => {
const {position} = context

const level: G.Level = selectors.currentLevel(context)
const stages: G.Stage[] = level.stages

const stageIndex = stages.findIndex((s: G.Stage) => s.id === position.stageId)
const stage: G.Stage = stages[stageIndex + 1]

const nextPosition: CR.Position = {
...position,
stageId: stage.id,
stepId: stage.steps[0].id,
}

return nextPosition
},
}),
// @ts-ignore
updateLevelPosition: assign({
position: (context: CR.MachineContext): CR.Position => {
const {position} = context
const version = selectors.currentVersion(context)
// merge in the updated position
// sent with the test to ensure consistency
const levels: G.Level[] = version.levels
const levels: G.Level[] = version.data.levels

const levelIndex = levels.findIndex((l: G.Level) => l.id === position.levelId)
const level: G.Level = levels[levelIndex + 1]

const nextPosition: CR.Position = {
levelId: level.id,
stageId: level.stages[0].id,
stepId: level.stages[0].steps[0].id,
stepId: level.steps[0].id,
}

return nextPosition
Expand All @@ -128,19 +107,6 @@ export default {
},
}),
// @ts-ignore
updateStageProgress: assign({
progress: (context: CR.MachineContext, event: CR.MachineEvent): CR.Progress => {
// update progress by tracking completed
const {progress, position} = context

const stageId: string = position.stageId

progress.stages[stageId] = true

return progress
},
}),
// @ts-ignore
updatePosition: assign({
position: (context: CR.MachineContext, event: CR.MachineEvent): CR.Progress => {
const {position} = event.payload
Expand All @@ -152,9 +118,8 @@ export default {

const version = selectors.currentVersion(context)
const level = selectors.currentLevel(context)
const stage = selectors.currentStage(context)

const steps: G.Step[] = stage.steps
const steps: G.Step[] = level.steps

const stepIndex = steps.findIndex((s: G.Step) => s.id === position.stepId)
const stepComplete = progress.steps[position.stepId]
Expand All @@ -168,39 +133,19 @@ export default {
return {type: 'NEXT_STEP', payload: {position: nextPosition}}
}

// has next stage?

const {stages} = level
const stageIndex = stages.findIndex((s: G.Stage) => s.id === position.stageId)
const finalStage = (stageIndex > -1 && stageIndex === stages.length - 1)
const hasNextStage = (!finalStage)

// NEXT STAGE
if (hasNextStage) {
const nextStage = stages[stageIndex + 1]
const nextPosition = {
levelId: position.levelId,
stageId: nextStage.id,
stepId: nextStage.steps[0].id,
}
return {type: 'NEXT_STAGE', payload: {position: nextPosition}}
}

// has next level?

const {levels} = version
const levels = context?.tutorial?.version.data.levels ?? []
const levelIndex = levels.findIndex((l: G.Level) => l.id === position.levelId)
const finalLevel = (levelIndex > -1 && levelIndex === levels.length - 1)
const hasNextLevel = (!finalLevel)

// NEXT LEVEL
if (hasNextLevel) {
const nextLevel = levels[levelIndex + 1]
const nextStage = nextLevel.stages[0]
const nextPosition = {
levelId: nextLevel.id,
stageId: nextStage.id,
stepId: nextStage.steps[0].id,
stepId: nextLevel.steps[0].id,
}
return {type: 'NEXT_LEVEL', payload: {position: nextPosition}}
}
Expand All @@ -211,9 +156,9 @@ export default {
stepNext: send((context: CR.MachineContext): CR.Action => {
const {position, progress} = context

const stage: G.Stage = selectors.currentStage(context)
const level: G.Level = selectors.currentLevel(context)

const {steps} = stage
const {steps} = level
// TODO: verify not -1
const stepIndex = steps.findIndex((s: G.Step) => s.id === position.stepId)
const finalStep = stepIndex === steps.length - 1
Expand Down
3 changes: 1 addition & 2 deletions web-app/src/services/state/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ export const machine = Machine<CR.MachineContext, CR.MachineStateSchema, CR.Mach
context: {
env: {machineId: '', sessionId: '', token: ''},
tutorial: null,
position: {levelId: '', stageId: '', stepId: ''},
position: {levelId: '', stepId: ''},
progress: {
levels: {},
stages: {},
steps: {},
complete: false
}
Expand Down