All files / src/components/person add_person_page.jsx

81.82% Statements 18/22
50% Branches 1/2
75% Functions 9/12
81.82% Lines 18/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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        2x       1x 1x 1x 1x       1x                   1x 1x 1x   1x 1x           57x 57x 57x       59x               12x                   10x                   35x                      
import toastr from 'toastr';
import React from 'react';
import { savePerson, getPerson } from '../../api_calls';
 
const emptyState = { firstName: '', lastName: '', email: '', id: '' };
 
export default class AddPerson extends React.Component {
  constructor(props) {
    super(props);
    this.state = emptyState;
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }
 
  componentDidMount() {
    Iif (this.props.params.id) {
      getPerson(this.props.params.id)
        .then(response => {
          this.setState(response.data);
        })
        .catch(error => toastr.error(error));
    }
  }
 
  handleSubmit(event) {
    event.preventDefault();
    let data = { ...this.state };
    savePerson(data)
      .then(response => {
        toastr.success(response.data);
        this.setState(emptyState);
      })
      .catch(error => toastr.error(error));
  }
 
  handleChange(fieldName, event) {
    const state = {};
    state[fieldName] = event.target.value;
    this.setState(state);
  }
 
  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <div>
          <input
            id="firstName"
            type="text"
            placeholder="First name"
            value={this.state.firstName}
            onChange={e => this.handleChange('firstName', e)}
            required
          />
        </div>
        <div>
          <input
            id="lastName"
            type="text"
            placeholder="Last name"
            value={this.state.lastName}
            onChange={e => this.handleChange('lastName', e)}
            required
          />
        </div>
        <div>
          <input
            id="email"
            type="email"
            placeholder="Email"
            value={this.state.email}
            onChange={e => this.handleChange('email', e)}
            required
          />
        </div>
        <div>
          <button type="submit">Save</button>
        </div>
      </form>
    );
  }
}