{"size": 1278, "ext": "js", "max_stars_repo_path": "public/js/comments.js", "max_stars_repo_name": "lancebailey26/techBlog", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "public/js/comments.js", "max_issues_repo_name": "lancebailey26/techBlog", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "public/js/comments.js", "max_forks_repo_name": "lancebailey26/techBlog", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.0816326531, "max_line_length": 87, "alphanum_fraction": 0.5962441315}
const commentHandler = async (event) => {
    event.preventDefault();
  
    
    const comment = document.querySelector('#comment-comment').value.trim();
    const posted_on = document.querySelector('#blog-id').textContent;
  
    if (comment && posted_on) {
      const response = await fetch(`/api/comments/newcomment`, {
        method: 'POST',
        body: JSON.stringify({ comment, posted_on }),
        headers: {
          'Content-Type': 'application/json',
        },
      });
  
      if (response.ok) {
        document.location.reload();
      } else {
        alert('Failed to create comment');
      }
    }
  };

  const deleteCommentButtonHandler = async (event) => {
    if (event.target.hasAttribute('data-id')) {
      const id = event.target.getAttribute('data-id');
  
      const response = await fetch(`/api/comments/${id}`, {
        method: 'DELETE',
      });
  
      if (response.ok) {
        document.location.reload();
      } else {
        alert('Failed to delete comment, you may only delete comments posted by you.');
      }
    
  };
};
  
  document
  .querySelector('.comment-form')
  .addEventListener('click', commentHandler);

  document
  .querySelector('.comment-section')
  .addEventListener('click', deleteCommentButtonHandler);
