Proper error handling when fetching comments fails

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2021-04-23 15:40:39 +02:00
parent 2c2c04493c
commit 2aad2d6677

View File

@@ -23,8 +23,8 @@
</ul> </ul>
<div v-else-if="isLoading" class="icon icon-loading" /> <div v-else-if="isLoading" class="icon icon-loading" />
<div v-else class="emptycontent"> <div v-else class="emptycontent">
<div class="icon-comment" /> <div :class="{ 'icon-comment': !error, 'icon-error': error }" />
<p>{{ t('deck', 'No comments yet. Begin the discussion!') }}</p> <p>{{ error || t('deck', 'No comments yet. Begin the discussion!') }}</p>
</div> </div>
</div> </div>
</template> </template>
@@ -60,6 +60,7 @@ export default {
newComment: '', newComment: '',
isLoading: false, isLoading: false,
currentUser: getCurrentUser(), currentUser: getCurrentUser(),
error: null,
} }
}, },
computed: { computed: {
@@ -85,20 +86,34 @@ export default {
}, },
methods: { methods: {
async infiniteHandler($state) { async infiniteHandler($state) {
this.error = null
try {
await this.loadMore() await this.loadMore()
if (this.hasMoreComments(this.card.id)) { if (this.hasMoreComments(this.card.id)) {
$state.loaded() $state.loaded()
} else { } else {
$state.complete() $state.complete()
} }
} catch (e) {
console.error('Failed to fetch more comments during infinite loading', e)
this.error = t('deck', 'Failed to load comments')
$state.complete()
}
}, },
async loadComments() { async loadComments() {
this.error = null
this.isLoading = true this.isLoading = true
try {
await this.$store.dispatch('fetchComments', { cardId: this.card.id }) await this.$store.dispatch('fetchComments', { cardId: this.card.id })
this.isLoading = false this.isLoading = false
if (this.card.commentsUnread > 0) { if (this.card.commentsUnread > 0) {
await this.$store.dispatch('markCommentsAsRead', this.card.id) await this.$store.dispatch('markCommentsAsRead', this.card.id)
} }
} catch (e) {
this.isLoading = false
console.error('Failed to fetch more comments during infinite loading', e)
this.error = t('deck', 'Failed to load comments')
}
}, },
async createComment(content) { async createComment(content) {
const commentObj = { const commentObj = {