Advertisement
Guest User

Untitled

a guest
Apr 17th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.29 KB | None | 0 0
  1. import React, { Component } from "react";
  2.  
  3. import "./RegistrationPage.css";
  4. import { Grid, Button, Header, Form, Segment } from "semantic-ui-react";
  5.  
  6. import { withRouter } from "react-router-dom";
  7. import PropTypes from "prop-types";
  8. import { connect } from "react-redux";
  9. import { registerUser } from "../actions/authActions";
  10.  
  11. class RegistrationPage extends Component {
  12. constructor() {
  13. super();
  14. this.state = {
  15. first_name: "", //na camelCase
  16. last_name: "",
  17. email: "",
  18. password: "",
  19. passwordConfirmation: "",
  20. errors: {},
  21. first_nameErrorEmpty: "", //trzymajcie się jednego stylu - powinno być firstNameErrorEmpty
  22. first_nameErrorWhitespaces: "",
  23. last_nameErrorEmpty: "",
  24. last_nameErrorWhitespaces: "",
  25. emailErrorEmpty: "",
  26. emailErrorDomain: "",
  27. emailErrorWhitespaces: "",
  28. passwordErrorEmpty: "",
  29. passwordErrorLength: "",
  30. passwordErrorWhitespaces: "",
  31. passwordConfirmationErrorEmpty: "",
  32. passwordConfirmationErrorMatch: "",
  33. passwordConfirmationErrorWhitespaces: ""
  34. };
  35. }
  36.  
  37. componentDidMount() {
  38. // If logged in and user navigates to Register page, should redirect them to dashboard
  39. if (this.props.auth.isAuthenticated) {
  40. this.props.history.push("/dashboard");
  41. }
  42. }
  43.  
  44. componentWillReceiveProps(nextProps) {
  45. if (nextProps.errors) {
  46. this.setState({
  47. errors: nextProps.errors
  48. });
  49. }
  50. }
  51.  
  52. //do usunięcia
  53. onChange = e => {
  54. this.setState({ [e.target.id]: e.target.value });
  55. };
  56.  
  57. //zamiast onChange
  58. updateInput = (e, getErrorMessages) => {
  59. this.setState({
  60. ...getErrorMessages(e.target.value),
  61. [e.target.id]: e.target.value
  62. });
  63. };
  64.  
  65. //każdy input powinien mieć własną walidację
  66. getFirstNameErrorMessages = firstName => {
  67. return {
  68. first_nameErrorEmpty: firstName.trim() === "" ? "First name cannot be empty" : "",
  69. first_nameErrorWhitespaces: /\s/.test(firstName) ? "First name cannot contain whitespaces" : ""
  70. };
  71. };
  72.  
  73. getLastNameErrorMessages = lastName => {
  74. return {
  75. last_nameErrorEmpty: lastName.trim() === "" ? "Last name cannot be empty" : "",
  76. last_nameErrorWhitespaces: /\s/.test(lastName) ? "Last name cannot contain whitespaces" : ""
  77. }
  78. };
  79.  
  80. // validate = () => {
  81. // let isError = false;
  82. // const errors = {
  83. // first_nameErrorEmpty: "",
  84. // first_nameErrorWhitespaces: "",
  85. // last_nameErrorEmpty: "",
  86. // last_nameErrorWhitespaces: "",
  87. // emailErrorEmpty: "",
  88. // emailErrorDomain: "",
  89. // emailErrorWhitespaces: "",
  90. // passwordErrorEmpty: "",
  91. // passwordErrorLength: "",
  92. // passwordErrorWhitespaces: "",
  93. // passwordConfirmationErrorEmpty: "",
  94. // passwordConfirmationErrorMatch: "",
  95. // passwordConfirmationErrorWhitespaces: ""
  96. // };
  97. // if (this.state.first_name.trim() === "") {
  98. // isError = true;
  99. // errors.first_nameErrorEmpty = "First name cannot be empty";
  100. // }
  101. //
  102. // if (/\s/.test(this.state.first_name)) {
  103. // // \s is regex for whitespaces
  104. // isError = true;
  105. // errors.first_nameErrorWhitespaces =
  106. // "First name cannot contain whitespaces";
  107. // }
  108. //
  109. // if (this.state.last_name.trim() === "") {
  110. // isError = true;
  111. // errors.last_nameErrorEmpty = "Last name cannot be empty";
  112. // }
  113. //
  114. // if (/\s/.test(this.state.last_name)) {
  115. // isError = true;
  116. // errors.last_nameErrorWhitespaces = "Last name cannot contain whitespaces";
  117. // }
  118. //
  119. // if (this.state.email.trim() === "") {
  120. // isError = true;
  121. // errors.emailErrorEmpty = "Email cannot be empty";
  122. // }
  123. //
  124. // if (/\s/.test(this.state.email)) {
  125. // isError = true;
  126. // errors.emailErrorWhitespaces = "Email cannot contain whitespaces";
  127. // }
  128. //
  129. // var emailRegex = /^[a-zA-Z0-9_.+-]+@nokia\.com$/;
  130. //
  131. // if (!emailRegex.test(this.state.email)) {
  132. // isError = true;
  133. // errors.emailErrorDomain =
  134. // "Email needs to be from nokia domain, eg. example@nokia.com";
  135. // }
  136. //
  137. // if (this.state.password.trim() === "") {
  138. // isError = true;
  139. // errors.passwordErrorEmpty = "Password cannot be empty";
  140. // }
  141. //
  142. // if (/\s/.test(this.state.password)) {
  143. // isError = true;
  144. // errors.passwordErrorWhitespaces = "Password cannot contain whitespaces";
  145. // }
  146. //
  147. // if (this.state.password.length < 6) {
  148. // isError = true;
  149. // errors.passwordErrorLength = "Password must have at least 6 characters ";
  150. // }
  151. //
  152. // if (this.state.passwordConfirmation.trim() === "") {
  153. // isError = true;
  154. // errors.passwordConfirmationErrorEmpty =
  155. // "Password Confirmation cannot be empty";
  156. // }
  157. //
  158. // if (/\s/.test(this.state.passwordConfirmation)) {
  159. // isError = true;
  160. // errors.passwordConfirmationErrorWhitespaces =
  161. // "Password Confirmation cannot contain whitespaces";
  162. // }
  163. //
  164. // if (this.state.password !== this.state.passwordConfirmation) {
  165. // isError = true;
  166. // errors.passwordConfirmationErrorMatch =
  167. // "Password and Password Confirmation must match";
  168. // }
  169. //
  170. // if (isError) {
  171. // this.setState(errors);
  172. // }
  173. //
  174. //
  175. // return isError;
  176. // };
  177.  
  178. onSubmit = e => {
  179. e.preventDefault();
  180.  
  181. //todo do dokończenia
  182. const errors = {
  183. ...this.getFirstNameErrorMessages(this.state.first_name),
  184. ...this.getLastNameErrorMessages(this.state.last_name)
  185. };
  186.  
  187. const hasErrors = Object.values(errors).some(message => message !== "");
  188.  
  189. if (!hasErrors) {
  190. /* ogólnie ta zmiana stanu nie będzie potrzebna - wszystko pójdzie w real timie*/
  191. this.setState({
  192. /*first_name: "",
  193. last_name: "",
  194. email: "",
  195. password: "",
  196. passwordConfirmation: "", <-- to jest niepotrzebne - powoduje utratę danych (przypadek z tym samym e-mailem),
  197. to jednak nie było związane z dispatcherem ;C*/
  198. errors: {},
  199. first_nameErrorEmpty: "",
  200. first_nameErrorWhitespaces: "",
  201. last_nameErrorEmpty: "",
  202. last_nameErrorWhitespaces: "",
  203. emailErrorEmpty: "",
  204. emailErrorDomain: "",
  205. emailErrorWhitespaces: "",
  206. passwordErrorEmpty: "",
  207. passwordErrorLength: "",
  208. passwordErrorWhitespaces: "",
  209. passwordConfirmationErrorEmpty: "",
  210. passwordConfirmationErrorMatch: "",
  211. passwordConfirmationErrorWhitespaces: ""
  212. });
  213.  
  214. const newUser = {
  215. first_name: this.state.first_name,
  216. last_name: this.state.last_name,
  217. email: this.state.email,
  218. password: this.state.password,
  219. passwordConfirmation: this.state.passwordConfirmation
  220. };
  221.  
  222. this.props.registerUser(newUser, this.props.history);
  223. } else {
  224. this.setState(errors);
  225. }
  226. };
  227.  
  228. render() {
  229. const { errors } = this.state;
  230. return (
  231. <div>
  232. <Grid
  233. textAlign="center"
  234. style={({ height: "100%" }, { marginTop: "5.5em" })}
  235. verticalAlign="middle"
  236. >
  237. <Grid.Column style={{ maxWidth: 450 }}>
  238. <Header as="h2" color="blue" textAlign="center">
  239. Registration
  240. </Header>
  241. <Form size="large" noValidate onSubmit={this.onSubmit}>
  242. <Segment stacked>
  243. <div>
  244. {/*class to invalid property, powinno być className*/}
  245. <span class="errorsColor">
  246. {this.state.first_nameErrorEmpty}
  247. </span>
  248. </div>
  249. <div>
  250. <span class="errorsColor">
  251. {this.state.first_nameErrorWhitespaces}
  252. </span>
  253. </div>
  254. <Form.Input
  255. id="first_name"
  256. name="first_name"
  257. fluid
  258. icon="user"
  259. iconPosition="left"
  260. placeholder="First Name"
  261. error={
  262. this.state.first_nameErrorEmpty ||
  263. this.state.first_nameErrorWhitespaces
  264. }
  265. value={this.state.first_name}
  266. onChange={e => this.updateInput(e, this.getFirstNameErrorMessages)}
  267. />
  268. <div>
  269. <span class="errorsColor">
  270. {this.state.last_nameErrorEmpty}
  271. </span>
  272. </div>
  273. <div>
  274. <span class="errorsColor">
  275. {this.state.last_nameErrorWhitespaces}
  276. </span>
  277. </div>
  278. <Form.Input
  279. id="last_name"
  280. name="last_name"
  281. fluid
  282. icon="user"
  283. iconPosition="left"
  284. placeholder="Last Name"
  285. error={
  286. this.state.last_nameErrorEmpty ||
  287. this.state.last_nameErrorWhitespaces
  288. }
  289. value={this.state.last_name}
  290. onChange={e => this.updateInput(e, this.getLastNameErrorMessages)}
  291. />
  292. <div>
  293. <span class="errorsColor">{this.state.emailErrorEmpty}</span>
  294. </div>
  295. <div>
  296. <span class="errorsColor">
  297. {this.state.emailErrorWhitespaces}
  298. </span>
  299. </div>
  300. <div>
  301. <span class="errorsColor">{this.state.emailErrorDomain}</span>
  302. </div>
  303. <div>
  304. <span class="errorsColor">{errors.email}</span>
  305. </div>
  306. <Form.Input
  307. id="email"
  308. name="email"
  309. fluid
  310. icon="mail"
  311. iconPosition="left"
  312. placeholder="E-mail address"
  313. error={
  314. this.state.emailErrorEmpty ||
  315. this.state.emailErrorDomain ||
  316. this.state.emailErrorWhitespaces
  317. }
  318. value={this.state.email}
  319. onChange={this.onChange}
  320. />
  321.  
  322. <div>
  323. <span class="errorsColor">
  324. {this.state.passwordErrorEmpty}
  325. </span>
  326. </div>
  327.  
  328. <div>
  329. <span class="errorsColor">
  330. {this.state.passwordErrorWhitespaces}
  331. </span>
  332. </div>
  333.  
  334. <div>
  335. <span class="errorsColor">
  336. {this.state.passwordErrorLength}
  337. </span>
  338. </div>
  339. <Form.Input
  340. id="password"
  341. name="password"
  342. fluid
  343. icon="lock"
  344. iconPosition="left"
  345. placeholder="Password"
  346. type="password"
  347. error={
  348. this.state.passwordErrorEmpty ||
  349. this.state.passwordErrorLength ||
  350. this.state.passwordErrorWhitespaces
  351. }
  352. value={this.state.password}
  353. onChange={this.onChange}
  354. />
  355. <div>
  356. <span class="errorsColor">
  357. {this.state.passwordConfirmationErrorEmpty}
  358. </span>
  359. </div>
  360. <div>
  361. <span class="errorsColor">
  362. {this.state.passwordConfirmationErrorWhitespaces}
  363. </span>
  364. </div>
  365. <div>
  366. <span class="errorsColor">
  367. {this.state.passwordConfirmationErrorMatch}
  368. </span>
  369. </div>
  370. <Form.Input
  371. id="passwordConfirmation"
  372. name="passwordConfirmation"
  373. fluid
  374. icon="lock"
  375. iconPosition="left"
  376. placeholder="Password Confrimation"
  377. type="password"
  378. error={
  379. this.state.passwordConfirmationErrorEmpty ||
  380. this.state.passwordConfirmationErrorMatch ||
  381. this.state.passwordConfirmationErrorWhitespaces
  382. }
  383. value={this.state.passwordConfirmation}
  384. onChange={this.onChange}
  385. />
  386.  
  387. <Button color="blue" fluid size="large">
  388. Register
  389. </Button>
  390. </Segment>
  391. </Form>
  392. </Grid.Column>
  393. </Grid>
  394. </div>
  395. );
  396. }
  397. }
  398.  
  399. RegistrationPage.propTypes = {
  400. registerUser: PropTypes.func.isRequired,
  401. auth: PropTypes.object.isRequired,
  402. errors: PropTypes.object.isRequired
  403. };
  404.  
  405. const mapStateToProps = state => ({
  406. auth: state.auth,
  407. errors: state.errors
  408. });
  409.  
  410. export default connect(
  411. mapStateToProps,
  412. { registerUser }
  413. )(withRouter(RegistrationPage));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement