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 | 22x 9x 6x 1x 18x 1x 6x 6x 6x 18x 2x 6x 2x 6x 6x 2x 10x 10x 10x 19x 10x 2x 6x 4x 10x 9x 9x 10x 8x 8x 9x 5x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x | <template>
<div class="transition-email-actions" data-testid="transition-email-actions">
<div
v-if="templates.length === 0"
class="form-text mb-2"
data-testid="email-actions-no-template"
>
{{ t('workflow.emailActions.noTemplate') }}
</div>
<div
v-for="(action, index) in actions"
:key="action.id"
class="action-card mb-2"
data-testid="email-action"
>
<div class="d-flex align-items-center gap-2 mb-2">
<font-awesome-icon icon="fa-solid fa-envelope" class="text-muted" />
<select
:value="action.templateId"
class="form-select form-select-sm flex-grow-1"
:aria-label="t('workflow.emailActions.template')"
data-testid="email-action-template"
name="email-action-template"
@change="update(index, { templateId: ($event.target as HTMLSelectElement).value })"
>
<option value="" disabled>{{ t('workflow.emailActions.chooseTemplate') }}</option>
<option v-for="template in templates" :key="template.id" :value="template.id">
{{ translate(template.name) || template.id }}
</option>
</select>
<button
type="button"
class="btn btn-sm btn-outline-danger"
:title="t('workflow.emailActions.remove')"
data-testid="email-action-remove"
@click="remove(index)"
>
<font-awesome-icon icon="fa-solid fa-trash" />
</button>
</div>
<div class="mb-2">
<div class="small fw-semibold mb-1">{{ t('workflow.emailActions.recipients') }}</div>
<div class="d-flex flex-wrap gap-3">
<div v-for="field in userFields" :key="field.fieldId" class="form-check">
<input
:id="`${action.id}-${field.fieldId}`"
class="form-check-input"
type="checkbox"
:checked="(action.recipients.fields ?? []).includes(field.fieldId)"
data-testid="email-action-recipient-field"
@change="
toggleField(index, field.fieldId, ($event.target as HTMLInputElement).checked)
"
/>
<label class="form-check-label small" :for="`${action.id}-${field.fieldId}`">
{{ field.label }}
</label>
</div>
<div class="form-check">
<input
:id="`${action.id}-creator`"
class="form-check-input"
type="checkbox"
:checked="action.recipients.creator === true"
data-testid="email-action-recipient-creator"
@change="toggleCreator(index, ($event.target as HTMLInputElement).checked)"
/>
<label class="form-check-label small" :for="`${action.id}-creator`">
{{ t('workflow.emailActions.creator') }}
</label>
</div>
</div>
<div class="form-text">{{ t('workflow.emailActions.recipientsHelp') }}</div>
</div>
<div class="small fw-semibold mb-1">{{ t('workflow.emailActions.onlyWhen') }}</div>
<ConditionBuilder
:model-value="action.when"
:fields="fields"
:translate="translate"
:linked-entities="linkedEntities"
@update:model-value="setWhen(index, $event)"
/>
<div v-if="!errors[index]" class="form-text" data-testid="email-action-summary">
{{ summaries[index] }}
</div>
<div v-if="errors[index]" class="invalid-feedback d-block" data-testid="email-action-error">
{{ t(`workflow.emailActions.errors.${errors[index]}`) }}
</div>
</div>
<button
type="button"
class="btn btn-sm btn-outline-primary"
:disabled="templates.length === 0 || actions.length >= MAX_ACTIONS_PER_TRANSITION"
data-testid="email-action-add"
@click="add"
>
<font-awesome-icon icon="fa-solid fa-plus" class="me-1" />
{{ t('workflow.emailActions.add') }}
</button>
</div>
</template>
<script setup lang="ts">
// @implements UC-WORKFLOW-011.1
import { computed } from 'vue'
import { useUILanguage } from '@/composables/useUILanguage'
import ConditionBuilder from './ConditionBuilder.vue'
import type {
EmailTemplate,
WorkflowCondition,
WorkflowTransitionAction
} from '@/schemas/entity-schema'
import type { ConditionField, LinkedEntities } from '@/utils/workflowConditions'
import {
describeEmailAction,
MAX_ACTIONS_PER_TRANSITION,
newActionId,
validateTransitionAction
} from '@/utils/emailTemplates'
const props = withDefaults(
defineProps<{
modelValue?: WorkflowTransitionAction[]
templates?: EmailTemplate[]
/** the entity's fields: recipients and conditions */
fields?: ConditionField[]
linkedEntities?: LinkedEntities
translate: (value: string | Record<string, string> | undefined) => string
}>(),
{ modelValue: () => [], templates: () => [], fields: () => [], linkedEntities: undefined }
)
const emit = defineEmits<{ (e: 'update:modelValue', value: WorkflowTransitionAction[]): void }>()
const { t } = useUILanguage()
const actions = computed(() => props.modelValue)
const userFields = computed(() =>
props.fields
.filter((field) => field.type === 'user' && field.fieldId)
.map((field) => ({
fieldId: field.fieldId!,
label: props.translate(field.name) || field.fieldId!
}))
)
const errors = computed(() =>
actions.value.map((action) =>
validateTransitionAction(action, {
fields: props.fields,
templates: props.templates,
linkedEntities: props.linkedEntities
})
)
)
const summaries = computed(() =>
actions.value.map((action) =>
describeEmailAction(action, props.templates, {
t,
translate: props.translate,
fields: props.fields,
linkedEntities: props.linkedEntities
})
)
)
function emitActions(next: WorkflowTransitionAction[]) {
emit('update:modelValue', next)
}
function update(index: number, patch: Partial<WorkflowTransitionAction>) {
emitActions(actions.value.map((action, i) => (i === index ? { ...action, ...patch } : action)))
}
function add() {
emitActions([
...actions.value,
{
id: newActionId(),
type: 'email',
templateId: props.templates[0]?.id ?? '',
recipients: { creator: true }
}
])
}
function remove(index: number) {
emitActions(actions.value.filter((_, i) => i !== index))
}
function toggleField(index: number, fieldId: string, checked: boolean) {
const action = actions.value[index]!
const current = action.recipients.fields ?? []
const fields = checked ? [...current, fieldId] : current.filter((id) => id !== fieldId)
update(index, {
recipients: { ...action.recipients, fields: fields.length ? fields : undefined }
})
}
function toggleCreator(index: number, checked: boolean) {
const action = actions.value[index]!
update(index, { recipients: { ...action.recipients, creator: checked || undefined } })
}
// No condition (an empty builder) = always sent
function setWhen(index: number, when: WorkflowCondition | undefined) {
const { when: _previous, ...rest } = actions.value[index]!
emitActions(actions.value.map((a, i) => (i === index ? (when ? { ...rest, when } : rest) : a)))
}
</script>
<style scoped>
.action-card {
border: 1px solid var(--bw-border);
border-radius: 6px;
padding: 10px 12px;
background: var(--bw-surface);
}
</style>
|