@@ -13,43 +13,41 @@ const CsvExport = () => {
13
13
const { pickedCourses } = useContext ( CourseContext ) ;
14
14
const { multipleOptions } = useContext ( MultipleOptionsContext ) ;
15
15
16
- const GET_NAMES = true ;
17
- const GET_IDS = false ;
16
+ enum GetOptionsBy { NAME , ID }
18
17
19
- const getOptions = ( getByName : boolean ) : string [ ] => {
20
- return pickedCourses . map ( course => {
21
-
22
- const line = getByName
23
- ? [ course . course_unit_year , csvEncode ( course . name ) , course . acronym ]
24
- : [ course . id ] ;
18
+ const getOptions = ( getByName : GetOptionsBy ) : string [ ] =>
19
+ pickedCourses . map ( course => {
20
+ const baseInfo = getByName === GetOptionsBy . NAME ?
21
+ [ course . course_unit_year , csvEncode ( course . name ) , course . acronym ] :
22
+ [ course . id ] ;
25
23
26
- multipleOptions . forEach ( option => {
24
+ const classValues = multipleOptions . map ( option => {
27
25
const courseOption = option . course_options . find ( co => co . course_id === course . id ) ;
28
- const pickedClass = courseOption
29
- ? course . classes . find ( c => c . id === courseOption . picked_class_id )
30
- : undefined ;
26
+ const pickedClass = courseOption ?
27
+ course . classes . find ( c => c . id === courseOption . picked_class_id ) :
28
+ undefined ;
31
29
32
- const value = getByName ? pickedClass ?. name : pickedClass ?. id ?. toString ( ) ;
33
- line . push ( csvEncode ( value || '' ) ) ;
30
+ return csvEncode ( getByName === GetOptionsBy . NAME ? pickedClass ?. name : pickedClass ?. id ?. toString ( ) || '' ) ;
34
31
} ) ;
35
32
36
- return line . join ( ',' ) ;
37
- } ) ;
38
- } ;
33
+ return [ ...baseInfo , ...classValues ] . join ( ',' ) ;
34
+ }
35
+ ) ;
36
+
39
37
40
38
const exportCSV = ( ) => {
41
39
const header = [ 'Ano' , 'Nome' , 'Sigla' ]
42
40
multipleOptions . forEach ( ( option ) => header . push ( option . name ) )
43
41
header . push ( pickedCourses . length . toString ( ) )
44
42
45
- const lines = getOptions ( GET_NAMES ) ;
43
+ const lines = getOptions ( GetOptionsBy . NAME ) ;
46
44
47
45
lines . push ( "////----////----////----////----////----////----////" )
48
46
49
47
const header_ids = [ 'UC_ID' ]
50
48
multipleOptions . forEach ( ( option ) => header_ids . push ( option . name + "_ID" ) )
51
49
52
- const lines_id = getOptions ( GET_IDS ) ;
50
+ const lines_id = getOptions ( GetOptionsBy . ID ) ;
53
51
54
52
const csv = [ header . join ( ',' ) , lines . flat ( ) . join ( '\n' ) , header_ids . join ( ',' ) , lines_id . flat ( ) . join ( '\n' ) ] . join ( '\n' )
55
53
const blob = new Blob ( [ csv ] , { type : 'text/csv' } )
0 commit comments