Advertisement
Diaxon

Untitled

Oct 5th, 2023
1,008
0
108 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useEffect, useState } from "react";
  2. import { Button, Form } from "react-bootstrap"
  3. import "bootstrap/dist/css/bootstrap.min.css"
  4. import Employees from "./Employees"
  5. import { v4 as uuid } from "uuid"
  6. import { Link, useNavigate } from "react-router-dom"
  7.  
  8. const Edit = () => {
  9.     const [name, setName] = useState("")
  10.     const [surname, setSurname] = useState("")
  11.     const [age, setAge] = useState("")
  12.     const [id, setId] = useState("")
  13.  
  14.     let history = useNavigate()
  15.  
  16.     let index = Employees.map((e) => {
  17.         return e.Id
  18.     }).indexOf(id)
  19.  
  20.     const handleSubmit = (e) => {
  21.         e.preventDefault()
  22.  
  23.         let a = Employees[index]
  24.         a.Name = name
  25.         a.Surname = surname
  26.         a.Age = age
  27.  
  28.         history("/")
  29.     }
  30.  
  31.     useEffect(() => {
  32.         setName(localStorage.getItem("Name"))
  33.         setSurname(localStorage.getItem("Surname"))
  34.         setAge(localStorage.getItem("Age"))
  35.         setId(localStorage.getItem("Id"))
  36.     }, [])
  37.  
  38.     return (<div>
  39.         <Form className={"d-grid gap-2"} style={{ margin: "15rem" }}>
  40.             <Form.Group className={"mb-3"} controlId={"formName"}>
  41.                 <Form.Control type={"text"} placeholder={"Enter name"} value={name}
  42.                     required onChange={(e) => setName(e.target.value)}>
  43.                 </Form.Control>
  44.             </Form.Group>
  45.             <Form.Group className={"mb-3"} controlId={"formSurname"}>
  46.                 <Form.Control type={"text"} placeholder={"Enter surname"} value={surname}
  47.                     required onChange={(e) => setSurname(e.target.value)}>
  48.                 </Form.Control>
  49.             </Form.Group>
  50.             <Form.Group className={"mb-3"} controlId={"formAge"}>
  51.                 <Form.Control type={"text"} placeholder={"Enter age"} value={age}
  52.                     required onChange={(e) => setAge(e.target.value)}>
  53.                 </Form.Control>
  54.             </Form.Group>
  55.             <Button onClick={(e) => handleSubmit(e)} type={"submit"}>Update</Button>
  56.         </Form>
  57.     </div>)
  58. }
  59.  
  60. export default Edit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement