All files / src/components ApiKeyFormSidebar.vue

75% Statements 42/56
80.95% Branches 34/42
50% Functions 8/16
75.47% Lines 40/53

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  19x                                         2x 2x                                 2x       2x 2x       2x             1x           2x                         2x       2x 2x               2x       2x 2x                                   7x                   17x   17x             17x             17x       17x 19x 3x 3x       17x 7x 7x 7x     17x 8x         1x 1x   1x     1x     1x                               1x 1x         1x 1x         17x                
<template>
  <BaseFormSidebar
    :is-open="isOpen"
    :title="t('apiKeys.createNew')"
    icon="fa-solid fa-key"
    :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
          @input="
            $emit('update:formData', {
              ...formData,
              name: ($event.target as HTMLInputElement).value
            })
          "
        />
        <div class="form-text">{{ t('apiKeys.nameHelp') }}</div>
      </div>
 
      <!-- Permissions -->
      <div class="mb-3">
        <label class="form-label">
          {{ t('apiKeys.permissions') }}
          <span class="text-danger">*</span>
        </label>
        <div class="form-check">
          <input
            id="perm-entities-read"
            type="checkbox"
            class="form-check-input"
            value="entities:read"
            :checked="formData.permissions.includes('entities:read')"
            @change="togglePermission('entities:read')"
          />
          <label class="form-check-label" for="perm-entities-read">
            <code>entities:read</code> - {{ t('apiKeys.permissionEntitiesRead') }}
          </label>
        </div>
        <div class="form-check">
          <input
            id="perm-items-read"
            type="checkbox"
            class="form-check-input"
            value="items:read"
            :checked="formData.permissions.includes('items:read')"
            @change="togglePermission('items:read')"
          />
          <label class="form-check-label" for="perm-items-read">
            <code>items:read</code> - {{ t('apiKeys.permissionItemsRead') }}
          </label>
        </div>
        <div class="form-text">{{ t('apiKeys.permissionsHelp') }}</div>
      </div>
 
      <!-- Allowed Entity IDs -->
      <div class="mb-3">
        <label class="form-label">{{ t('apiKeys.allowedEntities') }}</label>
        <textarea
          v-model="allowedEntitiesText"
          class="form-control font-monospace"
          rows="3"
          :placeholder="t('apiKeys.allowedEntitiesPlaceholder')"
          @input="updateAllowedEntities"
        ></textarea>
        <div class="form-text">{{ t('apiKeys.allowedEntitiesHelp') }}</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"
          @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 { ref, computed } from 'vue'
import BaseFormSidebar from '@/components/BaseFormSidebar.vue'
import { useUILanguage } from '@/composables/useUILanguage'
import type { CreateApiKeyRequest } from '@/stores/apiKeys'
 
const { t } = useUILanguage()
 
const props = defineProps<{
  isOpen: boolean
  formData: CreateApiKeyRequest
  loading: boolean
  error: string
}>()
 
const emit = defineEmits<{
  'update:formData': [value: CreateApiKeyRequest]
  close: []
  submit: []
}>()
 
// '*' means all entities — shown as default placeholder, empty field also means all entities
const allowedEntitiesText = ref(
  props.formData.allowedEntityIds?.filter((id) => id !== '*').join('\n') || ''
)
 
const isValid = computed(() => {
  if (!props.formData.name.trim() || props.formData.permissions.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 togglePermission(permission: string) {
  const permissions = [...props.formData.permissions]
  const index = permissions.indexOf(permission)
 
  Iif (index > -1) {
    permissions.splice(index, 1)
  } else {
    permissions.push(permission)
  }
 
  emit('update:formData', { ...props.formData, permissions })
}
 
function updateAllowedEntities(event: Event) {
  const target = event.target as HTMLTextAreaElement
  allowedEntitiesText.value = target.value
 
  const entityIds = target.value
    .split('\n')
    .map((line) => line.trim())
    .filter((line) => line.length > 0)
 
  emit('update:formData', { ...props.formData, allowedEntityIds: entityIds })
}
 
function handleClose() {
  Eif (!props.loading) {
    emit('close')
  }
}
 
function handleSubmit() {
  Eif (isValid.value && !props.loading) {
    emit('submit')
  }
}
 
// Expose for testing
defineExpose({
  isValid,
  togglePermission,
  updateAllowedEntities,
  handleSubmit,
  handleClose
})
</script>