|
1 | 1 | import * as React from 'react';
|
2 | 2 | import './User.scss';
|
3 |
| -import { FormEvent } from 'react'; |
4 | 3 |
|
5 | 4 | import { UserEntity } from '../entities/UserEntity';
|
6 |
| -import { Connection } from '../../../node_modules/typeorm'; |
7 |
| - |
| 5 | +import { Formik } from 'formik'; |
8 | 6 |
|
9 | 7 | interface UserProps {
|
10 |
| - db: Connection |
| 8 | + fetchUser: Function; |
| 9 | + saveUser: Function; |
| 10 | + user: UserEntity|null; |
11 | 11 | }
|
12 | 12 | export class User extends React.Component<UserProps, any> {
|
13 |
| - constructor(props: any){ |
14 |
| - super(props); |
15 |
| - this.state = { |
16 |
| - formName: "" |
17 |
| - } |
18 |
| - |
19 |
| - this.handleInputChange = this.handleInputChange.bind(this); |
20 |
| - this.handleFormSubmit = this.handleFormSubmit.bind(this); |
21 |
| - } |
22 |
| - |
23 |
| - async componentDidMount() { |
24 |
| - let userRepo = this.props.db.getRepository(UserEntity); |
25 |
| - let user = await userRepo.findOne(1); |
26 |
| - let newState:any = {userRepo}; |
27 |
| - if (user) { |
28 |
| - newState.user = user; |
29 |
| - } |
30 |
| - |
31 |
| - this.setState(newState); |
32 |
| - } |
33 | 13 |
|
34 |
| - async handleFormSubmit(event: any) { |
35 |
| - event.preventDefault(); |
36 |
| - |
37 |
| - if (this.state.formName) { |
38 |
| - let user = await this.state.userRepo.findOne({name: this.state.formName}); |
39 |
| - if (!user) { |
40 |
| - user = new UserEntity(); |
41 |
| - user.name = this.state.formName; |
42 |
| - await this.state.userRepo.save(user); |
43 |
| - |
44 |
| - let savedUser = await this.state.userRepo.findOne({name: this.state.formName}); |
45 |
| - this.setState({user: savedUser}); |
46 |
| - } |
| 14 | + componentDidMount() { |
| 15 | + if (!this.props.user) { |
| 16 | + this.props.fetchUser(); |
47 | 17 | }
|
48 | 18 | }
|
49 |
| - |
50 |
| - handleInputChange(event: FormEvent<HTMLInputElement>) { |
51 |
| - const target = event.currentTarget; |
52 |
| - const value = target.type === 'checkbox' ? target.checked : target.value; |
53 |
| - const name = target.name; |
54 |
| - |
55 |
| - this.setState({ |
56 |
| - [name]: value |
57 |
| - }); |
58 |
| - } |
59 | 19 |
|
60 | 20 | render(){
|
61 | 21 | let content;
|
62 |
| - if (this.state.user) { |
| 22 | + const user = this.props.user; |
| 23 | + if (user) { |
63 | 24 | content = (
|
64 | 25 | <div className='text-center'>
|
65 |
| - <span>You're {this.state.user.name}!</span> |
| 26 | + <span>You're {user.name}!</span> |
66 | 27 | </div>
|
67 | 28 | )
|
68 | 29 | } else {
|
69 | 30 | content = (
|
70 | 31 | <div className='col-sm-6 offset-sm-3'>
|
71 |
| - <form onSubmit={this.handleFormSubmit}> |
72 |
| - <div className="form-group"> |
73 |
| - <label>Name</label> |
74 |
| - <input |
75 |
| - name="formName" |
76 |
| - value={this.state.formName} |
77 |
| - onChange={this.handleInputChange} |
78 |
| - type="text" |
79 |
| - className="form-control" |
80 |
| - placeholder="Enter Name" /> |
81 |
| - </div> |
82 |
| - <button type="submit" className="btn btn-primary">Submit</button> |
83 |
| - </form> |
| 32 | + <Formik |
| 33 | + initialValues={{ |
| 34 | + name: '' |
| 35 | + }} |
| 36 | + validate={values => { |
| 37 | + let errors:any = {}; |
| 38 | + if (values.name.length < 2) { |
| 39 | + errors.name = 'Name must be greater than 2 characters'; |
| 40 | + } |
| 41 | + return errors; |
| 42 | + }} |
| 43 | + onSubmit={( |
| 44 | + values, |
| 45 | + { setSubmitting, setErrors /* setValues and other goodies */ } |
| 46 | + ) => { |
| 47 | + this.props.saveUser(values); |
| 48 | + }} |
| 49 | + render={({ |
| 50 | + values, |
| 51 | + errors, |
| 52 | + touched, |
| 53 | + handleChange, |
| 54 | + handleBlur, |
| 55 | + handleSubmit, |
| 56 | + isSubmitting, |
| 57 | + }) => ( |
| 58 | + <form onSubmit={handleSubmit}> |
| 59 | + <div className="form-group"> |
| 60 | + <label>Name</label> |
| 61 | + <input |
| 62 | + type="text" |
| 63 | + name="name" |
| 64 | + className="form-control" |
| 65 | + onChange={handleChange} |
| 66 | + onBlur={handleBlur} |
| 67 | + value={values.name} |
| 68 | + /> |
| 69 | + {touched.name && errors.name && <div>{errors.name}</div>} |
| 70 | + </div> |
| 71 | + <button |
| 72 | + type="submit" |
| 73 | + className="btn btn-primary" |
| 74 | + disabled={isSubmitting}> |
| 75 | + Submit |
| 76 | + </button> |
| 77 | + </form> |
| 78 | + )} |
| 79 | + /> |
84 | 80 | </div>
|
85 | 81 | )
|
86 | 82 | }
|
|
0 commit comments