All files / src/components StateTimerEditor.vue

100% Statements 49/49
92.85% Branches 52/56
100% Functions 18/18
100% Lines 45/45

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  32x 6x           4x   6x 6x 6x                       1x             1x   6x 6x   6x                 1x   6x                 1x           6x 6x                   2x             1x   6x 6x             6x                         30x                   30x   30x     30x     7x 4x     30x 32x 7x     30x 4x         30x 3x         30x 32x       4x 2x 1x       2x       5x 5x 5x 5x 5x                  
<template>
  <div class="state-timer-editor" data-testid="state-timer">
    <div class="d-flex flex-wrap gap-2 align-items-center">
      <select
        :value="mode"
        class="form-select form-select-sm w-auto"
        data-testid="timer-mode"
        name="timer-mode"
        @change="setMode(($event.target as HTMLSelectElement).value as Mode)"
      >
        <option value="none">{{ t('workflow.timer.none') }}</option>
        <option value="after">{{ t('workflow.timer.after') }}</option>
        <option value="at">{{ t('workflow.timer.at') }}</option>
      </select>
 
      <template v-if="mode === 'after'">
        <input
          :value="after.amount"
          type="number"
          min="1"
          step="1"
          class="form-control form-control-sm timer-count"
          data-testid="timer-after-count"
          name="timer-after-count"
          @input="emitAfter(Number(($event.target as HTMLInputElement).value), after.unit)"
        />
        <select
          :value="after.unit"
          class="form-select form-select-sm w-auto"
          data-testid="timer-after-unit"
          name="timer-after-unit"
          @change="emitAfter(after.amount, ($event.target as HTMLSelectElement).value as 'h' | 'd')"
        >
          <option value="d">{{ t('workflow.relative.days') }}</option>
          <option value="h">{{ t('workflow.relative.hours') }}</option>
        </select>
        <span class="small text-muted">{{ t('workflow.timer.sinceSave') }}</span>
      </template>
 
      <template v-else-if="mode === 'at'">
        <select
          :value="modelValue?.at"
          class="form-select form-select-sm w-auto"
          data-testid="timer-at-field"
          name="timer-at-field"
          @change="emitAt({ at: ($event.target as HTMLSelectElement).value })"
        >
          <option v-for="option in dateFields" :key="option.id" :value="option.id">
            {{ option.label }}
          </option>
        </select>
        <select
          :value="offset.amount < 0 ? '-' : '+'"
          class="form-select form-select-sm w-auto"
          data-testid="timer-offset-direction"
          name="timer-offset-direction"
          @change="
            emitAt({
              direction: ($event.target as HTMLSelectElement).value as '+' | '-'
            })
          "
        >
          <option value="+">{{ t('workflow.timer.plus') }}</option>
          <option value="-">{{ t('workflow.timer.minus') }}</option>
        </select>
        <input
          :value="Math.abs(offset.amount)"
          type="number"
          min="0"
          step="1"
          class="form-control form-control-sm timer-count"
          data-testid="timer-offset-count"
          name="timer-offset-count"
          @input="emitAt({ count: Number(($event.target as HTMLInputElement).value) })"
        />
        <select
          :value="offset.unit"
          class="form-select form-select-sm w-auto"
          data-testid="timer-offset-unit"
          name="timer-offset-unit"
          @change="emitAt({ unit: ($event.target as HTMLSelectElement).value as 'h' | 'd' })"
        >
          <option value="d">{{ t('workflow.relative.days') }}</option>
          <option value="h">{{ t('workflow.relative.hours') }}</option>
        </select>
      </template>
    </div>
    <div v-if="error" class="invalid-feedback d-block" data-testid="timer-error">
      {{ t(`workflow.conditionErrors.${error}`) }}
    </div>
    <div class="form-text">{{ t('workflow.timer.help') }}</div>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-WORKFLOW-009.1
import { computed } from 'vue'
import { useUILanguage } from '@/composables/useUILanguage'
import type { WorkflowTimer } from '@/schemas/entity-schema'
import { parseDuration, validateTimer, type ConditionField } from '@/utils/workflowConditions'
 
type Mode = 'none' | 'after' | 'at'
 
const props = withDefaults(
  defineProps<{
    modelValue?: WorkflowTimer
    /** the entity's fields: its date fields can be a timer's date */
    fields?: ConditionField[]
    translate: (value: string | Record<string, string> | undefined) => string
  }>(),
  { modelValue: undefined, fields: () => [] }
)
 
const emit = defineEmits<{ (e: 'update:modelValue', value: WorkflowTimer | undefined): void }>()
 
const { t } = useUILanguage()
 
// The item's own date fields, and its creation date, a timer can wait for
const dateFields = computed(() => [
  { id: 'createdAt', label: t('workflow.systemDates.createdAt') },
  ...props.fields
    .filter((field) => field.type === 'date' && field.fieldId)
    .map((field) => ({ id: field.fieldId!, label: props.translate(field.name) || field.fieldId! }))
])
 
const mode = computed<Mode>(() => {
  if (!props.modelValue) return 'none'
  return props.modelValue.at !== undefined ? 'at' : 'after'
})
 
const after = computed(() =>
  props.modelValue?.after
    ? parseDuration(props.modelValue.after)
    : { amount: 1, unit: 'd' as const }
)
 
const offset = computed(() =>
  props.modelValue?.offset
    ? parseDuration(props.modelValue.offset)
    : { amount: 0, unit: 'd' as const }
)
 
const error = computed(() =>
  props.modelValue ? validateTimer(props.modelValue, props.fields) : undefined
)
 
function setMode(next: Mode) {
  if (next === 'none') emit('update:modelValue', undefined)
  else if (next === 'after') emit('update:modelValue', { after: '1d' })
  else emit('update:modelValue', { at: dateFields.value[0]!.id })
}
 
function emitAfter(amount: number, unit: 'h' | 'd') {
  emit('update:modelValue', { after: `${Math.max(0, Math.round(amount) || 0)}${unit}` })
}
 
function emitAt(change: { at?: string; direction?: '+' | '-'; count?: number; unit?: 'h' | 'd' }) {
  const at = change.at ?? props.modelValue?.at ?? dateFields.value[0]!.id
  const direction = change.direction ?? (offset.value.amount < 0 ? '-' : '+')
  const count = Math.max(0, Math.round(change.count ?? Math.abs(offset.value.amount)) || 0)
  const unit = change.unit ?? offset.value.unit
  emit('update:modelValue', count === 0 ? { at } : { at, offset: `${direction}${count}${unit}` })
}
</script>
 
<style scoped>
.timer-count {
  width: 80px;
}
</style>