@@ -7,7 +7,8 @@ import PropTypes from 'prop-types'
77import '../styles/Questions.css'
88import { DateInput , TimeInput } from 'semantic-ui-calendar-react' ;
99import { Form , TextArea , Divider , Button , Select } from 'semantic-ui-react'
10- import { stat } from 'fs'
10+ import AWS from 'aws-sdk'
11+ require ( 'dotenv' ) . config ( ) ;
1112
1213class Preview extends Component {
1314 constructor ( props ) {
@@ -18,6 +19,8 @@ class Preview extends Component {
1819 time : '' ,
1920 answers : [ ] ,
2021 error : null ,
22+ success : false ,
23+ file : ''
2124 }
2225 }
2326
@@ -65,6 +68,14 @@ class Preview extends Component {
6568 }
6669 }
6770
71+ handleFileChange = ( e ) => {
72+ this . setState ( {
73+ success : false ,
74+ url : '' ,
75+ file : e . target . files [ 0 ] . name
76+ } )
77+ }
78+
6879 onSelect = ( e , id , { value} ) => {
6980 this . setState ( {
7081 answers : this . state . answers . map ( ( answer , index ) => {
@@ -86,21 +97,65 @@ class Preview extends Component {
8697 } )
8798 } )
8899 }
89-
90- checkboxChange = ( e , data , id , optionindex ) => {
91- console . log ( data )
100+
101+ checkboxChange = ( e , id , optionindex ) => {
92102 this . setState ( {
93103 answers : this . state . answers . map ( ( answer , index ) => {
94104 return id === index ? {
95105 ...answer ,
96- value : e . target . checked ? [ ...this . state . answers [ id ] . value , e . target . value ] : [ ...this . state . answers [ id ] . value . filter (
106+ value : e . target . checked ? [ ...this . state . answers [ id ] . value . filter ( ( value , idx ) => idx !== optionindex ) , e . target . value ] : [ ...this . state . answers [ id ] . value . filter (
97107 ( value , idx ) => idx !== optionindex
98108 ) ]
99109 } : answer
100110 } )
101111 } )
102112 }
103113
114+ uploadFile = ( file , id ) => {
115+ AWS . config . update ( {
116+ region : 'ap-south-1' ,
117+ accessKeyId : process . env . REACT_APP_AWSAccessKeyId ,
118+ secretAccessKey : process . env . REACT_APP_AWSSecretKey ,
119+ } )
120+ const send_file = file
121+ file = file . split ( '.' )
122+ const params = {
123+ ACL : 'public-read' ,
124+ Key : file [ 0 ] ,
125+ ContentType : 'application/octet-stream' ,
126+ Body : send_file ,
127+ Bucket : process . env . REACT_APP_Bucket
128+ }
129+ var myBucket = new AWS . S3 ( )
130+ myBucket . putObject ( params )
131+ . on ( 'httpUploadProgress' , ( evt ) => {
132+ this . setState ( {
133+ answers : this . state . answers . map ( ( answer , index ) => {
134+ return id === index ? {
135+ ...answer ,
136+ value : `https://${ process . env . REACT_APP_Bucket } .s3.amazonaws.com/${ file [ 0 ] } ` ,
137+ } : answer
138+ } ) ,
139+ progress : Math . round ( ( evt . loaded / evt . total ) * 100 ) ,
140+ } )
141+ } )
142+ . send ( ( err ) => {
143+ if ( err ) {
144+ console . log ( err )
145+ }
146+ } )
147+
148+ this . setState ( {
149+ answers : this . state . answers . map ( ( answer , index ) => {
150+ return id === index ? {
151+ ...answer ,
152+ value : this . state . url ,
153+ } : answer
154+ } ) ,
155+ url : ''
156+ } )
157+ }
158+
104159 submit = ( ) => {
105160 const data = {
106161 form : this . props . id ,
@@ -123,7 +178,6 @@ class Preview extends Component {
123178 render ( ) {
124179 const { questions } = this . props
125180 const { answers } = this . state
126- console . log ( answers )
127181 return (
128182 < Form className = 'preview' >
129183 { /* question type view based on each data type */ }
@@ -210,8 +264,8 @@ class Preview extends Component {
210264 type = 'checkbox'
211265 key = { id }
212266 value = { option }
213- checked = { answers [ index ] ? this . value === answers [ index ] . value [ id ] : false }
214- onChange = { ( event , data ) => this . checkboxChange ( event , data , index , id ) }
267+ checked = { answers [ index ] ? answers [ index ] . value [ id ] === option : false }
268+ onChange = { ( event ) => this . checkboxChange ( event , index , id ) }
215269 />
216270 )
217271 }
@@ -256,7 +310,10 @@ class Preview extends Component {
256310 label = { question . order + '. ' + question . label }
257311 required = { question . required }
258312 type = 'file'
313+ onChange = { this . handleFileChange }
314+ ref = { fileInput => ( this . fileInput = fileInput ) }
259315 />
316+ < Button basic onClick = { ( file ) => this . uploadFile ( this . state . file , index ) } > UPLOAD</ Button >
260317 < span > { question . description } </ span >
261318 </ div >
262319 : null
0 commit comments