All files / src/stores commentBlocks.ts

100% Statements 36/36
100% Branches 30/30
100% Functions 5/5
100% Lines 31/31

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                                          20x           7x 23x 23x 23x 23x 23x   23x     18x 18x   16x 16x 15x 15x 15x   16x 16x 16x 16x     13x 12x 18x   3x 2x 3x   16x         4x 4x 3x 3x       23x    
import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'
 
export interface CommentBlock {
  userId: string
  name: string | null
  nickname: string | null
  profilePicture: string | null
  createdAt: string
}
 
interface CommentBlockPage {
  blocks: CommentBlock[]
  nextToken: string | null
}
 
// Whose block list: an entity (its id), or a store pack
export type CommentBlocksOwner = string | { packId: string }
 
function ownerPath(owner: CommentBlocksOwner): string {
  return typeof owner === 'string' ? `/v1/entities/${owner}` : `/v1/store/${owner.packId}`
}
 
// The users blocked from commenting on one entity or pack, for its comment
// settings (managers only). Holds one list at a time, like the entity form.
// `entityId` names the list held: an entity's id, or a pack's.
export const useCommentBlocksStore = defineStore('commentBlocks', () => {
  const entityId = ref<string | null>(null)
  const blocks = ref<CommentBlock[]>([])
  const nextToken = ref<string | null>(null)
  const loading = ref(false)
  const error = ref<string | null>(null)
  // Dropped responses of a list that was replaced meanwhile (see fetchBlocks)
  let latestRequestId = 0
 
  async function fetchBlocks(owner: CommentBlocksOwner, more = false) {
    const forEntityId = typeof owner === 'string' ? owner : owner.packId
    if (more && (!nextToken.value || entityId.value !== forEntityId)) return
 
    const requestId = ++latestRequestId
    if (!more) {
      entityId.value = forEntityId
      blocks.value = []
      nextToken.value = null
    }
    loading.value = true
    error.value = null
    try {
      const response = await api.get<CommentBlockPage>(`${ownerPath(owner)}/comment-blocks`, {
        params: more && nextToken.value ? { nextToken: nextToken.value } : {}
      })
      if (requestId !== latestRequestId) return
      blocks.value = more ? [...blocks.value, ...response.data.blocks] : response.data.blocks
      nextToken.value = response.data.nextToken
    } catch (err: any) {
      if (requestId !== latestRequestId) return
      error.value = err.response?.data?.error || 'Failed to load blocked users'
      throw err
    } finally {
      if (requestId === latestRequestId) loading.value = false
    }
  }
 
  async function unblock(owner: CommentBlocksOwner, userId: string) {
    const forEntityId = typeof owner === 'string' ? owner : owner.packId
    await api.delete(`${ownerPath(owner)}/comment-blocks/${userId}`)
    if (entityId.value === forEntityId) {
      blocks.value = blocks.value.filter((block) => block.userId !== userId)
    }
  }
 
  return { entityId, blocks, nextToken, loading, error, fetchBlocks, unblock }
})