All files / src/components ApiKeyFormSidebar.vue

91.48% Statements 43/47
76.66% Branches 23/30
84.61% Functions 11/13
93.18% Lines 41/44

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  26x                                           2x 2x                                   2x       2x 2x       2x 18x           2x             2x       2x 2x                                     9x                       24x 24x   24x               24x           24x 26x 4x 4x       24x 9x 9x 9x     24x 10x         2x 2x   2x 1x   1x     2x       1x 1x         1x 1x         24x               24x 9x 24x                      
<template>
  <BaseFormSidebar
    :is-open="isOpen"
    :dirty="dirty"
    storage-key="api-key-form-sidebar"
    :title="t('apiKeys.createNew')"
    :loading="loading"
    :submit-disabled="!isValid"
    :submit-label="t('apiKeys.create')"
    submit-icon="fa-solid fa-key"
    submit-button-class="btn-success"
    :loading-label="t('common.creating')"
    @close="handleClose"
    @submit="handleSubmit"
  >
    <form @submit.prevent="handleSubmit">
      <!-- Error Message -->
      <div v-if="error" class="alert alert-danger">
        <font-awesome-icon icon="fa-solid fa-exclamation-triangle" class="me-2" />
        {{ error }}
      </div>
 
      <!-- Name -->
      <div class="mb-3">
        <label class="form-label">
          {{ t('apiKeys.name') }}
          <span class="text-danger">*</span>
        </label>
        <input
          :value="formData.name"
          type="text"
          class="form-control"
          :placeholder="t('apiKeys.namePlaceholder')"
          required
          name="api-key-name"
          @input="
            $emit('update:formData', {
              ...formData,
              name: ($event.target as HTMLInputElement).value
            })
          "
        />
        <div class="form-text">{{ t('apiKeys.nameHelp') }}</div>
      </div>
 
      <!-- Roles -->
      <div class="mb-3">
        <label class="form-label">
          {{ t('apiKeys.roles') }}
          <span class="text-danger">*</span>
        </label>
        <div class="roles-checkboxes">
          <div v-for="role in availableRoles" :key="role.roleId" class="form-check">
            <input
              :id="`apikey-role-${role.roleId}`"
              type="checkbox"
              class="form-check-input"
              :checked="formData.roleIds.includes(role.roleId)"
              @change="toggleRole(role.roleId)"
            />
            <label class="form-check-label" :for="`apikey-role-${role.roleId}`">
              {{ getTranslation(role.name) }}
            </label>
          </div>
        </div>
        <div class="form-text">{{ t('apiKeys.rolesHelp') }}</div>
      </div>
 
      <!-- Expiration Date -->
      <div class="mb-3">
        <label class="form-label">{{ t('apiKeys.expiresAt') }}</label>
        <input
          :value="formData.expiresAt"
          type="datetime-local"
          step="1"
          class="form-control"
          :class="{ 'is-invalid': expiresAtError }"
          :min="minExpiresAt"
          name="api-key-expires-at"
          @input="
            $emit('update:formData', {
              ...formData,
              expiresAt: ($event.target as HTMLInputElement).value || undefined
            })
          "
        />
        <div v-if="expiresAtError" class="invalid-feedback">{{ t('apiKeys.expiresAtPast') }}</div>
        <div v-else class="form-text">{{ t('apiKeys.expiresAtHelp') }}</div>
      </div>
    </form>
  </BaseFormSidebar>
</template>
 
<script setup lang="ts">
import { useUnsavedChanges } from '@/composables/useUnsavedChanges'
import { computed } from 'vue'
import BaseFormSidebar from '@/components/BaseFormSidebar.vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { useLanguage } from '@/composables/useLanguage'
import type { CreateApiKeyRequest } from '@/stores/apiKeys'
 
const { t } = useUILanguage()
const { getTranslation } = useLanguage()
 
const props = defineProps<{
  isOpen: boolean
  formData: CreateApiKeyRequest
  availableRoles: any[]
  loading: boolean
  error: string
}>()
 
const emit = defineEmits<{
  'update:formData': [value: CreateApiKeyRequest]
  close: []
  submit: []
}>()
 
const isValid = computed(() => {
  if (!props.formData.name.trim() || props.formData.roleIds.length === 0) return false
  Iif (expiresAtError.value) return false
  return true
})
 
// Minimum datetime-local value: current local time (truncated to seconds)
const minExpiresAt = computed(() => {
  const now = new Date()
  const offsetMs = now.getTimezoneOffset() * 60000
  return new Date(now.getTime() - offsetMs).toISOString().slice(0, 19)
})
 
const expiresAtError = computed(() => {
  Eif (!props.formData.expiresAt) return false
  return new Date(props.formData.expiresAt) <= new Date()
})
 
function toggleRole(roleId: string) {
  const roleIds = [...props.formData.roleIds]
  const index = roleIds.indexOf(roleId)
 
  if (index > -1) {
    roleIds.splice(index, 1)
  } else {
    roleIds.push(roleId)
  }
 
  emit('update:formData', { ...props.formData, roleIds })
}
 
function handleClose() {
  Eif (!props.loading) {
    emit('close')
  }
}
 
function handleSubmit() {
  Eif (isValid.value && !props.loading) {
    emit('submit')
  }
}
 
// Expose for testing
defineExpose({
  isValid,
  toggleRole,
  handleSubmit,
  handleClose
})
 
// Closing, or leaving the page, asks before dropping the changes (BaseFormSidebar)
const { dirty } = useUnsavedChanges(
  () => props.formData,
  () => props.isOpen
)
</script>
 
<style scoped>
.roles-checkboxes {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
</style>