All files / src/components RibbonsPanel.vue

93.65% Statements 59/63
85% Branches 17/20
93.75% Functions 15/16
95.08% Lines 58/61

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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240  55x 3x 3x                           18x       16x                   3x 3x             1x                       2x             1x                           3x                                                             11x 11x 11x 11x   11x 11x 11x 11x 11x 11x             15x       4x 4x 4x 4x       2x 2x 2x 2x       1x 1x       3x 3x 3x 3x 1x 1x   2x 1x   2x 2x   1x 1x   3x         1x 1x             1x 1x       1x       1x 1x 1x 1x       1x 1x       11x 11x                                                                                          
<template>
  <div class="ribbons-panel">
    <div class="d-flex justify-content-between align-items-center mb-3">
      <p class="text-muted mb-0 me-3">{{ t('ribbon.description') }}</p>
      <button class="btn btn-success" @click="openCreate">
        <font-awesome-icon icon="fa-solid fa-plus" class="me-2" />
        {{ t('ribbon.createNew') }}
      </button>
    </div>
 
    <ErrorAlert
      v-if="ribbonsStore.error"
      :title="t('common.error')"
      :message="ribbonsStore.error"
    />
    <LoadingSpinner v-else-if="ribbonsStore.loading" />
    <EmptyState v-else-if="ribbonsStore.ribbons.length === 0" variant="info">
      {{ t('ribbon.noRibbons') }}
    </EmptyState>
 
    <div v-else class="ribbon-cards">
      <div v-for="ribbon in ribbonsStore.ribbons" :key="ribbon.id" class="ribbon-card">
        <div
          class="ribbon-card-preview"
          :style="{
            backgroundColor: ribbon.color,
            color: getContrastTextColor(ribbon.color)
          }"
        >
          {{ getTranslation(ribbon.message, uiLanguage) }}
        </div>
        <div class="ribbon-card-actions">
          <div class="form-check form-switch mb-0">
            <input
              :id="`ribbon-toggle-${ribbon.id}`"
              class="form-check-input"
              type="checkbox"
              role="switch"
              :checked="ribbon.active"
              @change="toggleActive(ribbon)"
            />
            <label
              class="form-check-label small ribbon-toggle-label"
              :for="`ribbon-toggle-${ribbon.id}`"
            >
              {{ ribbon.active ? t('ribbon.active') : t('ribbon.inactive') }}
            </label>
          </div>
          <button
            class="btn btn-sm btn-secondary"
            :title="t('common.edit')"
            @click="editRibbon(ribbon)"
          >
            <font-awesome-icon icon="fa-solid fa-pen" />
          </button>
          <button
            class="btn btn-sm btn-danger"
            :title="t('common.delete')"
            @click="confirmDeleteRibbon(ribbon)"
          >
            <font-awesome-icon icon="fa-solid fa-trash" />
          </button>
        </div>
      </div>
    </div>
 
    <RibbonFormSidebar
      :is-open="showFormSidebar"
      :mode="editingRibbon ? 'edit' : 'create'"
      :form-data="formData"
      :loading="formLoading"
      :error="formError"
      @update:form-data="formData = $event"
      @close="closeForm"
      @submit="saveRibbon"
    />
 
    <ConfirmModal
      :is-open="deleteTarget.open"
      :title="t('ribbon.deleteRibbon')"
      :message="deleteTarget.message"
      :confirm-label="t('common.delete')"
      variant="danger"
      @confirm="doDeleteRibbon"
      @cancel="deleteTarget.open = false"
    />
  </div>
</template>
 
<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import ErrorAlert from '@/components/ErrorAlert.vue'
import LoadingSpinner from '@/components/LoadingSpinner.vue'
import EmptyState from '@/components/EmptyState.vue'
import ConfirmModal from '@/components/ConfirmModal.vue'
import RibbonFormSidebar from '@/components/RibbonFormSidebar.vue'
import { useRibbonsStore, type Ribbon, type RibbonInput } from '@/stores/ribbons'
import { useUILanguage } from '@/composables/useUILanguage'
import { useLanguage } from '@/composables/useLanguage'
import { useToastStore } from '@/stores/toast'
import { getContrastTextColor } from '@/utils/color'
import { getErrorMessage } from '@/utils/errors'
 
