From 091d3f05af154c8b6758a0fb12231613120e2d60 Mon Sep 17 00:00:00 2001 From: DispatchCommit Date: Wed, 18 Mar 2020 15:25:57 -0700 Subject: [PATCH] Add comment submission handler --- assets/js/time-ago.js | 3 +- components/Replay/ReplayComments/index.vue | 88 +++++++++++++++++++++- 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/assets/js/time-ago.js b/assets/js/time-ago.js index 0e669196..467aabc5 100644 --- a/assets/js/time-ago.js +++ b/assets/js/time-ago.js @@ -27,7 +27,8 @@ const getDuration = ( timeAgoInSeconds ) => { // Calculate const timeAgo = ( date ) => { const timeAgoInSeconds = Math.floor( ( new Date() - new Date( date ) ) / 1000 ); - if ( !timeAgoInSeconds && timeAgoInSeconds > 0 ) return null; + if ( timeAgoInSeconds === 0 ) return `just now`; + if ( !timeAgoInSeconds || timeAgoInSeconds < 0 ) return null; const { interval, epoch } = getDuration( timeAgoInSeconds ); const suffix = interval === 1 ? '' : 's'; return `${interval} ${epoch}${suffix} ago`; diff --git a/components/Replay/ReplayComments/index.vue b/components/Replay/ReplayComments/index.vue index 3de5021e..e8f98393 100644 --- a/components/Replay/ReplayComments/index.vue +++ b/components/Replay/ReplayComments/index.vue @@ -10,7 +10,9 @@
@@ -20,6 +22,8 @@ > cancel @@ -27,6 +31,8 @@ tile color="primary" class="black--text" + :disabled="submittingComment" + @click="addComment" > submit @@ -52,7 +58,10 @@ color="primary" outlined small - >Load More + :loading="loadingMoreComments" + :disabled="allCommentsLoaded" + @click="onLoadMore" + >{{ allCommentsLoaded ? 'No More Comments' : 'Load More' }}
@@ -81,10 +90,51 @@ return { comments: [], showSubmit: false, + userComment: '', + submittingComment: false, + loadingMoreComments: false, + allCommentsLoaded: false, }; }, methods: { + cancelComment () { + this.userComment = ''; + this.showSubmit = false; + }, + + async addComment () { + if ( !this.archiveId ) return console.log( `No Archive ID!` ); + if ( !this.userComment || this.userComment.length < 1 ) return console.log( `No user comment!` ); + + this.submittingComment = true; + + const commentData = { + _username: this.user.username.toLowerCase(), + name: this.user.username, + owner: this.user.uid, + text: this.userComment, + timestamp: new Date(), + }; + + const commentRef = db + .collection( 'archives' ) + .doc( this.archiveId ) + .collection( 'comments' ); + + const commentId = ( await commentRef.add( commentData ) ).id; + + this.comments = [ + { + id: commentId, + ...commentData, + }, + ...this.comments, + ]; + + this.submittingComment = false; + }, + async getComments () { if ( !this.archiveId ) return console.log( `No Archive ID!` ); @@ -116,8 +166,42 @@ timestamp: comment.timestamp.toDate(), } }); - console.log( this.comments ); }, + + async onLoadMore () { + // + if ( !this.archiveId ) return console.log( `No Archive ID!` ); + if ( this.loadingMoreComments ) return console.log( `Already loading!` ) + + this.loadingMoreComments = true; + + const offset = this.comments[ this.comments.length - 1 ].timestamp; + + const commentQuery = await db + .collection( 'archives' ) + .doc( this.archiveId ) + .collection( 'comments' ) + .orderBy('timestamp', 'desc' ) + .startAfter( offset ) + .limit( 5 ) + .get(); + + commentQuery.docs.map( doc => { + const comment = doc.data(); + this.comments.push({ + id: doc.id, + _username: comment._username, + name: comment.name, + owner: comment.owner, + text: comment.text, + timestamp: comment.timestamp.toDate(), + }); + }); + + this.loadingMoreComments = false; + + if ( commentQuery.size < 5 ) this.allCommentsLoaded = true; + } }, computed: {