All files / src/components EntityItemCard.vue

41.17% Statements 7/17
14.28% Branches 6/42
0% Functions 0/5
40% Lines 6/15

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  4x   1x                                 1x   1x       1x                                                                                           1x                                                                                                                                                                                                                                                            
<template>
  <div class="card h-100 position-relative">
    <!-- Action buttons (top-right) -->
    <div class="card-actions">
      <button
        class="btn btn-sm btn-secondary action-icon"
        title="Edit"
        @click.stop="$emit('edit', item)"
      >
        <font-awesome-icon icon="fa-solid fa-pen" />
      </button>
      <button
        class="btn btn-sm btn-danger action-icon"
        title="Delete"
        @click.stop="$emit('delete', item.id || '')"
      >
        <font-awesome-icon icon="fa-solid fa-trash" />
      </button>
    </div>
 
    <div class="card-body">
      <div v-for="field in fields" :key="getFieldKey(field)" class="mb-3">
        <div class="text-muted small mb-1">
          {{ getTranslation(field.name) }}
          <span v-if="field.mandatory" class="text-danger">*</span>
        </div>
        <div class="field-value">
          <template v-if="!field.type || field.type === 'text'">
            {{ getTranslation(item[getFieldKey(field)] ?? {}) }}
          </template>
          <template v-else-if="field.type === 'textarea'">
            <div class="text-truncate-3">
              {{ getTranslation(item[getFieldKey(field)] ?? {}) }}
            </div>
          </template>
          <template v-else-if="field.type === 'boolean'">
            <span class="badge" :class="item[getFieldKey(field)] ? 'bg-success' : 'bg-secondary'">
              {{ item[getFieldKey(field)] ? 'Yes' : 'No' }}
            </span>
          </template>
          <template v-else-if="field.type === 'image'">
            <ImageField
              v-if="item[getFieldKey(field)]"
              :model-value="item[getFieldKey(field)]"
              :field="field"
              :is-editing="false"
              :item-id="item.id"
              no-thumbnail
              :clickable="false"
              no-wrapper
            />
            <span v-else class="text-muted">No image</span>
          </template>
          <template v-else-if="field.type === 'image_set'">
            <ImageCarousel
              v-if="item[getFieldKey(field)]?.length"
              :images="item[getFieldKey(field)]"
              :item-id="item.id"
            />
            <span v-else class="text-muted">No images</span>
          </template>
          <template v-else-if="field.type === 'date'">
            <template v-if="item[getFieldKey(field)]">
              {{ formatDate(item[getFieldKey(field)]) }}
            </template>
            <span v-else class="text-muted">-</span>
          </template>
          <template v-else>
            {{ item[getFieldKey(field)] ?? '-' }}
          </template>
        </div>
      </div>
      <div class="text-muted small mt-3 mb-2">
        Created: {{ item.createdAt ? new Date(item.createdAt).toLocaleDateString() : '' }}
      </div>
    </div>
  </div>
</template>
 
<script setup lang="ts">
import type { EntityField } from '@/stores/entities'
import ImageField from '@/components/ImageField.vue'
import ImageCarousel from '@/components/ImageCarousel.vue'
 
defineProps<{
  item: Record<string, any>
  fields: EntityField[]
  getTranslation: (value: string | Record<string, string> | null | undefined) => string
}>()
 
defineEmits<{
  edit: [item: Record<string, any>]
  delete: [id: string]
}>()
 
// Helper to get stable field key for data storage
function getFieldKey(field: { fieldId?: string; name: string | Record<string, string> }): string {
  return field.fieldId!
}
 
// Helper to format date from ISO format (YYYY-MM-DD) to localized format
function formatDate(isoDate: string): string {
  if (!isoDate) return '-'
  try {
    // Parse ISO date (YYYY-MM-DD) and format to local date string
    const date = new Date(isoDate + 'T00:00:00')
    return date.toLocaleDateString()
  } catch {
    return isoDate
  }
}
</script>
 
<style scoped>
.card {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}
 
.card-clickable {
  cursor: pointer;
  transition:
    transform 0.2s,
    box-shadow 0.2s,
    background-color 0.2s;
}
 
.card-clickable::before {
  opacity: 0 !important;
}
 
.card-clickable:hover {
  transform: scale(1.02);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
 
.card-clickable:hover::before {
  opacity: 0 !important;
}
 
.card-actions {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 10;
  display: flex;
  gap: 4px;
}
 
.action-icon {
  padding: 0.25rem 0.5rem;
  font-size: 0.875rem;
  border-radius: 4px;
  opacity: 0.9;
}
 
.action-icon:hover {
  opacity: 1;
}
 
.field-value {
  padding: 0.5rem;
  background-color: #f8f9fa;
  border-radius: 0.25rem;
  min-height: 38px;
  word-wrap: break-word;
}
 
.field-value:has(.image-carousel) {
  padding: 0;
  min-height: unset;
  background-color: transparent;
}
 
.field-value img {
  pointer-events: none;
}
 
.text-truncate-3 {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
 
@media (max-width: 767px) {
  .card-actions {
    gap: 8px;
  }
 
  .action-icon {
    padding: 0.4rem 0.65rem;
    font-size: 1rem;
  }
}
</style>