All files / src/components CommentBlocksList.vue

100% Statements 28/28
100% Branches 18/18
100% Functions 7/7
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101  29x 6x                   17x           6x               3x                               6x                               11x   11x 17x   11x 11x 11x   11x     12x 12x             2x 2x   1x         3x 3x 3x 2x   1x   3x       11x 12x    
<template>
  <div class="comment-blocks mb-3">
    <label class="form-label">{{ t('comment.blockedUsers') }}</label>
 
    <div v-if="store.error && store.blocks.length === 0" class="text-danger small" role="alert">
      {{ store.error }}
    </div>
    <p v-else-if="!store.loading && store.blocks.length === 0" class="text-muted small mb-0">
      {{ t('comment.noBlockedUsers') }}
    </p>
 
    <ul v-else class="list-unstyled mb-0">
      <li
        v-for="block in store.blocks"
        :key="block.userId"
        class="comment-block-row d-flex align-items-center gap-2 py-1"
      >
        <UserAvatar :picture-key="block.profilePicture" :size="28" />
        <span class="flex-grow-1">
          {{ block.name || t('comment.unknownAuthor') }}
          <span v-if="block.nickname" class="text-muted small">@{{ block.nickname }}</span>
        </span>
        <button
          type="button"
          class="btn btn-sm btn-outline-secondary"
          :disabled="unblocking === block.userId"
          @click="unblock(block.userId)"
        >
          {{ t('comment.unblock') }}
        </button>
      </li>
    </ul>
 
    <button
      v-if="store.nextToken"
      type="button"
      class="btn btn-link btn-sm p-0 mt-1"
      :disabled="store.loading"
      @click="loadMore"
    >
      {{ t('common.loadMore') }}
    </button>
    <div class="form-text">
      {{ t(packId ? 'store.commentBlockedUsersHelp' : 'comment.blockedUsersHelp') }}
    </div>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-COMMENT-007.1, UC-STORE-009.1
import { onMounted, ref, watch } from 'vue'
import UserAvatar from '@/components/UserAvatar.vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { useCommentBlocksStore, type CommentBlocksOwner } from '@/stores/commentBlocks'
import { useToastStore } from '@/stores/toast'
import { getErrorMessage } from '@/utils/errors'
 
// An entity's block list, or a store pack's
const props = defineProps<{ entityId?: string; packId?: string }>()
 
const owner = (): CommentBlocksOwner =>
  props.packId ? { packId: props.packId } : (props.entityId as string)
 
const { t } = useUILanguage()
const store = useCommentBlocksStore()
const toastStore = useToastStore()
 
const unblocking = ref<string | null>(null)
 
async function load() {
  try {
    await store.fetchBlocks(owner())
  } catch {
    // The store keeps the error to display
  }
}
 
async function loadMore() {
  try {
    await store.fetchBlocks(owner(), true)
  } catch (err) {
    toastStore.error(getErrorMessage(err, t('comment.loadError'), t))
  }
}
 
async function unblock(userId: string) {
  unblocking.value = userId
  try {
    await store.unblock(owner(), userId)
    toastStore.success(t('comment.unblockSuccess'))
  } catch (err) {
    toastStore.error(getErrorMessage(err, t('comment.unblockError'), t))
  } finally {
    unblocking.value = null
  }
}
 
onMounted(load)
watch(() => [props.entityId, props.packId], load)
</script>