import React, { PropTypes, Component } from 'react';
import FileUpload from './FileUpload.js';
import Rating from './Rating.js';
import BarcodeReader from './BarcodeReader.js';

// const Quagga = require('quagga').default;

class AddPost extends Component {
  constructor(props) {
    super(props);

    this.state = {
      upc: 'Scanning..',
      rating: 5,
      review: '',
      media: []
    };

    this.handleRatingChange = this.handleRatingChange.bind(this);
    this.handleReviewChange = this.handleReviewChange.bind(this);
    this.handleMediaChange = this.handleMediaChange.bind(this);
    this.handleSubmitPost = this.handleSubmitPost.bind(this);
    this.handleBarcodeChange = this.handleBarcodeChange.bind(this);
  }

  handleBarcodeChange(upc) {
    this.setState({ upc });
  }

  handleReviewChange(event) {
    this.setState({ review: event.target.value });
  }

  handleRatingChange(rating) {
    this.setState({ rating });
  }

  handleMediaChange(fileName) {
    const newMedia = this.state.media;
    newMedia.push(fileName);
    this.setState({ media: newMedia });
  }

  handleSubmitPost() {
    console.log('upc: ', this.state.upc);
    console.log('rating: ', this.state.rating);
    console.log('review: ', this.state.review);
    console.log('media: ', this.state.media);
    // request
    //   .post('/api/post')
    //   .type('form')
    //   .send({ upc: this.state.upc })
    //   .send({ rating: this.state.rating })
    //   .send({ review: this.state.review })
    //   .send({ media: JSON.stringify(this.state.media) })
    //   .end((err, res) => {
    //   });
  }

  render() {
    return (
      <div>
        <BarcodeReader handleBarcodeChange={this.handleBarcodeChange} />
        <p> UPC: {this.state.upc} </p>
        <Rating handleRatingChange={this.handleRatingChange} />
        <textarea
          type="text"
          placeholder="Your review"
          value={this.review}
          onChange={this.handleReviewChange}
        />
        <FileUpload handleFile={this.handleMediaChange} />
        <button
          onClick={this.handleSubmitPost}
          className="pure-button pure-button-primary button-xlarge button-success"
        >Post</button>
      </div>
    );
  }
}

AddPost.propTypes = {
};

export default AddPost;

@@ -46,16 +46,56 @@
 react';%0A
+import %7B connect %7D from 'react-redux';%0A%0A
 import F
@@ -215,52 +215,92 @@
 ';%0A%0A
-// const Quagga = require('quagga').default;
+import fetch from 'isomorphic-fetch';%0A%0Aimport config from '../../../env/client.js';%0A
 %0A%0Acl
@@ -1186,24 +1186,132 @@
 mitPost() %7B%0A
+    if (isNaN(this.state.upc)) %7B%0A      return;%0A    %7D%0A%0A    console.log('facebookId', this.props.facebookId);%0A
     console.
@@ -1490,92 +1490,289 @@
     
-// request%0A    //   .post('/api/post')%0A
+%0A    fetch(%60$%7Bconfig.GOBBLE_API_URL%7D/review%60, %7B%0A      method: 'POST',%0A      headers: %7B%0A        Accept: 'application/json',%0A 
     
-//
    
-.type('form')
+'Content-Type': 'application/json'%0A      %7D,
 %0A    
-//   .send(%7B upc: 
+    body: JSON.stringify(%7B%0A          facebookId: Number(this.props.facebookId),%0A          upc: Number(
 this
@@ -1781,36 +1781,28 @@
 tate.upc
- %7D)%0A
+),%0A  
     
-//
    
-.send(%7B
  rating:
@@ -1819,36 +1819,27 @@
 e.rating
- %7D)
+,
 %0A    
-//
    
-.send(%7B
+  
  review:
@@ -1860,36 +1860,27 @@
 view
- %7D)
+,
 %0A    
-//
    
-.send(%7B
+  
  media: 
 JSON
@@ -1875,31 +1875,16 @@
  media: 
-JSON.stringify(
 this.sta
@@ -1895,49 +1895,140 @@
 edia
-) %7D)%0A    //
+%0A        %7D)%0A      %7D)%0A   
    .
+th
 en
-d
 ((
-err, res) =%3E %7B%0A    //
+res) =%3E %7B%0A        console.log(res);%0A      %7D)%0A      .catch((err) =%3E %7B%0A        console.err(err);%0A   
    %7D
@@ -2687,32 +2687,202 @@
 = %7B%0A
-%7D;%0A%0Aexport default 
+  facebookId: PropTypes.string.isRequired%0A%7D;%0A%0Aconst mapStateToProps = state =%3E (%7B%0A  facebookId: state.layout.navBarUser.facebookId.toString()%0A%7D); %0A%0Aexport default connect(mapStateToProps)(
 AddPost
+)
 ;%0A

refactor to include fb id and parse fb id and upc into number before sending