-
Notifications
You must be signed in to change notification settings - Fork 90
/
index.js
231 lines (213 loc) · 7.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
*
* AddProject
*
*/
import React, { PropTypes, Component } from 'react';
import { connect } from 'react-redux';
import Helmet from 'react-helmet';
import { Grid, Row, Col } from 'react-flexbox-grid';
import Paper from 'material-ui/Paper';
import TextField from 'material-ui/TextField';
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';
import Divider from 'material-ui/Divider';
import Subheader from 'material-ui/Subheader';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.min.css';
import 'whatwg-fetch';
import ButtonElement from '../../components/ButtonElement';
import FormButtonElement from '../../components/FormButtonElement';
import UploadButton from '../../components/UploadButton';
// import projectData from '../../data.json';
const styles = {
Papers: {
margin: 10,
padding: 10,
},
TextFields: {
width: 500,
},
Button: {
float: 'right',
},
Divider: {
backgroundColor: '#000a12',
},
Subheader: {
color: '#007ac1',
},
ButtonMargin: {
marginRight: '5px',
backgroundColor: 'red',
},
};
export class AddProject extends React.Component {
state = { investigator: '', selectedFile: null };
handleChangeInvestigator = (event, index, value) => {
this.setState({ investigator: value });
}
handleChangeFormItems = (event) => {
const field = event.target.name;
const value = event.target.value;
this.setState({ [field]: value });
}
handleAPIErrors(response) {
if (!response.ok) {
throw Error(response.statusText);
} else {
return response;
}
}
fileChangerHandler = (event) => {
this.setState({ selectedFile: event.target.files[0] });
}
saveProject = (event) => {
event.preventDefault();
// ToDo
// Add image upload POST request
const project = {
title: this.state.projectName,
text: this.state.projectDesc,
investigator: this.state.investigator,
company: this.state.companyName,
image: this.state.selectedFile.name,
};
fetch('http://localhost:8080/projects', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(project),
})
.then((response) => {
if (response.ok) {
toast('Project added successfully !', {
position: toast.POSITION.TOP_CENTER,
autoClose: 3000,
hideProgressBar: true,
closeOnClick: true,
});
} else {
toast.error('Error while saving the project! ', {
position: toast.POSITION.TOP_CENTER,
autoClose: 3000,
hideProgressBar: true,
closeOnClick: true,
});
}
})
.catch((error) => {
if (error.message === 'Failed to fetch') {
toast.error('Error while saving the project! ' + error.message, {
position: toast.POSITION.TOP_CENTER,
autoClose: 3000,
hideProgressBar: true,
closeOnClick: true,
});
} else {
toast('Error while saving the project! ', {
position: toast.POSITION.TOP_CENTER,
autoClose: 3000,
hideProgressBar: true,
closeOnClick: true,
});
}
});
}
render() {
return (
<div>
<Helmet
title="AddProject"
meta={[
{ name: 'description', content: 'Description of AddProject' },
]}
/>
<Grid fluid>
<Row>
<ToastContainer />
<Paper style={styles.Papers}>
<Col xs>
<Subheader style={styles.Subheader}>Add Project</Subheader>
<Divider style={styles.Divider} />
<form>
<TextField
name="projectName"
hintText="Project Name"
floatingLabelText="Project Name"
fullWidth={true}
style={styles.TextFields}
value={this.state.projectName}
onChange={this.handleChangeFormItems}
/> <br />
<TextField
name="companyName"
hintText="Company Name"
floatingLabelText="Company Name"
fullWidth={true}
style={styles.TextFields}
value={this.state.companyName}
onChange={this.handleChangeFormItems}
/> <br />
<TextField
name="projectDesc"
hintText="Project Description"
floatingLabelText="Project Description"
fullWidth={true}
style={styles.TextFields}
value={this.state.projectDesc}
onChange={this.handleChangeFormItems}
/> <br />
<SelectField
floatingLabelText="Investigator"
value={this.state.investigator}
onChange={this.handleChangeInvestigator}
>
<MenuItem value={'Investigator01'} primaryText="Investigator01" />
<MenuItem value={'Investigator02'} primaryText="Investigator02" />
<MenuItem value={'Investigator03'} primaryText="Investigator03" />
<MenuItem value={'Investigator04'} primaryText="Investigator04" />
<MenuItem value={'Investigator05'} primaryText="Investigator05" />
</SelectField> <br />
{/* Creating native file input element to call by Material-ui FormButtonElement */}
<input
type="file"
onChange={this.fileChangerHandler}
style={{ display: 'none' }}
ref={(fileinput) => this.fileinput = fileinput}
/>
{/* This FormButtonElement clicks the input file button which is defined above by ref */}
<UploadButton
label={'Upload Image'}
style={styles.ButtonMargin}
backgroundColor={'#FAFAFA'}
labelColor={'#272727'}
labelPosition={'after'}
click={() => this.fileinput.click()}
/>
<br />
<div style={styles.Button}>
<FormButtonElement label={'Save Project'} style={styles.ButtonMargin} backgroundColor={'#4CAF50'} labelColor={'#fff'} labelPosition={'after'} click={this.saveProject} />
{/* <ButtonElement type={'submit'} label={'Save'} backgroundColor={'#4CAF50'} labelColor={'#fff'} labelPosition={'after'} /> */}
<ButtonElement label={'Reset'} backgroundColor={'#FF5252'} labelColor={'#fff'} labelPosition={'after'} />
</div>
</form>
</Col>
</Paper>
</Row>
</Grid>
</div>
);
}
}
AddProject.propTypes = {
dispatch: PropTypes.func.isRequired,
};
function mapDispatchToProps(dispatch) {
return {
dispatch,
};
}
export default connect(null, mapDispatchToProps)(AddProject);