|
1 |
| -import {TUTORIAL_INIT, TUTORIAL_BUILD, TUTORIAL_LOAD} from './types'; |
| 1 | +import { |
| 2 | + TUTORIAL_INIT, TUTORIAL_BUILD, TUTORIAL_LOAD, |
| 3 | + TUTORIAL_PAGE_ADD, TUTORIAL_TASK_ADD, TUTORIAL_HINT_ADD, TUTORIAL_ACTION_ADD |
| 4 | +} from './types'; |
2 | 5 | import {create, build} from 'coderoad-cli';
|
3 | 6 | import {readFileSync} from 'fs';
|
4 | 7 | import {join} from 'path';
|
| 8 | +// import taskUpdate from './utils/taskUpdate'; |
| 9 | +import pageNew from './utils/pageNew'; |
5 | 10 |
|
6 |
| -const _tutorial = { |
7 |
| - title: '', |
8 |
| - pages: [{ |
9 |
| - file: './01/page-01.md', |
10 |
| - title: 'Page One', |
11 |
| - description: 'page one description', |
12 |
| - tasks: [{ |
13 |
| - tests: ['./01/01.js'], |
14 |
| - description: 'first task', |
15 |
| - hints: ['hint 1', 'hint 2'] |
16 |
| - }] |
17 |
| - }] |
| 11 | +const _tutorial: CR.Tutorial = { |
| 12 | + info: { |
| 13 | + title: '', |
| 14 | + description: '', |
| 15 | + }, |
| 16 | + pages: [].concat(pageNew(0)) |
18 | 17 | };
|
19 | 18 |
|
20 |
| -export default function tutorial(t = _tutorial, action: Action) { |
| 19 | +export default function tutorial(t = _tutorial, action: Action): CR.Tutorial { |
21 | 20 | switch (action.type) {
|
22 | 21 |
|
23 | 22 | case TUTORIAL_INIT:
|
24 | 23 | const {dir, name} = action.payload;
|
25 | 24 | create(dir, name);
|
26 |
| - if (_tutorial.title.length < 1) { |
27 |
| - t = Object.assign({}, t, { title: name }); |
| 25 | + if (_tutorial.info.title.length < 1) { |
| 26 | + // no title? save packageJson name as title |
| 27 | + const info = Object.assign({}, t.info, { title: name }); |
| 28 | + t = Object.assign({}, t, {info}); |
28 | 29 | }
|
29 | 30 | return t;
|
30 | 31 |
|
31 | 32 | case TUTORIAL_BUILD:
|
| 33 | + // run CLI build to create a new coderoad.json file |
32 | 34 | build(action.payload.dir, join('tutorial', 'tutorial.md'));
|
33 | 35 | return t;
|
34 | 36 |
|
35 | 37 | case TUTORIAL_LOAD:
|
| 38 | + // load data from coderoad.json |
36 | 39 | const data = JSON.parse(readFileSync(
|
37 | 40 | join(action.payload.dir, 'coderoad.json'), 'utf8')
|
38 | 41 | );
|
39 | 42 | // TODO: validate coderoad.json data
|
40 |
| - |
41 | 43 | return data;
|
42 | 44 |
|
| 45 | + case TUTORIAL_PAGE_ADD: |
| 46 | + // add a new page template |
| 47 | + const pages = t.pages.concat(pageNew(t.pages.length)); |
| 48 | + return Object.assign({}, t, {pages}); |
| 49 | + |
| 50 | + // case TUTORIAL_ADD_HINT: |
| 51 | + // const {pagePosition, taskPosition} = action.payload; |
| 52 | + // t.pages[pagePosition].tasks[taskPosition].hints.concat('') |
| 53 | + |
43 | 54 | default:
|
44 | 55 | return t;
|
45 | 56 | }
|
|
0 commit comments