function submitPost() {
  const title = document.querySelector('#title').value
  const body = document.querySelector('#message-body').value
  const id = document.querySelector('#hidden-id').value

  const data = {
    title,
    body
  }
  // validate input
  if (title === '' || body === '') {
    ui.showAlert('Please fill in all fields', 'alert alert-danger')
  } else {
    // check for edit state  by hidden ID field - only contains a value if in edit state
    if (id === '') {
      // we are in add state
      // create post
      http.post('http://localhost:3000/posts', data)
        .then(data => {
          ui.showAlert('Post added', 'alert alert-success')
          ui.clearFields()
          getPosts()
        })
        .catch(err => console.log(err))
    } else {
      // we are in edit state, update post
      http.put(`http://localhost:3000/posts/${id}`, data)
      .then(data => {
        ui.showAlert('Post updated', 'alert alert-success')
        ui.changeFormState('add')
        getPosts()
      })
      .catch(err => console.log(err))
    }




  }

}
// add post, or submit edited post via form
add post, or submit edited post via form
add post or submit edited post via form
function submitPost() {
  const title = document.querySelector('#title').value
  const body = document.querySelector('#message-body').value
  const id = document.querySelector('#hidden-id').value
  const data = {
    title,
    body
  }
  if (title === '' || body === '') {
    ui.showAlert('Please fill in all fields', 'alert alert-danger')
  } else {
    if (id === '') {
      http.post('http://localhost:3000/posts', data)
        .then(data => {
          ui.showAlert('Post added', 'alert alert-success')
          ui.clearFields()
          getPosts()
        })
        .catch(err => console.log(err))
    } else {
      http.put(`http://localhost:3000/posts/${id}`, data)
      .then(data => {
        ui.showAlert('Post updated', 'alert alert-success')
        ui.changeFormState('add')
        getPosts()
      })
      .catch(err => console.log(err))
    }
  }
}
function submitPost ( ) { const title = document . querySelector ( '#title' ) . value const body = document . querySelector ( '#message-body' ) . value const id = document . querySelector ( '#hidden-id' ) . value const data = { title , body } if ( title === '' || body === '' ) { ui . showAlert ( 'Please fill in all fields' , 'alert alert-danger' ) } else { if ( id === '' ) { http . post ( 'http://localhost:3000/posts' , data ) . then ( data => { ui . showAlert ( 'Post added' , 'alert alert-success' ) ui . clearFields ( ) getPosts ( ) } ) . catch ( err => console . log ( err ) ) } else { http . put ( ` ${ id } ` , data ) . then ( data => { ui . showAlert ( 'Post updated' , 'alert alert-success' ) ui . changeFormState ( 'add' ) getPosts ( ) } ) . catch ( err => console . log ( err ) ) } } }
add post, or submit edited post via form
add post or submit edited post via form
// validate input // check for edit state  by hidden ID field - only contains a value if in edit state // we are in add state // create post // we are in edit state, update post