From b9e7d9557af45d529de46efcc303bc95b11994a2 Mon Sep 17 00:00:00 2001
From: shmck <shawn.j.mckay@gmail.com>
Date: Wed, 12 Jun 2019 21:05:49 -0700
Subject: [PATCH 1/3] fix issue with new tutorial on startup

---
 src/editor/commands/index.ts | 1 +
 src/state/actions/index.ts   | 8 ++++++++
 2 files changed, 9 insertions(+)

diff --git a/src/editor/commands/index.ts b/src/editor/commands/index.ts
index 6cd66a59..aeaeba7a 100644
--- a/src/editor/commands/index.ts
+++ b/src/editor/commands/index.ts
@@ -63,6 +63,7 @@ export const createCommands = ({ context, machine, storage, git, position }: Cre
         const { steps } = tutorial.data
         const { setup } = steps[pos.stepId].actions
         await git.gitLoadCommits(setup)
+        machine.send('TUTORIAL_LOADED')
     },
     [COMMANDS.TUTORIAL_SETUP]: async (tutorial: CR.Tutorial) => {
         console.log('tutorial setup', tutorial)
diff --git a/src/state/actions/index.ts b/src/state/actions/index.ts
index 9bdc9dce..2042c018 100644
--- a/src/state/actions/index.ts
+++ b/src/state/actions/index.ts
@@ -32,6 +32,7 @@ export default {
         const canContinue = !!(tutorial && progress && hasGit && hasGitRemote)
 
         if (canContinue) {
+            // continue
             currentTutorial = tutorial
             currentProgress = progress
         }
@@ -41,6 +42,7 @@ export default {
     async tutorialLaunch() {
         // TODO: add selection of tutorial id
         const tutorial: CR.Tutorial = await api({ resource: 'getTutorial', params: { id: '1' } })
+        currentTutorial = tutorial
         console.log('api')
         console.log(tutorial)
         vscode.commands.executeCommand('coderoad.tutorial_launch', tutorial)
@@ -151,5 +153,11 @@ export default {
     }),
     stepLoadNext() {
         console.log("LOAD NEXT STEP")
+    },
+    loadLevel() {
+        console.log('loadLevel')
+    },
+    loadStage() {
+        console.log('loadStage')
     }
 }
\ No newline at end of file

From 893eca7c7def9dd1923c9f4d2ed2a886fd79abba Mon Sep 17 00:00:00 2001
From: shmck <shawn.j.mckay@gmail.com>
Date: Wed, 12 Jun 2019 21:14:47 -0700
Subject: [PATCH 2/3] initialize position

---
 src/state/actions/index.ts                      | 13 +++++++++++++
 src/state/machine.ts                            |  8 +++++++-
 typings/index.d.ts                              |  1 +
 web-app/src/containers/Tutorial/SummaryPage.tsx |  2 +-
 4 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/state/actions/index.ts b/src/state/actions/index.ts
index 2042c018..8e71d7c7 100644
--- a/src/state/actions/index.ts
+++ b/src/state/actions/index.ts
@@ -51,6 +51,19 @@ export default {
         vscode.commands.executeCommand('coderoad.tutorial_setup', currentTutorial)
         vscode.commands.executeCommand('coderoad.open_webview', vscode.ViewColumn.Two)
     },
+    initializeNewTutorial: assign({
+        position: (context: any): CR.Position => {
+            const { data } = context
+            const levelId = data.summary.levelList[0]
+            const stageId = data.levels[levelId].stageList[0]
+            const stepId = data.stages[stageId].stepList[0]
+            return {
+                levelId,
+                stageId,
+                stepId
+            }
+        }
+    }),
     tutorialContinue: assign({
         // load initial data, progress & position
         data(): CR.TutorialData {
diff --git a/src/state/machine.ts b/src/state/machine.ts
index 46921862..5071eff2 100644
--- a/src/state/machine.ts
+++ b/src/state/machine.ts
@@ -57,9 +57,15 @@ export const machine = Machine<
             },
             Tutorial: {
                 id: 'tutorial',
-                initial: 'Summary',
+                initial: 'Initialize',
                 onEntry: ['tutorialSetup'],
                 states: {
+                    Initialize: {
+                        onEntry: ['initializeNewTutorial'],
+                        after: {
+                            0: 'Summary'
+                        }
+                    },
                     LoadNext: {
                         id: 'tutorial-load-next',
                         onEntry: ['tutorialLoadNext'],
diff --git a/typings/index.d.ts b/typings/index.d.ts
index b95db2cf..65f78019 100644
--- a/typings/index.d.ts
+++ b/typings/index.d.ts
@@ -146,6 +146,7 @@ export interface MachineStateSchema {
         }
         Tutorial: {
             states: {
+                Initialize: {}
                 Summary: {}
                 LoadNext: {}
                 Level: {}
diff --git a/web-app/src/containers/Tutorial/SummaryPage.tsx b/web-app/src/containers/Tutorial/SummaryPage.tsx
index 8cbec9d8..9d3dffca 100644
--- a/web-app/src/containers/Tutorial/SummaryPage.tsx
+++ b/web-app/src/containers/Tutorial/SummaryPage.tsx
@@ -9,7 +9,7 @@ interface PageProps {
 
 const SummaryPage = (props: PageProps) => {
   const { data } = React.useContext(DataContext)
-  return <Summary data={data} onNext={() => props.send('LOAD_NEXT')} />
+  return <Summary data={data} onNext={() => props.send('NEXT')} />
 }
 
 export default SummaryPage

From 5d0abd69445b30894780ecba45f4eea9709fe596 Mon Sep 17 00:00:00 2001
From: shmck <shawn.j.mckay@gmail.com>
Date: Wed, 12 Jun 2019 21:20:58 -0700
Subject: [PATCH 3/3] load next step

---
 src/state/actions/index.ts | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/state/actions/index.ts b/src/state/actions/index.ts
index 8e71d7c7..1a3fd19b 100644
--- a/src/state/actions/index.ts
+++ b/src/state/actions/index.ts
@@ -164,9 +164,18 @@ export default {
             return nextProgress
         }
     }),
-    stepLoadNext() {
-        console.log("LOAD NEXT STEP")
-    },
+    stepLoadNext: assign({
+        position: (context: any) => {
+            const { data, position } = context
+            const { stepList } = data.stages[position.stageId]
+            const currentStepIndex = stepList.indexOf(position.stepId)
+            const nextStepId = stepList[currentStepIndex + 1]
+            return {
+                ...context.position,
+                stepId: nextStepId,
+            }
+        }
+    }),
     loadLevel() {
         console.log('loadLevel')
     },