All files / src/components PricingPanel.vue

91.3% Statements 42/46
91.66% Branches 22/24
100% Functions 12/12
91.11% Lines 41/45

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  25x 3x                 8x       3x       3x         12x     3x             3x 3x     1x             3x     3x         1x               1x                                                   6x 6x 6x   6x 6x     12x 5x         12x 12x       1x 1x       1x 1x 1x 1x       1x         1x 1x 1x 1x 1x       1x       6x 12x 12x       6x 6x                                      
<template>
  <div class="pricing-panel">
    <p class="text-muted mb-3">{{ t('pricing.description') }}</p>
 
    <ErrorAlert
      v-if="pricingStore.error"
      :title="t('common.error')"
      :message="pricingStore.error"
    />
    <LoadingSpinner v-else-if="pricingStore.loading" />
    <EmptyState v-else-if="pricingStore.pricing.length === 0" variant="info">
      {{ t('pricing.noEntries') }}
    </EmptyState>
 
    <div v-else class="table-responsive">
      <table class="table align-middle pricing-table">
        <thead>
          <tr>
            <th>{{ t('pricing.operation') }}</th>
            <th class="text-end">{{ t('pricing.value') }}</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="entry in pricingStore.pricing" :key="entry.id">
            <td>
              <div>{{ entry.description }}</div>
              <div class="text-muted small">
                {{ t('pricing.defaultValue') }}: {{ entry.defaultValue }} {{ entry.unit }}
                <span v-if="entry.isOverridden" class="badge text-bg-secondary ms-2">
                  {{ t('pricing.overridden') }}
                </span>
              </div>
            </td>
            <td class="text-end">
              <div class="input-group input-group-sm pricing-value-input">
                <input
                  :id="`pricing-value-${entry.id}`"
                  v-model.number="draftValues[entry.id]"
                  type="number"
                  min="0"
                  step="any"
                  class="form-control text-end"
                  :disabled="savingId === entry.id"
                />
                <span class="input-group-text">{{ entry.unit }}</span>
              </div>
            </td>
            <td class="text-end pricing-actions">
              <button
                class="btn btn-sm btn-secondary me-2"
                :disabled="!entry.isOverridden || savingId === entry.id"
                :title="t('pricing.resetToDefault')"
                @click="resetEntry(entry)"
              >
                <font-awesome-icon icon="fa-solid fa-rotate-left" />
              </button>
              <button
                class="btn btn-sm btn-success"
                :disabled="!isDirty(entry) || savingId === entry.id"
                :title="t('common.save')"
                @click="saveEntry(entry)"
              >
                <font-awesome-icon
                  :icon="savingId === entry.id ? 'fa-solid fa-spinner' : 'fa-solid fa-check'"
                  :spin="savingId === entry.id"
                />
              </button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-CREDITS-009.1
import { reactive, ref, onMounted, watch } from 'vue'
import ErrorAlert from '@/components/ErrorAlert.vue'
import LoadingSpinner from '@/components/LoadingSpinner.vue'
import EmptyState from '@/components/EmptyState.vue'
import { usePricingStore, type PricingEntry } from '@/stores/pricing'
import { useUILanguage } from '@/composables/useUILanguage'
import { useToastStore } from '@/stores/toast'
import { getErrorMessage } from '@/utils/errors'
 
const pricingStore = usePricingStore()
const toastStore = useToastStore()
const { t } = useUILanguage()
 
const draftValues = reactive<Record<string, number>>({})
const savingId = ref<string | null>(null)
 
function syncDraftValues(entries: PricingEntry[]) {
  for (const entry of entries) {
    draftValues[entry.id] = entry.value
  }
}
 
function isDirty(entry: PricingEntry): boolean {
  const draft = draftValues[entry.id]
  return typeof draft === 'number' && !Number.isNaN(draft) && draft !== entry.value
}
 
async function saveEntry(entry: PricingEntry) {
  const value = draftValues[entry.id]
  Iif (typeof value !== 'number' || Number.isNaN(value) || value < 0) {
    toastStore.error(t('pricing.invalidValue'))
    return
  }
  savingId.value = entry.id
  try {
    await pricingStore.updatePricing(entry.id, value)
    toastStore.success(t('pricing.updateSuccess'))
  } catch (err) {
    toastStore.error(getErrorMessage(err, t('pricing.updateError'), t))
  } finally {
    savingId.value = null
  }
}
 
async function resetEntry(entry: PricingEntry) {
  savingId.value = entry.id
  try {
    const updated = await pricingStore.updatePricing(entry.id, null)
    draftValues[entry.id] = updated.value
    toastStore.success(t('pricing.updateSuccess'))
  } catch (err) {
    toastStore.error(getErrorMessage(err, t('pricing.updateError'), t))
  } finally {
    savingId.value = null
  }
}
 
watch(
  () => pricingStore.pricing,
  (entries) => syncDraftValues(entries),
  { immediate: true }
)
 
onMounted(() => {
  pricingStore.fetchPricing()
})
</script>
 
<style scoped>
.pricing-table th {
  font-weight: 600;
  color: var(--bw-text-muted);
}
 
.pricing-value-input {
  max-width: 12rem;
  margin-left: auto;
}
 
.pricing-actions {
  white-space: nowrap;
}
</style>