|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import { Form, Button, Row, Col, Container } from "react-bootstrap"; |
| 3 | +import { useHistory } from "react-router"; |
| 4 | + |
| 5 | +export const TargetCreate = () => { |
| 6 | + const [firstName, setFirstName] = useState<string>(""); |
| 7 | + const [lastName, setLastName] = useState<string>(""); |
| 8 | + const [dob, setDob] = useState<string>(""); |
| 9 | + const [disabled, setDisabled] = useState<boolean>(true); |
| 10 | + const history = useHistory(); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + if (firstName.length > 0 && lastName.length > 0) { |
| 14 | + setDisabled(false); |
| 15 | + } else { |
| 16 | + setDisabled(true); |
| 17 | + } |
| 18 | + }, [firstName, lastName]); |
| 19 | + |
| 20 | + const createTarget = (e: React.MouseEvent) => { |
| 21 | + fetch("/api/targets", { |
| 22 | + method: "POST", |
| 23 | + headers: { "content-type": "application/json" }, |
| 24 | + body: JSON.stringify({ |
| 25 | + first_name: firstName, |
| 26 | + last_name: lastName, |
| 27 | + dob: dob, |
| 28 | + }), |
| 29 | + }) |
| 30 | + .then((res) => res.json()) |
| 31 | + .then((data) => history.push("/targets/" + data.id)) |
| 32 | + .catch(console.error); |
| 33 | + }; |
| 34 | + |
| 35 | + return ( |
| 36 | + <Container> |
| 37 | + <Row> |
| 38 | + <Col> |
| 39 | + <Form> |
| 40 | + <Form.Group> |
| 41 | + <Form.Label>First Name</Form.Label> |
| 42 | + <Form.Control |
| 43 | + type="text" |
| 44 | + value={firstName} |
| 45 | + onChange={(e) => setFirstName(e.target.value)} |
| 46 | + /> |
| 47 | + </Form.Group> |
| 48 | + <Form.Group> |
| 49 | + <Form.Label>Last Name</Form.Label> |
| 50 | + <Form.Control |
| 51 | + type="text" |
| 52 | + value={lastName} |
| 53 | + onChange={(e) => setLastName(e.target.value)} |
| 54 | + /> |
| 55 | + </Form.Group> |
| 56 | + <Form.Group> |
| 57 | + <Form.Label>Date of Birth</Form.Label> |
| 58 | + <Form.Control |
| 59 | + type="date" |
| 60 | + value={dob} |
| 61 | + onChange={(e) => setDob(e.target.value)} |
| 62 | + /> |
| 63 | + </Form.Group> |
| 64 | + <Button disabled={disabled} onClick={createTarget}> |
| 65 | + Create |
| 66 | + </Button> |
| 67 | + </Form> |
| 68 | + </Col> |
| 69 | + </Row> |
| 70 | + </Container> |
| 71 | + ); |
| 72 | +}; |
0 commit comments