All files / src/components WorkflowTransitionPanel.vue

92.42% Statements 61/66
98.21% Branches 55/56
64.28% Functions 9/14
91.66% Lines 55/60

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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263  212x               1x         3x 3x   67x           1x   3x             3x 3x 3x               3x 3x                     3x 3x                   3x 3x                   3x 3x             3x       3x 3x                       3x       3x 3x             3x   41x                       108x     108x                                   108x               108x                           108x     108x 75x 71x 69x     108x 112x   112x 83x 83x       29x   29x 29x 29x 55x         55x     29x                           10x   9x 9x 9x 18x 18x     9x                 10x                                                          
<template>
  <BaseFormSidebar
    :is-open="isOpen"
    :title="t('workflow.editTransition')"
    icon="fa-solid fa-arrow-right-arrow-left"
    panel-width="1120px"
    :close-on-overlay-click="false"
    :submit-label="t('common.save')"
    submit-icon="fa-solid fa-check"
    @close="$emit('close')"
    @submit="handleSubmit"
  >
    <div v-if="form">
      <!-- Language selector -->
      <div class="mb-4 d-flex justify-content-end align-items-center gap-2">
        <label class="small text-muted mb-0">{{ t('language.editingLanguage') }}</label>
        <div class="btn-group btn-group-sm" role="group">
          <button
            v-for="lang in enabledLanguages"
            :key="lang"
            type="button"
            :class="['btn', editingLang === lang ? 'btn-primary' : 'btn-outline-secondary']"
            :title="getLanguageInfo(lang)?.nativeName"
            @click="editingLang = lang"
          >
            <span class="me-1">{{ getLanguageInfo(lang)?.flag }}</span>
            {{ lang.toUpperCase() }}
          </button>
        </div>
      </div>
 
      <!-- From / To (read-only) -->
      <div class="row g-2 mb-3">
        <div class="col-6">
          <label class="form-label fw-semibold">{{ t('workflow.fromState') }}</label>
          <input
            type="text"
            class="form-control form-control-sm"
            :value="getStateName(form.from)"
            disabled
          />
        </div>
        <div class="col-6">
          <label class="form-label fw-semibold">{{ t('workflow.toState') }}</label>
          <input
            type="text"
            class="form-control form-control-sm"
            :value="getStateName(form.to)"
            disabled
          />
        </div>
      </div>
 
      <!-- Label -->
      <div class="mb-3">
        <label class="form-label fw-semibold">{{ t('workflow.label') }}</label>
        <input
          v-model="form.labels[editingLang]"
          type="text"
          class="form-control"
          :placeholder="t('workflow.label')"
        />
      </div>
 
      <!-- Description -->
      <div class="mb-3">
        <label class="form-label fw-semibold">{{ t('workflow.description') }}</label>
        <input
          v-model="form.descriptions[editingLang]"
          type="text"
          class="form-control"
          :placeholder="t('workflow.description')"
        />
      </div>
 
      <!-- Required permission -->
      <div class="mb-3">
        <label class="form-label fw-semibold">{{ t('workflow.permission') }}</label>
        <input
          v-model="form.requiresPermission"
          type="text"
          class="form-control form-control-sm font-monospace"
          :placeholder="t('workflow.permissionOptional')"
        />
        <div class="form-text">{{ t('workflow.permissionHelp') }}</div>
      </div>
 
      <!-- Automatic toggle -->
      <div class="mb-3">
        <div class="form-check form-switch">
          <input
            id="auto-toggle"
            v-model="form.automatic"
            class="form-check-input"
            type="checkbox"
            role="switch"
          />
          <label class="form-check-label fw-semibold" for="auto-toggle">
            {{ t('workflow.isAutomatic') }}
          </label>
        </div>
        <div class="form-text">{{ t('workflow.isAutomaticHelp') }}</div>
      </div>
 
      <!-- Condition -->
      <div class="mb-4">
        <label class="form-label fw-semibold">{{ t('workflow.condition') }}</label>
        <input
          v-model="form.condition"
          type="text"
          class="form-control form-control-sm font-monospace"
          :placeholder="t('workflow.condition')"
        />
        <div class="form-text">{{ t('workflow.conditionHelp') }}</div>
      </div>
    </div>
  </BaseFormSidebar>
