guest_01

Untitled

Mar 11th, 2021 (edited)
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import api from "../../services/api";
  3. import { withRouter } from "react-router-dom";
  4. import { Form, Container } from "./styles";
  5. import NavBar from "../../components/navbar";
  6.  
  7. class EditUser extends Component {
  8.   constructor(props) {
  9.     super(props);
  10.     this.state = {
  11.       user: {
  12.         id: null,
  13.         firstName: "",
  14.         lastName: "",
  15.         username: "",
  16.         password: "",
  17.       },
  18.     };
  19.  
  20.     this.getUserById = this.getUserById.bind(this);
  21.     this.handleSubmit = this.handleSubmit.bind(this);
  22.     this.onChangeFirstName = this.onChangeFirstName.bind(this);
  23.     this.onChangeLastName = this.onChangeLastName.bind(this);
  24.     this.onChangeUsername = this.onChangeUsername.bind(this);
  25.     this.onChangePassword = this.onChangePassword.bind(this);
  26.   }
  27.  
  28.   onChangeFirstName(e) {
  29.     const firstName = e.target.value;
  30.  
  31.     this.setState(function (prevState) {
  32.       return {
  33.         user: {
  34.           ...prevState.firstName,
  35.           firstName: firstName,
  36.         },
  37.       };
  38.     });
  39.   }
  40.  
  41.   onChangeLastName(e) {
  42.     const lastName = e.target.value;
  43.  
  44.     this.setState((prevState) => ({
  45.       user: {
  46.         ...prevState.lastName,
  47.         lastName: lastName,
  48.       },
  49.     }));
  50.   }
  51.  
  52.   onChangeUsername(e) {
  53.     const username = e.target.value;
  54.  
  55.     this.setState((prevState) => ({
  56.       user: {
  57.         ...prevState.username,
  58.         username: username,
  59.       },
  60.     }));
  61.   }
  62.  
  63.   onChangePassword(e) {
  64.     const password = e.target.value;
  65.  
  66.     this.setState((prevState) => ({
  67.       user: {
  68.         ...prevState.password,
  69.         password: password,
  70.       },
  71.     }));
  72.   }
  73.  
  74.   componentDidMount() {
  75.     this.getUserById(this.props.match.params.id);
  76.   }
  77.  
  78.   getUserById(id) {
  79.     api
  80.       .get(`/users/${id}`)
  81.       .then((response) => {
  82.         this.setState({
  83.           user: response.data,
  84.         });
  85.       })
  86.       .catch((e) => {
  87.         console.log(e);
  88.       });
  89.   }
  90.  
  91.   handleSubmit(e) {
  92.     var data = {
  93.       id: this.state.user.id,
  94.       firstName: this.state.user.firstName,
  95.       lastName: this.state.user.lastName,
  96.       username: this.state.user.username,
  97.       password: this.state.user.password,
  98.     };
  99.     api
  100.       .put(`/users/${this.state.user.id}`, data)
  101.       .then((response) => {
  102.         this.setState((prevState) => ({
  103.           user: {
  104.             ...prevState.user,
  105.           },
  106.         }));
  107.         console.log(response.data);
  108.       })
  109.       .catch((e) => {
  110.         console.log(e);
  111.       });
  112.     e.preventDefault();
  113.     alert("Usuário atualizado com sucesso");
  114.   }
  115.  
  116.   render() {
  117.     return (
  118.       <div>
  119.         <NavBar />
  120.         <Container>
  121.           <Form onSubmit={this.handleSubmit}>
  122.             <label>Primeiro nome</label>
  123.             <input
  124.               type="text"
  125.               value={this.state.user.firstName}
  126.               onChange={this.onChangeFirstName}
  127.             />
  128.             <label>Último nome</label>
  129.             <input
  130.               type="text"
  131.               value={this.state.user.lastName}
  132.               onChange={this.onChangeLastName}
  133.             />
  134.             <label>Username</label>
  135.             <input
  136.               type="text"
  137.               value={this.state.user.username}
  138.               onChange={this.onChangeUsername}
  139.             />
  140.             <label>Password</label>
  141.             <input
  142.               type="password"
  143.               placeholder="Digite sua senha"
  144.               value={this.state.user.password}
  145.               onChange={this.onChangePassword}
  146.             />
  147.             <button type="submit">Atualizar</button>
  148.           </Form>
  149.         </Container>
  150.       </div>
  151.     );
  152.   }
  153. }
  154.  
  155. export default withRouter(EditUser);
Add Comment
Please, Sign In to add comment