const ribbonsStore = useRibbonsStore()
const toastStore = useToastStore()
const { t, uiLanguage } = useUILanguage()
const { getTranslation } = useLanguage()
 
const showFormSidebar = ref(false)
const editingRibbon = ref<Ribbon | null>(null)
const formLoading = ref(false)
const formError = ref('')
const formData = ref<RibbonInput>(emptyFormData())
const deleteTarget = reactive<{ open: boolean; ribbon: Ribbon | null; message: string }>({
  open: false,
  ribbon: null,
  message: ''
})
 
function emptyFormData(): RibbonInput {
  return { message: { en: '', fr: '' }, color: '#615581', active: true }
}
 
function openCreate() {
  editingRibbon.value = null
  formData.value = emptyFormData()
  formError.value = ''
  showFormSidebar.value = true
}
 
function editRibbon(ribbon: Ribbon) {
  editingRibbon.value = ribbon
  formData.value = { message: ribbon.message, color: ribbon.color, active: ribbon.active }
  formError.value = ''
  showFormSidebar.value = true
}
 
function closeForm() {
  showFormSidebar.value = false
  editingRibbon.value = null
}
 
async function saveRibbon() {
  formLoading.value = true
  formError.value = ''
  try {
    if (editingRibbon.value) {
      await ribbonsStore.updateRibbon(editingRibbon.value.id, formData.value)
      toastStore.success(t('ribbon.updateSuccess'))
    } else {
      await ribbonsStore.createRibbon(formData.value)
      toastStore.success(t('ribbon.createSuccess'))
    }
    showFormSidebar.value = false
    editingRibbon.value = null
  } catch (err) {
    formError.value = getErrorMessage(err, 'Failed to save ribbon')
    toastStore.error(editingRibbon.value ? t('ribbon.updateError') : t('ribbon.createError'))
  } finally {
    formLoading.value = false
  }
}
 
async function toggleActive(ribbon: Ribbon) {
  try {
    await ribbonsStore.updateRibbon(ribbon.id, { active: !ribbon.active })
  } catch (err) {
    toastStore.error(getErrorMessage(err, 'Failed to update ribbon'))
  }
}
 
function confirmDeleteRibbon(ribbon: Ribbon) {
  deleteTarget.ribbon = ribbon
  deleteTarget.message = t('ribbon.deleteConfirmMessage').replace(
    '{name}',
    getTranslation(ribbon.message, uiLanguage.value)
  )
  deleteTarget.open = true
}
 
async function doDeleteRibbon() {
  Iif (!deleteTarget.ribbon) return
  try {
    await ribbonsStore.deleteRibbon(deleteTarget.ribbon.id)
    toastStore.success(t('ribbon.deleteSuccess'))
  } catch (err) {
    toastStore.error(getErrorMessage(err, 'Failed to delete ribbon'))
  } finally {
    deleteTarget.open = false
    deleteTarget.ribbon = null
  }
}
 
onMounted(() => {
  ribbonsStore.fetchRibbons()
})
</script>
 
<style scoped>
.ribbon-cards {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
 
.ribbon-card {
  display: flex;
  align-items: center;
  gap: 1rem;
  border: 1px solid var(--bw-border);
  border-radius: 0.375rem;
  padding: 0.75rem;
  background: var(--bw-surface);
}
 
.ribbon-card-preview {
  flex: 1;
  padding: 0.5rem 1rem;
  border-radius: 0.25rem;
  font-weight: 500;
  word-break: break-word;
  white-space: pre-line;
}
 
.ribbon-card-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-shrink: 0;
}
 
/* Fixed width so the switch doesn't shift the buttons next to it when the
   label text changes length between "Active"/"Inactive" (or the French
   "Actif"/"Inactif"). */
.ribbon-toggle-label {
  display: inline-block;
  min-width: 4rem;
}
</style>