11import * as G from 'typings/graphql'
22import * as CR from 'typings'
33import * as git from '../../services/git'
4- import { machine } from '../../extension'
5- import * as storage from '../storage'
64
75interface TutorialConfig {
86codingLanguage :G . EnumCodingLanguage
@@ -16,10 +14,7 @@ export interface TutorialModel {
1614position :CR . Position
1715progress :CR . Progress
1816launch ( tutorial :G . Tutorial ) :void
19- updateProgress ( ) :void
20- nextPosition ( ) :CR . Position
2117hasExisting ( ) :Promise < boolean >
22- syncClient ( ) :void
2318triggerCurrent ( stepActions :G . StepActions ) :void
2419}
2520
@@ -29,7 +24,6 @@ class Tutorial implements TutorialModel {
2924public version :G . TutorialVersion
3025public position :CR . Position
3126public progress :CR . Progress
32- public syncClient :( ) => void
3327public openFile :( file :string ) => void
3428
3529constructor ( editorDispatch :CR . EditorDispatch ) {
@@ -40,11 +34,6 @@ class Tutorial implements TutorialModel {
4034this . version = { } as G . TutorialVersion
4135this . position = { } as CR . Position
4236this . progress = { } as CR . Progress
43- this . syncClient = ( ) => editorDispatch ( 'coderoad.send_data' , {
44- tutorial :{ id :this . version . tutorialId } ,
45- progress :this . progress ,
46- position :this . position ,
47- } )
4837this . openFile = ( file :string ) => editorDispatch ( 'coderoad.open_file' , file )
4938}
5039public launch = async ( tutorial :G . Tutorial ) => {
@@ -71,42 +60,11 @@ class Tutorial implements TutorialModel {
7160this . version = tutorial . version
7261console . log ( 'version' , this . version )
7362
74- // set initial position
75- this . position = {
76- levelId :this . version . levels [ 0 ] . id ,
77- stageId :this . version . levels [ 0 ] . stages [ 0 ] . id ,
78- stepId :this . version . levels [ 0 ] . stages [ 0 ] . steps [ 0 ] . id ,
79- }
80- // set empty initial progress
81- this . progress = {
82- levels :{ } ,
83- stages :{ } ,
84- steps :{ } ,
85- complete :false ,
86- }
87-
88- // setup git, git remote
89-
90-
91- await this . syncClient ( )
92-
93- // set tutorial, position, progress locally
94- // TODO: base position off of progress
95- Promise . all ( [
96- storage . setTutorial ( tutorial ) ,
97- storage . setPosition ( this . position ) ,
98- storage . setProgress ( this . progress )
99- ] )
100-
101- console . log ( 'tutorial loaded' )
102- machine . send ( 'TUTORIAL_LOADED' )
10363}
10464
10565public async hasExisting ( ) :Promise < boolean > {
106- const [ tutorial , progress ] = await Promise . all ( [
107- storage . getTutorial ( ) ,
108- storage . getProgress ( ) ,
109- ] )
66+ // instead should configure git if does not exist
67+ // and update git to current position
11068
11169// verify git is setup with a coderoad remote
11270const [ hasGit , hasGitRemote ] = await Promise . all ( [
@@ -115,82 +73,13 @@ class Tutorial implements TutorialModel {
11573] )
11674// TODO: may need to clean up git remote if no existing tutorial
11775
118- const canContinue = ! ! ( tutorial && progress && hasGit && hasGitRemote )
76+ const canContinue = ! ! ( hasGit && hasGitRemote )
11977
12078return canContinue
12179}
12280public triggerCurrent = ( stepActions :G . StepActions ) => {
12381git . gitLoadCommits ( stepActions , this . openFile )
12482}
125- public updateProgress = ( ) => {
126- const { levelId, stageId, stepId} = this . position
127- this . progress . levels [ levelId ] = true
128- this . progress . stages [ stageId ] = true
129- this . progress . steps [ stepId ] = true
130-
131- this . syncClient ( )
132- }
133- public nextPosition = ( ) :CR . Position => {
134- const { levelId, stageId, stepId} = this . position
135- // TODO: calculate and return next position
136-
137- // is next step
138- const stage :G . Stage | null = this . stage ( stageId )
139- if ( ! stage ) {
140- throw new Error ( 'Stage not found' )
141- }
142- const { steps} = stage
143- const indexOfStep = steps . findIndex ( ( s :G . Step ) :boolean => s . id === stepId )
144- if ( indexOfStep === - 1 ) {
145- throw new Error ( 'Step not found' )
146- }
147- if ( indexOfStep < steps . length - 1 ) {
148-
149- return {
150- levelId,
151- stageId,
152- stepId :steps [ indexOfStep + 1 ] . id
153- }
154- }
155-
156- // is next stage
157- const level :G . Level | null = this . level ( levelId )
158- if ( ! level ) {
159- throw new Error ( 'Level not found' )
160- }
161- const { stages} = level
162- const indexOfStage = stages . findIndex ( ( s :G . Stage ) :boolean => s . id === stageId )
163- if ( indexOfStage === - 1 ) {
164- throw new Error ( 'Stage not found' )
165- }
166- if ( indexOfStage < stages . length - 1 ) {
167- // next stage
168- const nextStage = stages [ indexOfStage + 1 ]
169- return {
170- levelId,
171- stageId :nextStage . id ,
172- stepId :nextStage . steps [ 0 ] . id
173- }
174- }
175-
176- // is next level
177- const levels = this . version . levels
178- const indexOfLevel = levels . findIndex ( ( l :G . Level ) :boolean => l . id === levelId )
179- if ( indexOfLevel === - 1 ) {
180- throw new Error ( 'Level not found' )
181- }
182- if ( indexOfLevel < levels . length - 1 ) {
183- const nextLevel = levels [ indexOfLevel + 1 ]
184- const nextStage = nextLevel . stages [ 0 ]
185- return {
186- levelId :nextLevel . id ,
187- stageId :nextStage . id ,
188- stepId :nextStage . steps [ 0 ] . id ,
189- }
190- }
191-
192- throw new Error ( 'Could not calculate next position' )
193- }
19483}
19584
19685export default Tutorial