Skip to content

Commit 0c785cb

Browse files
committed
feat: Remove state error problems
Modify upload file process
1 parent 076c907 commit 0c785cb

File tree

5 files changed

+67
-88
lines changed

5 files changed

+67
-88
lines changed

src/Routes.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import Dashboard from './components/Dashboard'
66
import Forms from './components/Forms'
77
import Submission from './components/Submission'
88
import Questions from './components/Questions'
9-
import { login, register, dashboard, forms, upload, submission, urlBaseFrontend } from './urls'
10-
import Upload from './components/Upload'
9+
import { login, register, dashboard, forms, submission, urlBaseFrontend } from './urls'
1110
import {PrivateRoute} from './PrivateRoute'
1211
import { AuthRoute } from './AuthRoute'
1312

@@ -22,7 +21,6 @@ export default class Routes extends Component {
2221
<PrivateRoute exact path={`${urlBaseFrontend()}form/:id`} component={Questions} />
2322
<AuthRoute path={login()} component={Login} />
2423
<AuthRoute path={register()} component={Register} />
25-
<Route path={upload()} component={Upload} />
2624
<Route render={() => <Redirect to='/' />} />
2725
</Switch>
2826
</>

src/components/Preview.js

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import PropTypes from 'prop-types'
77
import '../styles/Questions.css'
88
import { DateInput, TimeInput } from 'semantic-ui-calendar-react';
99
import { 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

1213
class 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

src/components/Upload.js

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/reducers/answer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const answerReducer = (state = initialState, action) => {
2121
return {
2222
...state,
2323
postanswers: action.payload,
24-
// questions: action.payload
24+
answers: action.payload
2525
};
2626
case ANSWER_ERROR:
2727
return {

src/urls.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ export function submission() {
2727
return `${urlBaseFrontend()}submission`
2828
}
2929

30-
export function upload() {
31-
return `${urlBaseFrontend()}upload`
32-
}
33-
3430
// backend URLs
3531
export function urlBaseBackend() {
3632
return `http://localhost:8000/api`

0 commit comments

Comments
 (0)