<commit_before>import React from 'react';
import ShowThumbnail from './ShowThumbnail';
import sessionStore from '../stores/Session';
import { observer } from 'mobx-react';
import rootStore from '../stores/RootStore';
import { Redirect } from 'react-router'
import {FormGroup,
        Button, 
        Col,
        Alert,
        Form,
        FormControl, 
        ControlLabel} from 'react-bootstrap';

        
const request = require('superagent');
const FontAwesome = require('react-fontawesome');

class AddBook extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loadingBookData: false,
      errorMsg: '',
      infoMsg: '',
      room_id: '',
      isbn: '',
      tag: '',
      title: '',
      authors: '',
      description: '',
      format: '',
      pages: '',
      publication_date: '',
      thumbnail: ''
    };
  }

  onFormInput(e) {
    this.setState({[e.target.id]: e.target.value});
  }
  onIsbnChange(e) {
    this.setState({[e.target.id]: e.target.value});
    let isbn = e.target.value;
    if (isbn.length === 13) {
      this.getBookData(isbn);
    }
  }
  getBookData(isbn) {
    let url = `${window.__appUrl}/api/books/goodreads/${isbn}`;
    this.setState({loadingBookData: true});
    request
      .get(url)
      .type('application/json')
      .on('error', (err) => {
        this.setState({
          errorMsg: `Unable to fetch book info for ${isbn}`,
          loadingBookData: false
        });
      })
      .end((err, res) => {
        if (err) {
          return;
        }
        this.setState({loadingBookData: false});
        this.setState({errorMsg: null});
        let state = Object.assign({}, res.body);
        state.pages = state.num_pages;
        state.isbn = isbn;
        delete state.num_pages;
        delete state.errorMsg;
        this.setState(state);
      });
  }

  reset(e) {
    this.setState({
        loadingBookData: false,
        errorMsg: null,
        infoMsg: null,
        thumbnail: null,
        isbn: '',
        title: '',
        tag: '',
        authors: '',
        description: '',
        format: '',
        pages: '',
        publication_date: ''
     });
  }

  submitBook() {
    let url = `${window.__appUrl}/api/books/${this.state.tag}`;
    request
      .put(url)
      .send(this.state)
      .type('application/json')
      .on('error', (err) => {
        this.setState({errorMsg: `Unable to post new book: ${err.response.text}`});
      })
      .end((err, res) => {
        if (!err) {
          this.setState({
              errorMsg: null,
              infoMsg: `New book with tag ${res.body.tag} added!`,
            });
        }
      });
  }

  onRoomChange(e) {
    this.setState({room_id: e.target.value});
    rootStore.selectRoom(e.target.value);
  }

  render() {
    const ErrAlert = this.state.errorMsg ? <Alert bsStyle="danger"><strong>{this.state.errorMsg}</strong></Alert> : null;
    const InfoAlert = this.state.infoMsg ? <Alert bsStyle="success"><strong>{this.state.infoMsg}</strong></Alert> : null;
    let rooms = []
    this.props.sites.map((site) =>
      site.rooms.map((room) =>
        rooms.push({name: `${site.name}-${room.name}`, id: room.id})
      )
    );
    if (!sessionStore.loggedIn) {<commit_msg>Parse correct book_id from add book response<commit_after>import React from 'react';
import ShowThumbnail from './ShowThumbnail';
import sessionStore from '../stores/Session';
import { observer } from 'mobx-react';
import rootStore from '../stores/RootStore';
import { Redirect } from 'react-router'
import {FormGroup,
        Button, 
        Col,
        Alert,
        Form,
        FormControl, 
        ControlLabel} from 'react-bootstrap';

        
const request = require('superagent');
const FontAwesome = require('react-fontawesome');

class AddBook extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loadingBookData: false,
      errorMsg: '',
      infoMsg: '',
      room_id: '',
      isbn: '',
      tag: '',
      title: '',
      authors: '',
      description: '',
      format: '',
      pages: '',
      publication_date: '',
      thumbnail: ''
    };
  }

  onFormInput(e) {
    this.setState({[e.target.id]: e.target.value});
  }
  onIsbnChange(e) {
    this.setState({[e.target.id]: e.target.value});
    let isbn = e.target.value;
    if (isbn.length === 13) {
      this.getBookData(isbn);
    }
  }
  getBookData(isbn) {
    let url = `${window.__appUrl}/api/books/goodreads/${isbn}`;
    this.setState({loadingBookData: true});
    request
      .get(url)
      .type('application/json')
      .on('error', (err) => {
        this.setState({
          errorMsg: `Unable to fetch book info for ${isbn}`,
          loadingBookData: false
        });
      })
      .end((err, res) => {
        if (err) {
          return;
        }
        this.setState({loadingBookData: false});
        this.setState({errorMsg: null});
        let state = Object.assign({}, res.body);
        state.pages = state.num_pages;
        state.isbn = isbn;
        delete state.num_pages;
        delete state.errorMsg;
        this.setState(state);
      });
  }

  reset(e) {
    this.setState({
        loadingBookData: false,
        errorMsg: null,
        infoMsg: null,
        thumbnail: null,
        isbn: '',
        title: '',
        tag: '',
        authors: '',
        description: '',
        format: '',
        pages: '',
        publication_date: ''
     });
  }

  submitBook() {
    let url = `${window.__appUrl}/api/books/${this.state.tag}`;
    request
      .put(url)
      .send(this.state)
      .type('application/json')
      .on('error', (err) => {
        this.setState({errorMsg: `Unable to post new book: ${err.response.text}`});
      })
      .end((err, res) => {
        if (!err) {
          this.setState({
              errorMsg: null,
              infoMsg: `New book with tag ${res.body.id} added!`,
            });
        }
      });
  }

  onRoomChange(e) {
    this.setState({room_id: e.target.value});
    rootStore.selectRoom(e.target.value);
  }

  render() {
    const ErrAlert = this.state.errorMsg ? <Alert bsStyle="danger"><strong>{this.state.errorMsg}</strong></Alert> : null;
    const InfoAlert = this.state.infoMsg ? <Alert bsStyle="success"><strong>{this.state.infoMsg}</strong></Alert> : null;
    let rooms = []
    this.props.sites.map((site) =>
      site.rooms.map((room) =>
        rooms.push({name: `${site.name}-${room.name}`, id: room.id})
      )
    );
    if (!sessionStore.loggedIn) {
{'fraction_non_alphanumeric': 0.16610891390325525, 'fraction_numerical': 0.000608457560085184, 'mean_word_length': 2.1294621608757733, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '349272', 'n_tokens_mistral': 2077, 'n_tokens_neox': 2084, 'n_words': 394}