Guest User

Untitled

a guest
Dec 29th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Fragment, useState, useContext } from "react";
  2. import { Button, Form, FormGroup, Label, Input, Row, Col } from "reactstrap";
  3. import Axios from "axios";
  4. import { SocialContext } from "./SocialContext";
  5.  
  6. const SocialForm = () => {
  7.     const [formData, setFormData] = useState({
  8.         linkName: "",
  9.         link: ""
  10.     });
  11.     const [socialList, setSocialList] = useContext(SocialContext);
  12.     const { linkName, link } = formData;
  13.     const onChange = e =>
  14.         setFormData({ ...formData, [e.target.name]: e.target.value });
  15.     const token = localStorage.getItem("token");
  16.     const handleSubmit = e => {
  17.         e.preventDefault();
  18.         const socialList = {
  19.             linkName,
  20.             link
  21.         };
  22.         try {
  23.             const config = {
  24.                 headers: {
  25.                     "Content-Type": "application/json",
  26.                     "x-auth-token": `${token}`
  27.                 }
  28.             };
  29.             const body = JSON.stringify(socialList);
  30.             Axios.post("/api/social", body, config)
  31.                 .then(
  32.                     setSocialList(prev => [
  33.                         ...prev,
  34.                         { _id: Math.random(), link, linkName }
  35.                     ])
  36.                 )
  37.                 .then(setFormData({ link: " ", linkName: " " }));
  38.         } catch (err) {
  39.             console.error(err);
  40.         }
  41.     };
  42.     return (
  43.         <Fragment>
  44.             <br />
  45.             <Form onSubmit={e => handleSubmit(e)} className="socialForm">
  46.                 <Row form>
  47.                     <Col md={5}>
  48.                         <FormGroup>
  49.                             <Label for="LinkName">LinkName</Label>
  50.                             <Input
  51.                                 type="text"
  52.                                 name="linkName"
  53.                                 placeholder="Ex. Facebook"
  54.                                 onChange={e => onChange(e)}
  55.                             />
  56.                         </FormGroup>
  57.                     </Col>
  58.                     <Col md={5}>
  59.                         <FormGroup>
  60.                             <Label for="Link">Link</Label>
  61.                             <Input
  62.                                 type="text"
  63.                                 name="link"
  64.                                 placeholder="Ex. https://www.facebook.com/"
  65.                                 onChange={e => onChange(e)}
  66.                             />
  67.                         </FormGroup>
  68.                     </Col>
  69.                 </Row>
  70.                 <Button>Add</Button>
  71.             </Form>
  72.         </Fragment>
  73.     );
  74. };
  75.  
  76. export default SocialForm;
Add Comment
Please, Sign In to add comment