|
1 | 1 | import * as T from 'typings' |
2 | 2 | import * as TT from 'typings/tutorial' |
3 | 3 |
|
4 | | -interface Props { |
| 4 | +interface Input { |
5 | 5 | progress: T.Progress |
6 | 6 | position: T.Position |
7 | 7 | levels: TT.Level[] |
8 | 8 | testStatus: T.TestStatus | null |
9 | 9 | } |
10 | 10 |
|
| 11 | +type Output = { |
| 12 | + level: T.LevelUI |
| 13 | + stepIndex: number |
| 14 | +} |
| 15 | + |
11 | 16 | /* |
12 | 17 | * Format levels to include: |
13 | 18 | * - level.status = 'ACTIVE' | 'COMPLETE' | 'INCOMPLETE' |
14 | 19 | * - step.status = 'ACTIVE' | 'COMPLETE' | 'INCOMPLETE' | 'FAIL' |
15 | | - * - step.subtasks as { name: string, pass: boolean }[] |
| 20 | + * - step.subtasks as { name: string, status: 'ACTIVE' | 'COMPLETE' | 'INCOMPLETE' }[] |
16 | 21 | */ |
17 | | -const formatLevels = ({ |
18 | | - progress, |
19 | | - position, |
20 | | - levels, |
21 | | - testStatus, |
22 | | -}: Props): { levels: TT.Level[]; level: TT.Level; stepIndex: number } => { |
| 22 | +const formatLevels = ({ progress, position, levels, testStatus }: Input): Output => { |
23 | 23 | // clone levels |
24 | | - const formattedLevels = [...levels] |
25 | 24 |
|
26 | | - const level = formattedLevels.find((l: TT.Level) => l.id === position.levelId) |
| 25 | + const level: TT.Level | undefined = levels.find((l: TT.Level) => l.id === position.levelId) |
27 | 26 |
|
28 | 27 | if (!level) { |
29 | | - throw new Error(`Level "${position.levelId}" not found`) |
| 28 | + throw new Error(`Level ${position.levelId} not found`) |
30 | 29 | } |
31 | 30 |
|
32 | | - // add level status |
33 | | - level.status = progress.levels[position.levelId] ? 'COMPLETE' : 'ACTIVE' |
34 | | - |
35 | | - // add step status |
36 | | - level.steps = level.steps.map((step: TT.Step) => { |
37 | | - // label step status for step component |
38 | | - let status: T.ProgressStatus = 'INCOMPLETE' |
39 | | - if (progress.steps[step.id]) { |
40 | | - status = 'COMPLETE' |
41 | | - } else if (step.id === position.stepId) { |
42 | | - status = 'ACTIVE' |
43 | | - if (step.subtasks && step.subtasks) { |
44 | | - step.subtasks.map((subtask: string, subtaskIndex: number) => ({ |
45 | | - name: subtask, |
46 | | - pass: !!(testStatus?.summary ? testStatus.summary[subtaskIndex] : false), |
47 | | - })) |
| 31 | + const levelUI: T.LevelUI = { |
| 32 | + ...level, |
| 33 | + status: progress.levels[position.levelId] ? 'COMPLETE' : 'ACTIVE', |
| 34 | + steps: level.steps.map((step: TT.Step) => { |
| 35 | + // label step status for step component |
| 36 | + let status: T.ProgressStatus = 'INCOMPLETE' |
| 37 | + let subtasks |
| 38 | + if (progress.steps[step.id]) { |
| 39 | + status = 'COMPLETE' |
| 40 | + } else if (step.id === position.stepId) { |
| 41 | + status = 'ACTIVE' |
| 42 | + if (step.subtasks && step.subtasks) { |
| 43 | + subtasks = step.subtasks.map((subtask: string, subtaskIndex: number) => { |
| 44 | + let subtaskStatus: T.ProgressStatus = 'INCOMPLETE' |
| 45 | + // task is complete, subtasks must be complete |
| 46 | + if (status === 'COMPLETE') { |
| 47 | + subtaskStatus = 'COMPLETE' |
| 48 | + // task is active, check which are complete from test results |
| 49 | + } else if (status === 'ACTIVE') { |
| 50 | + subtaskStatus = !!(testStatus?.summary && testStatus.summary[subtaskIndex]) ? 'COMPLETE' : 'ACTIVE' |
| 51 | + } |
| 52 | + return { |
| 53 | + name: subtask, |
| 54 | + status: subtaskStatus, |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
48 | 58 | } |
49 | | - } |
50 | | - return { ...step, status } |
51 | | - }) |
52 | | - |
53 | | - let stepIndex = level.steps.findIndex((s: TT.Step) => s.status === 'ACTIVE') |
| 59 | + return { ...step, status, subtasks } |
| 60 | + }), |
| 61 | + } |
| 62 | + let stepIndex = levelUI.steps.findIndex((s: T.StepUI) => s.status === 'ACTIVE') |
54 | 63 | if (stepIndex === -1) { |
55 | 64 | stepIndex = level.steps.length |
56 | 65 | } |
57 | | - return { levels: formattedLevels, level, stepIndex } |
| 66 | + return { level: levelUI, stepIndex } |
58 | 67 | } |
59 | 68 |
|
60 | 69 | export default formatLevels |
0 commit comments