All files / src/components WorkflowStatePanel.vue

93.05% Statements 67/72
90.74% Branches 49/54
78.26% Functions 18/23
92.42% Lines 61/66

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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283  109x               1x         3x 3x   64x           1x   3x             3x 3x                   3x 3x                     3x   3x 1x           3x           3x 3x 3x           3x   8x 3x 3x                                     39x                       105x     105x                               105x               105x                                 105x   105x 109x   109x 84x 84x       25x   25x 25x 25x 50x 50x     25x       6x 6x           109x                       3x 2x       4x 4x               1x       7x   6x 6x 6x 12x 12x     6x 3x 1x       2x 2x           6x               7x                                                                    
<template>
  <BaseFormSidebar
    :is-open="isOpen"
    :title="t('workflow.editState')"
    icon="fa-solid fa-circle-dot"
    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>
 
      <!-- 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 form-control-sm"
          :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 form-control-sm"
          :placeholder="t('workflow.description')"
        />
      </div>
 
      <!-- Form selector -->
      <div v-if="forms && forms.length > 0" class="mb-3">
        <label class="form-label fw-semibold">{{ t('workflow.form') }}</label>
        <select v-model="form.formId" class="form-select form-select-sm">
          <option value="">{{ t('workflow.noForm') }}</option>
          <option v-for="f in forms" :key="f.id" :value="f.id">
            {{
              f.name[currentLanguage] || f.name[defaultLanguage] || Object.values(f.name)[0] || f.id
            }}
          </option>
        </select>
        <div class="form-text">{{ t('workflow.formHelp') }}</div>
      </div>
 
      <hr />
 
      <!-- Actions section -->
      <div class="mb-3">
        <div class="d-flex align-items-center justify-content-between mb-2">
          <label class="form-label fw-semibold mb-0">{{ t('workflow.actions') }}</label>
          <button type="button" class="btn btn-outline-primary btn-xs" @click="addAction">
            <font-awesome-icon icon="fa-solid fa-plus" class="me-1" />
            {{ t('workflow.addAction') }}
          </button>
        </div>
        <p class="form-text mt-0 mb-2">{{ t('workflow.actionsHelp') }}</p>
 
        <div v-for="(action, idx) in form.actions" :key="idx" class="action-card mb-2">
          <div class="d-flex align-items-start gap-2">
            <div class="flex-grow-1">
              <input
                v-model="action.labels[editingLang]"
                type="text"
                class="form-control form-control-sm"
                :placeholder="t('workflow.actionLabel')"
              />
            </div>
            <button
              type="button"
              class="btn btn-outline-danger btn-xs mt-1"
              :title="t('workflow.deleteAction')"
              @click="removeAction(idx)"
            >
              <font-awesome-icon icon="fa-solid fa-trash" />
            </button>
          </div>
        </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 { WorkflowState, WorkflowStateAction, FormDefinition } 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
  state?: WorkflowState | null
  enabledLanguages?: string[]
  defaultLanguage?: string
  currentLanguage?: string
  forms?: FormDefinition[]
}
 
interface Emits {
  (e: 'close'): void
  (e: 'save', state: WorkflowState): void
}
 
const props = withDefaults(defineProps<Props>(), {
  state: null,
  enabledLanguages: () => ['en', 'fr'],
  defaultLanguage: 'en',
  currentLanguage: 'en',
  forms: () => []
})
 
const emit = defineEmits<Emits>()
 
// ---- local form model ----
 
interface ActionForm {
  id: string
  labels: Record<string, string>
}
 
interface StateForm {
  id: string
  labels: Record<string, string>
  descriptions: Record<string, string>
  actions: ActionForm[]
  formId: string
}
 
const form = ref<StateForm | null>(null)
 
watch(
  () => [props.state, props.isOpen] as const,
  ([state, isOpen]) => {
    if (!isOpen || !state) {
      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] = typeof state.label === 'string' ? state.label : (state.label[lang] ?? '')
      descriptions[lang] = state.description?.[lang] ?? ''
    }
 
    const actions: ActionForm[] = (state.actions ?? []).map((a) => ({
      id: a.id,
      labels: props.enabledLanguages.reduce(
        (acc, lang) => {
          acc[lang] = a.label[lang] ?? ''
          return acc
        },
        {} as Record<string, string>
      )
    }))
 
    form.value = {
      id: state.id,
      labels,
      descriptions,
      actions,
      formId: state.formId ?? ''
    }
  },
  { immediate: true }
)
 
function addAction() {
  if (!form.value) return
  form.value.actions.push({
    id: `action_${Date.now()}`,
    labels: props.enabledLanguages.reduce(
      (acc, lang) => {
        acc[lang] = ''
        return acc
      },
      {} as Record<string, string>
    )
  })
}
 
function removeAction(idx: number) {
  form.value?.actions.splice(idx, 1)
}
 
function handleSubmit() {
  if (!form.value || !props.state) 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 actions: WorkflowStateAction[] = form.value.actions
    .filter((a) => Object.values(a.labels).some((v) => v.trim()))
    .map((a) => ({
      id: a.id,
      label: props.enabledLanguages.reduce(
        (acc, lang) => {
          acc[lang] = a.labels[lang] ?? ''
          return acc
        },
        {} as Record<string, string>
      )
    }))
 
  const updated: WorkflowState = {
    ...props.state,
    label,
    description: Object.keys(description).length ? description : undefined,
    actions: actions.length ? actions : undefined,
    formId: form.value.formId || undefined
  }
 
  emit('save', 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;
}
 
.form-label-sm {
  font-size: 12px;
  color: #6c757d;
}
</style>