public/javascript/comment.js
// write js to enable users to comment on posts
write js to enable users to comment on posts
write js to enable users to comment on posts
async function commentFormHandler(event) {
    event.preventDefault();
    const comment_text = document.querySelector('textarea[name="comment-body"]').value.trim();
    const post_id = window.location.toString().split('/')[
      window.location.toString().split('/').length - 1
    ];
    if (comment_text) {
        const response = await fetch('/api/comments', {
          method: 'POST',
          body: JSON.stringify({
            post_id,
            comment_text
          }),
          headers: {
            'Content-Type': 'application/json'
          }
        });
        if (response.ok) {
          document.location.reload();
        } else {
          alert(response.statusText);
        }
      }
  }
async function commentFormHandler ( event ) { event . preventDefault ( ) ; const comment_text = document . querySelector ( 'textarea[name="comment-body"]' ) . value . trim ( ) ; const post_id = window . location . toString ( ) . split ( '/' ) [ window . location . toString ( ) . split ( '/' ) . length - 1 ] ; if ( comment_text ) { const response = await fetch ( '/api/comments' , { method : 'POST' , body : JSON . stringify ( { post_id , comment_text } ) , headers : { 'Content-Type' : 'application/json' } } ) ; if ( response . ok ) { document . location . reload ( ) ; } else { alert ( response . statusText ) ; } } }
write js to enable users to comment on posts
write js to enable users to comment on posts