All files / src/views InvitationsPage.vue

93.84% Statements 61/65
87.5% Branches 35/40
84% Functions 21/25
93.65% Lines 59/63

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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215                  1x       39x     11x         2x                             1x 1x     20x     20x   1x               1x   31x   1x     31x   1x 1x             1x 1x             1x 1x 3x             1x                             31x                                                                                         28x 28x 28x 28x 28x 28x 28x 28x 28x 28x         28x 28x 6x 5x   28x 28x 28x         48x 16x       28x     28x     4x 4x 3x 3x   3x 3x   1x 1x         4x 4x       2x   2x 2x 1x   1x 1x        
<template>
  <PageLayout :title="t('navigation.invitations')" class="invitations-page">
    <template #action>
      <button
        class="btn btn-outline-secondary refresh-invitations-btn"
        :disabled="loading"
        @click="refreshInvitations"
      >
        <font-awesome-icon icon="fa-solid fa-rotate-right" class="me-md-2" />
        <span class="d-none d-md-inline">{{ t('common.refresh') }}</span>
      </button>
    </template>
 
    <LoadingSpinner v-if="loading" />
 
    <EmptyState v-else-if="!loading && invitations.length === 0">
      {{ t('invitation.noPending') }}
    </EmptyState>
 
    <SearchFilterHeader
      v-else
      v-model="invitationSearchQuery"
      mode="client"
      :items="invitations"
      :search-fields="invitationSearchFields"
      :sort-value="invitationSortField"
      :sort-direction="invitationSortDirection"
      :get-sort-value="getInvitationSortValue"
      :placeholder="t('invitation.searchPlaceholder')"
    >
      <template #extra>
        <SortControl
          :options="invitationSortOptions"
          :field-value="invitationSortField"
          :direction="invitationSortDirection"
          :label="t('common.sortBy')"
          @update:field-value="invitationSortField = $event"
          @toggle-direction="
            invitationSortDirection = invitationSortDirection === 'asc' ? 'desc' : 'asc'
          "
        />
      </template>
 
      <template #default="{ filteredItems }">
        <EmptyState v-if="filteredItems.length === 0">
          {{ t('common.noResults') }}
        </EmptyState>
 
        <VirtualCardGrid
          v-else
          :items="filteredItems"
          :has-more="rolesStore.invitationsHasMore"
          :is-loading="loading"
          @load-more="rolesStore.loadMoreInvitations()"
        >
          <template #card="{ item: invitation }">
            <UnifiedContentCard>
              <h5 class="card-title mb-3">
                <font-awesome-icon icon="fa-solid fa-envelope" class="me-2 text-primary" />
                {{ invitation.flowName || invitation.flowId }}
              </h5>
 
              <div class="mb-3">
                <div class="text-muted small mb-1">{{ t('invitation.invitedBy') }}</div>
                <div>{{ invitation.invitedByName || invitation.invitedBy }}</div>
                <div v-if="invitation.invitedByNickname" class="text-muted small">
                  @{{ invitation.invitedByNickname }}
                </div>
              </div>
 
              <div class="mb-3">
                <div class="text-muted small mb-1">{{ t('invitation.date') }}</div>
                <div>
                  {{ formatDate(invitation.invitedAt) }}
                </div>
              </div>
 
              <div v-if="invitation.roles?.length" class="mb-3">
                <div class="text-muted small mb-1">{{ t('invitation.roles') }}</div>
                <div class="d-flex flex-wrap gap-1">
                  <span v-for="role in invitation.roles" :key="role.id" class="badge bg-primary">
                    {{ getTranslation(role.name) || role.id }}
                  </span>
                </div>
              </div>
 
              <template #footer>
                <div class="d-flex gap-2">
                  <button
                    class="btn btn-sm btn-success flex-fill"
                    @click="accept(invitation.flowId)"
                  >
                    <font-awesome-icon icon="fa-solid fa-check" class="me-1" />
                    {{ t('invitation.accept') }}
                  </button>
                  <button
                    class="btn btn-sm btn-danger flex-fill"
                    @click="decline(invitation.flowId)"
                  >
                    <font-awesome-icon icon="fa-solid fa-times" class="me-1" />
                    {{ t('invitation.decline') }}
                  </button>
                </div>
              </template>
            </UnifiedContentCard>
          </template>
        </VirtualCardGrid>
      </template>
    </SearchFilterHeader>
 
    <ConfirmModal
      :is-open="confirmDeclineInvitation.open"
      :title="t('invitation.declineTitle')"
      :message="t('common.confirmDecline')"
      variant="danger"
      @confirm="doDecline"
      @cancel="confirmDeclineInvitation.open = false"
    />
  </PageLayout>
 
  <FloatingPageActions :scroll-top-title="t('common.backToTop')" />
