portfolio/src/main/webapp/comments.js
/* this file is a set of methods for fetching/creating comments */
this file is a set of methods for fetching/creating comments
this file is a set of methods for fetching / creating comments
async function fetchComments() {
  const commentsResponse = await fetch('/comments');
  const commentsJson = await commentsResponse.json();
  const authResponse = await fetch('/auth');
  const authState = await authResponse.json();
  const commentsMap = {};
  for (const i in commentsJson) {
    const comment = commentsJson[i];
    commentsMap[comment.key] = comment;
  }
  for (const i in commentsMap) {
    const comment = commentsMap[i];
    if (!comment.replyTo) {
      buildCommentsTree(commentsMap, comment, 0, authState);
    }
  }
  if (!commentsJson.length) {
    createNoCommentsTag();
  }
  if (authState.authenticated) {
    createNewCommentForm();
  }
  addActionButtonsBasedOnAuthState(authState);
}
async function fetchComments ( ) { const commentsResponse = await fetch ( '/comments' ) ; const commentsJson = await commentsResponse . json ( ) ; const authResponse = await fetch ( '/auth' ) ; const authState = await authResponse . json ( ) ; const commentsMap = { } ; for ( const i in commentsJson ) { const comment = commentsJson [ i ] ; commentsMap [ comment . key ] = comment ; } for ( const i in commentsMap ) { const comment = commentsMap [ i ] ; if ( ! comment . replyTo ) { buildCommentsTree ( commentsMap , comment , 0 , authState ) ; } } if ( ! commentsJson . length ) { createNoCommentsTag ( ) ; } if ( authState . authenticated ) { createNewCommentForm ( ) ; } addActionButtonsBasedOnAuthState ( authState ) ; }
this file is a set of methods for fetching/creating comments
this file is a set of methods for fetching / creating comments
// create a hash table of type "comment id" -> "comment" to build a comment tree // build a tree, starting DFS from the vertex (comments) that have no incoming vertexes (are not replies) // if there are no comments, add tag 'no comments' // if client is authenticated, add new comment form