All files / components/composite EventForm.vue

100% Statements 125/125
100% Branches 24/24
100% Functions 8/8
100% Lines 125/125

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  1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                     1x                   1x         1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x         1x           1x   1x 1x 11x   11x     11x 11x 11x 5x     5x 5x 11x 5x     5x 5x 11x   7x 7x 7x 1x 1x 1x 11x 4x   4x 4x         11x 11x 1x 1x   5x   5x   6x     6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x    
<template>
  <form data-testid="event-form" @submit.prevent="handleSubmit" class="space-y-4">
    <!-- Title -->
    <BaseInput
      v-model="formData.title"
      :label="$t('eventForm.eventTitle')"
      :placeholder="$t('eventForm.eventTitlePlaceholder')"
      required
      :error="errors.title"
    />
 
    <!-- Event Type -->
    <div>
      <label class="block text-sm font-medium text-secondary-700 mb-1">
        {{ $t('eventForm.eventType') }} <span class="text-error-600">*</span>
      </label>
      <select
        v-model="formData.type"
        class="w-full px-3 py-2 border border-secondary-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500 transition-colors"
        required
      >
        <option value="practice">{{ $t('eventTypes.practice') }}</option>
        <option value="game">{{ $t('eventTypes.game') }}</option>
      </select>
    </div>
 
    <!-- Date and Time -->
    <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
      <BaseInput
        v-model="formData.date"
        type="date"
        :label="$t('eventForm.date')"
        required
        :error="errors.date"
      />
      <BaseInput
        v-model="formData.time"
        type="time"
        :label="$t('eventForm.time')"
        required
      />
    </div>
 
    <!-- Location -->
    <BaseInput
      v-model="formData.location"
      :label="$t('eventForm.location')"
      :placeholder="$t('eventForm.locationPlaceholder')"
      required
      :error="errors.location"
    />
 
    <!-- Notes (Optional) -->
    <div>
      <label class="block text-sm font-medium text-secondary-700 mb-1">
        {{ $t('eventForm.notes') }}
      </label>
      <textarea
        v-model="formData.notes"
        class="w-full px-3 py-2 border border-secondary-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500 transition-colors resize-y"
        rows="3"
        :placeholder="$t('eventForm.notesPlaceholder')"
      />
    </div>
 
    <!-- Actions -->
    <div class="flex flex-col sm:flex-row gap-2 pt-2">
      <BaseButton type="submit" :loading="loading" variant="primary" class="w-full sm:w-auto">
        {{ $t('eventForm.createEvent') }}
      </BaseButton>
      <BaseButton type="button" variant="secondary" @click="emit('cancel')" class="w-full sm:w-auto">
        {{ $t('eventForm.cancel') }}
      </BaseButton>
    </div>
  </form>
</template>
 
<script setup lang="ts">
/**
 * EventForm
 * @description Form component for creating new events with validation
 * Validates required fields and ensures date is in the future
 */
 
import type { Event, EventType } from '~/types'
 
interface FormData {
  title: string
  type: EventType
  date: string // YYYY-MM-DD format
  time: string // HH:MM format
  location: string
  notes: string
}
 
interface FormErrors {
  title: string
  date: string
  location: string
}
 
const emit = defineEmits<{
  /** Emitted when form is successfully validated and submitted */
  submit: [data: Omit<Event, 'id'>]
  /** Emitted when user clicks cancel */
  cancel: []
}>()
 
/**
 * i18n
 */
const { t } = useI18n()
 
/**
 * Form data state
 */
const formData = reactive<FormData>({
  title: '',
  type: 'practice',
  date: '',
  time: '',
  location: '',
  notes: ''
})
 
/**
 * Form validation errors
 */
const errors = reactive<FormErrors>({
  title: '',
  date: '',
  location: ''
})
 
/**
 * Loading state during submission
 */
const loading = ref(false)
 
/**
 * Validate form fields
 * @returns true if form is valid, false otherwise
 */
const validate = (): boolean => {
  // Reset errors
  errors.title = ''
  errors.date = ''
  errors.location = ''
 
  let isValid = true
 
  // Validate title
  if (!formData.title.trim()) {
    errors.title = t('eventForm.errors.titleRequired')
    isValid = false
  }
 
  // Validate location
  if (!formData.location.trim()) {
    errors.location = t('eventForm.errors.locationRequired')
    isValid = false
  }
 
  // Validate date and time
  if (formData.date && formData.time) {
    const selectedDateTime = new Date(`${formData.date}T${formData.time}`)
    const now = new Date()
 
    if (selectedDateTime <= now) {
      errors.date = t('eventForm.errors.dateMustBeFuture')
      isValid = false
    }
  } else if (!formData.date) {
    errors.date = t('eventForm.errors.dateRequired')
    isValid = false
  }
 
  return isValid
}
 
/**
 * Handle form submission
 */
const handleSubmit = async (): Promise<void> => {
  if (!validate()) {
    return
  }
 
  loading.value = true
 
  try {
    // Combine date and time into ISO datetime string
    const dateTime = new Date(`${formData.date}T${formData.time}`).toISOString()
 
    // Emit submit event with event data
    emit('submit', {
      title: formData.title.trim(),
      type: formData.type,
      dateTime,
      location: formData.location.trim(),
      notes: formData.notes.trim() || undefined
    })
  } finally {
    loading.value = false
  }
}
</script>