</template>
 
<script setup lang="ts">
// @implements UC-I18N-007.1, UC-I18N-010.1
import { ref, watch } from 'vue'
import BaseFormSidebar from './BaseFormSidebar.vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { getLanguageInfo } from '@/utils/languages'
import type { WorkflowTransition, WorkflowState } from '@/schemas/entity-schema'
 
const { t, uiLanguage } = useUILanguage()
 
// Active editing language (defaults to user's preferred UI language)
const editingLang = ref<string>(uiLanguage.value)
 
interface Props {
  isOpen: boolean
  transition?: WorkflowTransition | null
  /** transition index within the workflow (used to identify it on save) */
  transitionIndex?: number
  allStates: WorkflowState[]
  enabledLanguages?: string[]
  defaultLanguage?: string
  currentLanguage?: string
}
 
interface Emits {
  (e: 'close'): void
  (e: 'save', transitionIndex: number, transition: WorkflowTransition): void
}
 
const props = withDefaults(defineProps<Props>(), {
  transition: null,
  transitionIndex: -1,
  enabledLanguages: () => ['en', 'fr'],
  defaultLanguage: 'en',
  currentLanguage: 'en'
})
 
const emit = defineEmits<Emits>()
 
// ---- local form model ----
 
interface TransitionForm {
  from: string
  to: string
  labels: Record<string, string>
  descriptions: Record<string, string>
  requiresPermission: string
  automatic: boolean
  condition: string
}
 
const form = ref<TransitionForm | null>(null)
 
function getStateName(stateId: string): string {
  const state = props.allStates.find((s) => s.id === stateId)
  if (!state) return stateId
  if (typeof state.label === 'string') return state.label
  return state.label[uiLanguage.value] || state.label[props.defaultLanguage] || state.id
}
 
watch(
  () => [props.transition, props.isOpen] as const,
  ([transition, isOpen]) => {
    if (!isOpen || !transition) {
      form.value = null
      return
    }
 
    // Reset editing language to user's preferred UI language when panel opens
    editingLang.value = uiLanguage.value
 
    const labels: Record<string, string> = {}
    const descriptions: Record<string, string> = {}
    for (const lang of props.enabledLanguages) {
      labels[lang] = !transition.label
        ? ''
        : typeof transition.label === 'string'
          ? transition.label
          : (transition.label[lang] ?? '')
      descriptions[lang] = transition.description?.[lang] ?? ''
    }
 
    form.value = {
      from: transition.from,
      to: transition.to,
      labels,
      descriptions,
      requiresPermission: transition.requiresPermission ?? '',
      automatic: transition.automatic ?? false,
      condition: transition.condition ?? ''
    }
  },
  { immediate: true }
)
 
function handleSubmit() {
  if (!form.value || !props.transition) return
 
  const label: Record<string, string> = {}
  const description: Record<string, string> = {}
  for (const lang of props.enabledLanguages) {
    Eif (form.value.labels[lang]) label[lang] = form.value.labels[lang]
    if (form.value.descriptions[lang]) description[lang] = form.value.descriptions[lang]
  }
 
  const updated: WorkflowTransition = {
    ...props.transition,
    label,
    description: Object.keys(description).length ? description : undefined,
    requiresPermission: form.value.requiresPermission.trim() || undefined,
    automatic: form.value.automatic || undefined,
    condition: form.value.condition.trim() || undefined
  }
 
  emit('save', props.transitionIndex, updated)
}
</script>
 
<style scoped>
.action-card {
  border: 1px solid #dee2e6;
  border-radius: 6px;
  padding: 10px 12px;
  background: #f8f9fa;
}
 
.lang-badge {
  font-size: 11px;
  font-weight: 700;
  min-width: 38px;
  justify-content: center;
  background: #e9ecef;
  color: #495057;
  letter-spacing: 0.05em;
}
 
.btn-xs {
  padding: 2px 8px;
  font-size: 12px;
  line-height: 1.5;
  border-radius: 4px;
}
</style>