ArpitB

form js

Jul 19th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import Link from 'next/link';
  3.  
  4. export default class Form extends React.Component {
  5.     constructor() {
  6.         super();
  7.         this.state = { token: '', formFields: [], formData: {} };
  8.     }
  9.  
  10.     // Make state targets equal to the value of input fields
  11.     change = e => {
  12.         this.setState({
  13.             formData: {
  14.                 ...this.state.formData,
  15.                 [e.target.name]: e.target.value
  16.             }
  17.         });
  18.     };
  19.  
  20.     // Fetch the data form fields from API, use it in state
  21.     componentDidMount() {
  22.         fetch(
  23.             'https://rominacsvsms.api.ushahidi.io/api/v3/forms/14/attributes?order=asc&orderby=priority'
  24.         )
  25.             .then(res => res.json())
  26.             .then(data => {
  27.                 this.setState({
  28.                     formFields: data.results
  29.                 });
  30.             });
  31.  
  32.         if (!this.state.token) {
  33.             fetch('https://rominacsvsms.api.ushahidi.io/oauth/token', {
  34.                 method: 'POST',
  35.                 body: JSON.stringify({
  36.                     scope: '*',
  37.                     client_secret: '35e7f0bca957836d05ca0492211b0ac707671261',
  38.                     client_id: 'ushahidiui',
  39.                     grant_type: 'password',
  40.                     password: 'hellohello',
  41.                     username: '[email protected]'
  42.                 }),
  43.                 headers: {
  44.                     Accept: 'application/json',
  45.                     'Content-Type': 'application/json',
  46.                     Authorization: 'large-auth-string'
  47.                 }
  48.             })
  49.                 .then(response => {
  50.                     return response.json();
  51.                 })
  52.                 .then(data => {
  53.                     this.setState({ token: data });
  54.                 })
  55.                 .catch(error => {
  56.                     console.log(error);
  57.                 });
  58.         }
  59.     }
  60.  
  61.     // Handler htmlFor when the submit button is pressed
  62.     onSubmit = e => {
  63.         e.preventDefault();
  64.  
  65.         // Make a post request to Ushahidi sever
  66.         fetch('https://rominacsvsms.api.ushahidi.io/api/v3/posts', {
  67.             method: 'POST',
  68.             body: JSON.stringify(this.state.formData),
  69.             headers: {
  70.                 Accept: 'application/json',
  71.                 'Content-Type': 'application/json',
  72.                 Authorization: `Bearer ${this.state.token.access_token}`
  73.             }
  74.         })
  75.             .then(response => {
  76.                 return response.json();
  77.             })
  78.             .then(data => {
  79.                 console.log(`Success!!! \n ${data}`);
  80.             })
  81.             .catch(error => {
  82.                 console.log(error);
  83.             });
  84.  
  85.         this.setState({
  86.             formData: {}
  87.         });
  88.     };
  89.  
  90.     // Create an array of elemets to be rendered in the form
  91.     FormInputs = props => {
  92.         return props.fields.map(field => {
  93.             return (
  94.                 <div key={field.key}>
  95.                     <label htmlFor={field.label}>{field.label}</label>
  96.                     <input
  97.                         type={field.input}
  98.                         name={field.label}
  99.                         placeholder={`Enter the ${field.label}`}
  100.                         value={this.state.formData[field.label]}
  101.                         onChange={e => this.change(e)}
  102.                     />
  103.                 </div>
  104.             );
  105.         });
  106.     };
  107.  
  108.     render() {
  109.         return (
  110.             <form>
  111.                 <this.FormInputs fields={this.state.formFields} />
  112.                 <div>
  113.                     <button onClick={e => this.onSubmit(e)}>Submit</button>
  114.                 </div>
  115.             </form>
  116.         );
  117.     }
  118. }
Add Comment
Please, Sign In to add comment