</template>
 
<script setup lang="ts">
// @implements UC-INVITE-002.1
// @implements UC-INVITE-003.1
import PageLayout from '@/components/PageLayout.vue'
import VirtualCardGrid from '@/components/VirtualCardGrid.vue'
import UnifiedContentCard from '@/components/UnifiedContentCard.vue'
import ConfirmModal from '@/components/ConfirmModal.vue'
import LoadingSpinner from '@/components/LoadingSpinner.vue'
import EmptyState from '@/components/EmptyState.vue'
import FloatingPageActions from '@/components/FloatingPageActions.vue'
import SearchFilterHeader from '@/components/SearchFilterHeader.vue'
import SortControl, { type SortOption } from '@/components/SortControl.vue'
import { onMounted, reactive, ref, computed } from 'vue'
import { useRolesStore, type Invitation } from '@/stores/roles'
import { useFlowsStore } from '@/stores/flows'
import { storeToRefs } from 'pinia'
import { useRouter } from 'vue-router'
import { useFlowLink } from '@/composables/useFlowLink'
import { flowRoute } from '@/router/flowRoutes'
import { useUILanguage } from '@/composables/useUILanguage'
import { useDateFormat } from '@/composables/useDateFormat'
import { useLanguage } from '@/composables/useLanguage'
import { useToastStore } from '@/stores/toast'
 
const rolesStore = useRolesStore()
const flowsStore = useFlowsStore()
const toastStore = useToastStore()
const router = useRouter()
const { slugOf } = useFlowLink()
const { t } = useUILanguage()
const { formatDate } = useDateFormat()
const { getTranslation } = useLanguage()
const { invitations, invitationsLoading: loading } = storeToRefs(rolesStore)
const confirmDeclineInvitation = reactive({ open: false, flowId: '' })
 
// Client-side search/sort, applied only to the invitations already loaded —
// the list is paginated (see loadMoreInvitations), so an invitation further
// down the list that hasn't been fetched yet won't be matched until it is.
const invitationSearchQuery = ref('')
const invitationSearchFields = [
  (invitation: Invitation) => invitation.flowName || invitation.flowId,
  (invitation: Invitation) => invitation.invitedByName || invitation.invitedBy || ''
]
const invitationSortField = ref('invitedAt')
const invitationSortDirection = ref<'asc' | 'desc'>('desc')
const invitationSortOptions = computed<SortOption[]>(() => [
  { value: 'invitedAt', label: t('invitation.date') },
  { value: 'flowName', label: t('invitation.flowName') }
])
function getInvitationSortValue(invitation: Invitation, field: string): string | number {
  if (field === 'invitedAt') return new Date(invitation.invitedAt).getTime()
  return (invitation.flowName || invitation.flowId).toLowerCase()
}
 
async function refreshInvitations() {
  await rolesStore.fetchInvitations()
}
 
onMounted(refreshInvitations)
 
async function accept(flowId: string) {
  try {
    await flowsStore.acceptInvitation(flowId)
    rolesStore.removeInvitation(flowId)
    toastStore.success(t('invitation.acceptSuccess'))
    // Opens the flow just joined (acceptInvitation reloaded the memberships)
    const flowSlug = slugOf(flowId)
    void router.push(flowSlug ? flowRoute(flowSlug, 'home') : { name: 'root' })
  } catch (error) {
    console.error('Failed to accept invitation:', error)
    toastStore.error(t('invitation.acceptError'))
  }
}
 
function decline(flowId: string) {
  confirmDeclineInvitation.flowId = flowId
  confirmDeclineInvitation.open = true
}
 
async function doDecline() {
  confirmDeclineInvitation.open = false
 
  try {
    await rolesStore.declineInvitation(confirmDeclineInvitation.flowId)
    toastStore.success(t('invitation.declineSuccess'))
  } catch (error) {
    console.error('Failed to decline invitation:', error)
    toastStore.error(t('invitation.declineError'))
  }
}
</script>