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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | 77x 4x 4x 4x 4x 34x 1x 4x 2x 4x 4x 4x 2x 4x 1x 1x 4x 2x 1x 4x 2x 4x 79x 4x 4x 4x 1x 4x 4x 4x 4x 54x 54x 54x 67x 54x 54x 68x 54x 54x 54x 78x 78x 68x 54x 15x 46x 38x 59x 77x 54x 21x 24x 12x 4x 4x 34x 29x 28x 5x 6x 1x 1x 1x 2x 2x 3x 2x 2x 2x 2x 3x 3x 3x 4x 54x 54x 54x 54x 54x 3x 3x 3x 2x 3x 1x 3x 3x 4x 10x 4x 4x 2x 54x 11x 11x 2x 2x 2x 4x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 1x 2x | <template>
<div class="email-templates-editor" data-testid="email-templates-editor">
<div class="row g-3">
<!-- Left: template list -->
<div class="col-md-3">
<div class="mb-2">
<button
type="button"
class="btn btn-sm btn-success p-2 w-100"
:disabled="templates.length >= MAX_EMAIL_TEMPLATES"
data-testid="email-template-add"
@click="addTemplate"
>
<font-awesome-icon icon="fa-solid fa-plus" class="me-1" />
{{ t('emailTemplates.newTemplate') }}
</button>
</div>
<div v-if="templates.length === 0" class="empty-list text-center text-muted py-4">
<font-awesome-icon icon="fa-solid fa-envelope" size="2x" class="mb-2 opacity-50" />
<p class="small mb-0">{{ t('emailTemplates.noTemplates') }}</p>
</div>
<div
v-for="template in templates"
:key="template.id"
class="template-list-item"
:class="{ selected: selectedId === template.id, invalid: !!errors[template.id] }"
data-testid="email-template-item"
@click="selectedId = template.id"
>
<div class="flex-grow-1 text-truncate small">{{ templateName(template) }}</div>
<button
type="button"
class="btn btn-sm btn-danger btn-delete-template"
:disabled="usedTemplateIds.has(template.id)"
:title="
usedTemplateIds.has(template.id)
? t('emailTemplates.inUse')
: t('emailTemplates.deleteTemplate')
"
data-testid="email-template-delete"
@click.stop="removeTemplate(template.id)"
>
<font-awesome-icon icon="fa-solid fa-trash" />
</button>
</div>
</div>
<!-- Right: editor -->
<div class="col-md-9">
<div v-if="!selected" class="text-center text-muted py-5">
<font-awesome-icon icon="fa-solid fa-hand-pointer" size="2x" class="mb-2 opacity-50" />
<p class="small">{{ t('emailTemplates.selectToEdit') }}</p>
</div>
<div v-else>
<div class="mb-2">
<label class="form-label small fw-bold mb-1" for="template-name">
{{ t('emailTemplates.name') }}
</label>
<input
id="template-name"
autocomplete="off"
:value="localizedName(selected)"
type="text"
class="form-control form-control-sm"
data-testid="email-template-name"
@input="setName(($event.target as HTMLInputElement).value)"
/>
</div>
<div class="mb-2">
<label class="form-label small fw-bold mb-1" for="template-subject">
{{ t('emailTemplates.subject') }}
</label>
<input
id="template-subject"
ref="subjectInput"
autocomplete="off"
:value="displayText(selected.subject[currentLanguage])"
type="text"
class="form-control form-control-sm"
:maxlength="MAX_SUBJECT_LENGTH"
data-testid="email-template-subject"
@focus="lastFocused = 'subject'"
@input="setText('subject', ($event.target as HTMLInputElement).value)"
/>
</div>
<div class="mb-2">
<label class="form-label small fw-bold mb-1" for="template-body">
{{ t('emailTemplates.body') }}
</label>
<textarea
id="template-body"
ref="bodyInput"
autocomplete="off"
:value="displayText(selected.body[currentLanguage])"
class="form-control form-control-sm"
rows="10"
:maxlength="MAX_BODY_LENGTH"
data-testid="email-template-body"
@focus="lastFocused = 'body'"
@input="setText('body', ($event.target as HTMLTextAreaElement).value)"
/>
</div>
<div class="mb-2 d-flex align-items-center gap-2">
<select
class="form-select form-select-sm w-auto"
data-testid="email-template-insert"
:value="''"
name="email-template-insert"
@change="insertField($event)"
>
<option value="">{{ t('emailTemplates.insertField') }}</option>
<option v-for="field in insertableFields" :key="field.fieldId" :value="field.fieldId">
{{ field.label }}
</option>
</select>
<span class="form-text m-0">{{ t('emailTemplates.help') }}</span>
</div>
<div
v-if="errors[selected.id]"
class="invalid-feedback d-block"
data-testid="email-template-error"
>
{{ errorText(errors[selected.id]!) }}
</div>
<!-- Preview on an existing item of the entity -->
<div v-if="entityId" class="mt-3" data-testid="email-template-preview">
<div class="d-flex align-items-center gap-2 mb-2">
<span class="small fw-bold">{{ t('emailTemplates.preview') }}</span>
<select
v-if="previewItems.length > 0"
v-model="previewItemId"
class="form-select form-select-sm w-auto"
data-testid="email-template-preview-item"
name="email-template-preview-item"
>
<option v-for="item in previewItems" :key="item.id" :value="item.id">
{{ itemLabel(item) }}
</option>
</select>
<button
v-else
type="button"
class="btn btn-sm btn-outline-secondary"
:disabled="previewLoading"
data-testid="email-template-preview-load"
@click="loadPreviewItems"
>
<font-awesome-icon icon="fa-solid fa-eye" class="me-1" />
{{ t('emailTemplates.previewWithItem') }}
</button>
</div>
<div
v-if="previewLoaded && previewItems.length === 0"
class="form-text"
data-testid="email-template-preview-empty"
>
{{ t('emailTemplates.previewNoItems') }}
</div>
<div v-if="preview" class="email-preview" data-testid="email-template-preview-text">
<div class="fw-semibold mb-2">{{ preview.subject }}</div>
<div class="email-preview-body">{{ preview.body }}</div>
<div class="text-muted small mt-2">{{ t('emailTemplates.previewLink') }}</div>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
// @implements UC-WORKFLOW-010.1
import { computed, ref, nextTick } from 'vue'
import api from '@/services/api'
import { useUILanguage } from '@/composables/useUILanguage'
import { useFlowMemberReference } from '@/composables/useFlowMemberReference'
import type { EmailTemplate } from '@/schemas/entity-schema'
import type { EntityField } from '@/stores/entities'
import { formulaForDisplay, formulaForStorage } from '@/utils/conditionModel'
import {
INSERTABLE_TYPES,
MAX_BODY_LENGTH,
MAX_EMAIL_TEMPLATES,
MAX_SUBJECT_LENGTH,
newTemplateId,
renderEmailPreview,
validateEmailTemplate
} from '@/utils/emailTemplates'
const props = withDefaults(
defineProps<{
modelValue: EmailTemplate[]
entityFields: EntityField[]
enabledLanguages: string[]
defaultLanguage: string
currentLanguage: string
/** templates an Email action uses: they can't be deleted */
usedTemplateIds?: Set<string>
/** the saved entity, whose items a template can be previewed on */
entityId?: string
}>(),
{ usedTemplateIds: () => new Set(), entityId: undefined }
)
const emit = defineEmits<{ (e: 'update:modelValue', value: EmailTemplate[]): void }>()
const { t, uiLanguage } = useUILanguage()
const templates = computed(() => props.modelValue ?? [])
const selectedId = ref<string | null>(templates.value[0]?.id ?? null)
const selected = computed(() =>
templates.value.find((template) => template.id === selectedId.value)
)
const subjectInput = ref<HTMLInputElement | null>(null)
const bodyInput = ref<HTMLTextAreaElement | null>(null)
const lastFocused = ref<'subject' | 'body'>('body')
function translate(value: string | Record<string, string> | undefined): string {
Iif (!value) return ''
if (typeof value === 'string') return value
return value[uiLanguage.value] || value[props.defaultLanguage] || Object.values(value)[0] || ''
}
// Fields a template can insert, named as the user reads them
const insertableFields = computed(() =>
props.entityFields
.filter((field) => field.fieldId && INSERTABLE_TYPES.includes(field.type ?? ''))
.map((field) => ({
fieldId: field.fieldId!,
label: translate(field.name) || field.fieldId!
}))
)
// Placeholders are stored as `{fieldId}` and shown as `{Field name}`
function displayText(text: string | undefined): string {
return formulaForDisplay(
text ?? '',
(id) => insertableFields.value.find((f) => f.fieldId === id)?.label
)
}
const errors = computed(() =>
Object.fromEntries(
templates.value.map((template) => [
template.id,
validateEmailTemplate(template, props.entityFields)
])
)
)
function errorText(error: { code: string; fieldId?: string }): string {
const field = props.entityFields.find((f) => f.fieldId === error.fieldId)
const name = (field && translate(field.name)) || error.fieldId || ''
return t(`emailTemplates.errors.${error.code}`).replace('{field}', name)
}
function templateName(template: EmailTemplate): string {
return translate(template.name) || t('emailTemplates.unnamed')
}
function localizedName(template: EmailTemplate): string {
if (typeof template.name === 'string') return template.name
return template.name[props.currentLanguage] ?? ''
}
function update(id: string, patch: Partial<EmailTemplate>) {
emit(
'update:modelValue',
templates.value.map((template) => (template.id === id ? { ...template, ...patch } : template))
)
}
function addTemplate() {
const id = newTemplateId()
emit('update:modelValue', [
...templates.value,
{
id,
name: { [props.currentLanguage]: t('emailTemplates.unnamed') },
subject: {},
body: {}
}
])
selectedId.value = id
}
function removeTemplate(id: string) {
Iif (props.usedTemplateIds.has(id)) return
emit(
'update:modelValue',
templates.value.filter((template) => template.id !== id)
)
Eif (selectedId.value === id) selectedId.value = null
}
function setName(value: string) {
Iif (!selected.value) return
const name = typeof selected.value.name === 'string' ? {} : selected.value.name
update(selected.value.id, { name: { ...name, [props.currentLanguage]: value } })
}
function setText(part: 'subject' | 'body', value: string) {
Iif (!selected.value) return
const stored = formulaForStorage(value, insertableFields.value)
update(selected.value.id, {
[part]: { ...selected.value[part], [props.currentLanguage]: stored }
})
}
// ---- preview on an existing item ----
const PREVIEW_ITEMS_LIMIT = 20
const { memberLabelById } = useFlowMemberReference()
const previewItems = ref<Array<Record<string, any>>>([])
const previewItemId = ref<string | null>(null)
const previewLoading = ref(false)
const previewLoaded = ref(false)
// One page of the entity's items is enough to pick an example
async function loadPreviewItems() {
previewLoading.value = true
try {
const response = await api.get(`/v1/entities/${props.entityId}/items`, {
params: { limit: PREVIEW_ITEMS_LIMIT }
})
previewItems.value = response.data.items ?? []
previewItemId.value = previewItems.value[0]?.id ?? null
} catch (error) {
console.error('Failed to load items for the preview:', error)
} finally {
previewLoading.value = false
previewLoaded.value = true
}
}
// The item's first text value, as on its card
function itemLabel(item: Record<string, any>): string {
for (const field of props.entityFields) {
if (field.type !== 'text' && field.type !== 'textarea') continue
const label = translate(item[field.fieldId!])
if (label) return label
}
return item.id
}
const preview = computed(() => {
const item = previewItems.value.find((i) => i.id === previewItemId.value)
if (!item || !selected.value) return null
return renderEmailPreview(selected.value, props.currentLanguage, props.entityFields, item, {
t,
userName: (userId) => memberLabelById.value.get(userId) ?? userId
})
})
// Inserts `{Field name}` where the cursor was in the subject or the body
async function insertField(event: Event) {
const select = event.target as HTMLSelectElement
const field = insertableFields.value.find((f) => f.fieldId === select.value)
select.value = ''
if (!field || !selected.value) return
const part = lastFocused.value
const input = part === 'subject' ? subjectInput.value : bodyInput.value
const current = displayText(selected.value[part][props.currentLanguage])
const start = input?.selectionStart ?? current.length
const end = input?.selectionEnd ?? current.length
const placeholder = `{${field.label}}`
setText(part, current.slice(0, start) + placeholder + current.slice(end))
await nextTick()
input?.focus()
input?.setSelectionRange(start + placeholder.length, start + placeholder.length)
}
</script>
<style scoped>
.template-list-item {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 7px 10px;
border: 1px solid var(--bw-border);
border-radius: 6px;
margin-bottom: 5px;
cursor: pointer;
background: var(--bw-surface);
}
.template-list-item:hover {
background: color-mix(in srgb, var(--bw-surface) 84%, #615581 16%);
}
.template-list-item.selected {
background: color-mix(in srgb, var(--bw-surface) 78%, #615581 22%);
border-color: #615581;
border-width: 2px;
}
.template-list-item.invalid {
border-color: var(--bs-danger);
}
.btn-delete-template {
font-size: 13px;
display: flex;
padding: 3px;
}
.email-preview {
border: 1px solid var(--bw-border);
border-radius: 6px;
padding: 10px 12px;
background: var(--bw-surface);
}
.email-preview-body {
white-space: pre-wrap;
}
.empty-list {
border: 1px dashed var(--bw-border);
border-radius: 5px;
}
</style>
|