import { h, Component } from 'preact'
import { Link } from 'react-router-dom'

import client from '../../client.js'

export default class FolderForm extends Component {
  constructor(props) {
    super()
    this.state = {
      name: '',
    }
    this.updateState = this.updateState.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
  }
  updateState(event){
    const name = event.target.name
    let value = event.target.value
    console.log(name, value)
    this.setState({
      [name]: value,
      error: null,
    })
  }
  handleSubmit(event) {
    event.preventDefault()
    let rec = Object.assign({}, this.state)
    delete rec.error
    client.folder.create( rec ).then( (data) => {
      console.log(data)
    })
  }
  render() {
    return (
      <div class='form'>
        <h1>Create a new folder</h1>
        <div>
          <label for='folders_folder_name'>Name</label>
          <input type='text' id='folders_folder_name' name='name' value={this.state.name} onChange={this.updateState} />
          <label for='folders_folder_description'>Description</label>
          <textarea id='folders_folder_description' name='description' value={this.state.description} onChange={this.updateState} />
        </div>
        <div>
          <label></label>
          <button onClick={this.handleSubmit}>Create</button>
        </div>
      </div>
    )
  }
}
