1
- import { Plan , Workout , Units } from "./workout" ;
2
- import { parse as parseCsv } from "papaparse" ;
1
+ import { Plan } from "./workout" ;
3
2
import { getFileExtension } from "./utils" ;
3
+ import { csvToPlan } from "./csvProcessor" ;
4
4
5
5
export interface UploadResult {
6
6
plan ?: Plan ;
7
7
error ?: string ;
8
8
}
9
9
10
- type FileProcessor = ( file : string ) => Promise < Plan > ;
10
+ type FileProcessor = ( file : string ) => Plan | null ;
11
11
12
12
export const importFile = ( file : File ) : Promise < Plan > => {
13
13
const extension = getFileExtension ( file . name ) ;
14
14
switch ( extension ) {
15
15
case ".json" :
16
16
return importCore ( file , processJsonFile ) ;
17
17
case ".csv" :
18
- return importCore ( file , processCsvFile ) ;
18
+ return importCore ( file , csvToPlan ) ;
19
19
default :
20
20
return Promise . reject (
21
21
`Could not import file. Filetype is not supported: ${ extension } `
@@ -31,9 +31,11 @@ const importCore = (file: File, processFile: FileProcessor): Promise<Plan> => {
31
31
const result =
32
32
typedEvent && typedEvent . target && typedEvent . target . result ;
33
33
34
- try {
35
- resolve ( processFile ( result ) ) ;
36
- } catch ( e ) {
34
+ const plan = processFile ( result ) ;
35
+
36
+ if ( plan ) {
37
+ resolve ( plan ) ;
38
+ } else {
37
39
reject ( `Unable to read ${ file . name } . Check the file and try again.` ) ;
38
40
}
39
41
} ;
@@ -42,72 +44,13 @@ const importCore = (file: File, processFile: FileProcessor): Promise<Plan> => {
42
44
} ) ;
43
45
} ;
44
46
45
- const processJsonFile = ( file : string ) : Promise < Plan > => {
47
+ const processJsonFile = ( file : string ) : Plan | null => {
46
48
try {
47
49
const planObject = JSON . parse ( file ) as Plan ;
48
50
// TODO: validate object
49
- return Promise . resolve ( planObject ) ;
50
- } catch ( e ) {
51
- return Promise . reject (
52
- `Unable to read the json file. Check the file and try again.`
53
- ) ;
54
- }
55
- } ;
56
-
57
- type PlanHeadings = keyof Plan ;
58
- type WorkoutHeadings = keyof Workout ;
59
- type WorkoutValue = [ string , string ] ;
60
-
61
- const processCsvFile = ( file : string ) : Promise < Plan > => {
62
- try {
63
- const result = parseCsv ( file ) ;
64
- const [
65
- planHeadings ,
66
- planValues ,
67
- workoutHeadings ,
68
- ...workouts
69
- ] = result . data as [
70
- PlanHeadings [ ] ,
71
- string [ ] ,
72
- WorkoutHeadings [ ] ,
73
- ...string [ ] [ ]
74
- ] ;
75
-
76
- const titleIndex = planHeadings . indexOf ( "title" ) ;
77
- const raceTypeIndex = planHeadings . indexOf ( "raceType" ) ;
78
- const raceDistanceIndex = planHeadings . indexOf ( "raceDistance" ) ;
79
- const unitsIndex = planHeadings . indexOf ( "units" ) ;
80
-
81
- const descriptionIndex = workoutHeadings . indexOf ( "description" ) ;
82
- const workoutDistanceIndex = workoutHeadings . indexOf ( "totalDistance" ) ;
83
-
84
- // TODO: Validate values
85
- const plan : Plan = {
86
- // Add some resiliency for the header in case these are in a different order
87
- title : planValues [ titleIndex ] ,
88
- raceType : planValues [ raceTypeIndex ] ,
89
- raceDistance : parseFloat ( planValues [ raceDistanceIndex ] ) ,
90
- units : planValues [ unitsIndex ] as Units ,
91
- workouts : workouts . reduce (
92
- ( output , workout ) => {
93
- if ( workout . length === 2 ) {
94
- output . push ( {
95
- description : workout [ descriptionIndex ] ,
96
- totalDistance : parseFloat ( workout [ workoutDistanceIndex ] )
97
- } ) ;
98
- }
99
-
100
- return output ;
101
- } ,
102
- [ ] as Workout [ ]
103
- )
104
- } ;
105
-
106
- return Promise . resolve ( plan ) ;
51
+ return planObject ;
107
52
} catch ( e ) {
108
- return Promise . reject (
109
- `Unable to read the csv file. Check the file and try again.`
110
- ) ;
53
+ return null ;
111
54
}
112
55
} ;
113
56
0 commit comments