1+ import * as React from 'react'
2+ import { connect , MapDispatchToProps } from "react-redux"
3+ import { bindActionCreators , Dispatch } from 'redux'
4+ import { parseString } from 'xml2js'
5+ import { updateAssessment , updateAssessmentOverview } from '../../actions/session'
6+ import {
7+ AssessmentCategories ,
8+ AssessmentStatuses ,
9+ GradingStatuses ,
10+ // IAssessment,
11+ IAssessmentOverview
12+ } from '../../components/assessment/assessmentShape'
13+ // import { IDispatchProps } from '../../components/assessment'
14+
15+
16+ const mapDispatchToProps : MapDispatchToProps < any , IAssessmentOverview > = ( dispatch : Dispatch < any > ) =>
17+ bindActionCreators (
18+ {
19+ newAssessment : updateAssessment ,
20+ newAssessmentOverview : updateAssessmentOverview
21+ } ,
22+ dispatch
23+ )
24+
25+ const capitalizeFirstLetter = ( str : string ) => {
26+ return str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) ;
27+ }
28+
29+ const makeAssessmentOverview = ( task : any ) => {
30+ const rawOverview = task . $ ;
31+ return {
32+ category : capitalizeFirstLetter ( rawOverview . kind ) as AssessmentCategories ,
33+ closeAt : rawOverview . duedate ,
34+ coverImage : rawOverview . coverimage ,
35+ grade : 1 ,
36+ id : 7 ,
37+ maxGrade : 3000 ,
38+ maxXp : 1000 ,
39+ openAt : rawOverview . startdate ,
40+ title : rawOverview . title ,
41+ shortSummary : task . WEBSUMMARY [ 0 ] ,
42+ status : AssessmentStatuses . not_attempted ,
43+ story : rawOverview . story ,
44+ xp : 0 ,
45+ gradingStatus : "none" as GradingStatuses
46+ }
47+ }
48+
49+ class ImportFromFileComponent extends React . Component < any , any > {
50+ public fileReader : FileReader ;
51+ public constructor ( props : any ) {
52+ super ( props ) ;
53+ this . handleFileRead = this . handleFileRead . bind ( this ) ;
54+ this . handleChangeFile = this . handleChangeFile . bind ( this ) ;
55+ }
56+
57+ public render ( ) {
58+ return (
59+ < div >
60+ < input
61+ type = "file"
62+ id = "file"
63+ accept = ".xml"
64+ onChange = { this . handleChangeFile }
65+ />
66+ </ div >
67+ )
68+ }
69+
70+ private handleFileRead = ( e : any ) => {
71+ const content = this . fileReader . result ;
72+ if ( content ) {
73+ parseString ( content , ( err : any , result : any ) => {
74+ // tslint:disable-next-line:no-console
75+ console . dir ( result ) ;
76+ const task = result . CONTENT . TASK [ 0 ] ;
77+ const overview : IAssessmentOverview = makeAssessmentOverview ( task ) ;
78+ this . props . newAssessmentOverview ( overview ) ;
79+ } ) ;
80+ }
81+ // You can set content in state and show it in render.
82+ }
83+
84+ private handleChangeFile = ( e : any ) => {
85+ const files = e . target . files
86+ if ( e . target . files ) {
87+ this . fileReader = new FileReader ( ) ;
88+ this . fileReader . onloadend = this . handleFileRead ;
89+ this . fileReader . readAsText ( files [ 0 ] ) ;
90+ }
91+ }
92+ }
93+
94+ export default connect ( null , mapDispatchToProps ) ( ImportFromFileComponent )
0 commit comments