User can add/edit flights

This commit is contained in:
2026-04-14 20:06:33 +10:00
parent a9aa65f0d2
commit 4110b52ba5
2 changed files with 52 additions and 46 deletions
+40 -27
View File
@@ -5,6 +5,7 @@ import { Head, useForm } from '@inertiajs/vue3'
import AirlineSearchBox from '@/Components/FlightsGoneBy/AirlineSearchBox.vue'
import AircraftSearchBox from '@/Components/FlightsGoneBy/AircraftSearchBox.vue'
import AirportSearchBox from '@/Components/FlightsGoneBy/AirportSearchBox.vue'
import type { SeatType, FlightReason, FlightClass} from '@/Types/types'
import { ref } from 'vue'
defineOptions({ layout: MainLayout })
@@ -19,17 +20,17 @@ const props = defineProps<{
seat_number: string
note: string
auto_update: boolean
seat_type: { value: number; title: string } | null
flight_class: { value: number; title: string } | null
flight_reason: { value: number; title: string } | null
seat_type: SeatType | null
flight_class: FlightClass | null
flight_reason: FlightReason | null
airline_options: { value: number; title: string }[]
from_options: { value: number; title: string; country_code: string }[]
to_options: { value: number; title: string; country_code: string }[]
aircraft_options: { value: number; title: string }[]
}
seat_types: { value: number; title: string }[]
flight_classes: { value: number; title: string }[]
flight_reasons: { value: number; title: string }[]
seat_types: SeatType[]
flight_classes: FlightClass[]
flight_reasons: FlightReason[]
}>()
const isEdit = !!props.flight
@@ -50,6 +51,7 @@ interface LookupResult {
const lookupResult = ref<LookupResult | null>(null)
const lookupKey = ref(0)
async function lookupFlight() {
if (!flightNumber.value.trim()) return
lookupLoading.value = true
@@ -93,7 +95,7 @@ async function lookupFlight() {
}
}
// ── Form ──────────────────────────────────────────────────────────────────────
// ── Display form (drives the template) ───────────────────────────────────────
const form = useForm({
flight_number: props.flight?.flight_number ?? '',
@@ -105,13 +107,15 @@ const form = useForm({
aircraft: props.flight?.aircraft_options[0] ?? null as { value: number; title: string } | null,
aircraft_registration: props.flight?.aircraft_registration ?? '',
seat_number: props.flight?.seat_number ?? '',
seat_type: props.flight?.seat_type ?? props.seat_types[0],
flight_class: props.flight?.flight_class ?? props.flight_classes[0],
flight_reason: props.flight?.flight_reason ?? props.flight_reasons[0],
seat_type: props.flight?.seat_type ?? props.seat_types[0] ?? null as SeatType | null,
flight_class: props.flight?.flight_class ?? props.flight_classes[0] ?? null as FlightClass | null,
flight_reason: props.flight?.flight_reason ?? props.flight_reasons[0] ?? null as FlightReason | null,
note: props.flight?.note ?? '',
auto_update: props.flight?.auto_update ?? false,
})
// ── Submit form (ID-based, what actually gets sent) ───────────────────────────
const submitForm = useForm({
flight_number: '' as string | null,
departure_date: '' as string | null,
@@ -130,20 +134,20 @@ const submitForm = useForm({
})
function submit() {
submitForm.flight_number = form.flight_number
submitForm.departure_date = form.departure_date
submitForm.arrival_date = form.arrival_date
submitForm.from_id = form.from?.value ?? null
submitForm.to_id = form.to?.value ?? null
submitForm.airline_id = form.airline?.value ?? null
submitForm.aircraft_id = form.aircraft?.value ?? null
submitForm.aircraft_registration = form.aircraft_registration
submitForm.seat_number = form.seat_number
submitForm.seat_type_id = form.seat_type?.value ?? null
submitForm.flight_class_id = form.flight_class?.value ?? null
submitForm.flight_reason_id = form.flight_reason?.value ?? null
submitForm.note = form.note
submitForm.auto_update = form.auto_update
submitForm.flight_number = form.flight_number
submitForm.departure_date = form.departure_date
submitForm.arrival_date = form.arrival_date
submitForm.from_id = form.from?.value ?? null
submitForm.to_id = form.to?.value ?? null
submitForm.airline_id = form.airline?.value ?? null
submitForm.aircraft_id = form.aircraft?.value ?? null
submitForm.aircraft_registration = form.aircraft_registration
submitForm.seat_number = form.seat_number
submitForm.seat_type_id = form.seat_type?.id
submitForm.flight_class_id = form.flight_class?.id
submitForm.flight_reason_id = form.flight_reason?.id
submitForm.note = form.note
submitForm.auto_update = form.auto_update
if (isEdit) {
submitForm.put(route('flights.update', { flight: props.flight!.id }))
@@ -154,9 +158,9 @@ function submit() {
// ── Prefilled options ─────────────────────────────────────────────────────────
const airlineOptionsData = ref<{ value: number; title: string }[]>(props.flight?.airline_options ?? [])
const fromOptionsData = ref<{ value: number; title: string; country_code: string }[]>(props.flight?.from_options ?? [])
const toOptionsData = ref<{ value: number; title: string; country_code: string }[]>(props.flight?.to_options ?? [])
const airlineOptionsData = ref<{ value: number; title: string }[]>(props.flight?.airline_options ?? [])
const fromOptionsData = ref<{ value: number; title: string; country_code: string }[]>(props.flight?.from_options ?? [])
const toOptionsData = ref<{ value: number; title: string; country_code: string }[]>(props.flight?.to_options ?? [])
const aircraftOptionsData = ref<{ value: number; title: string }[]>(props.flight?.aircraft_options ?? [])
</script>
@@ -288,6 +292,9 @@ const aircraftOptionsData = ref<{ value: number; title: string }[]>(props.flight
v-model="form.flight_class"
label="Flight Class"
:items="flight_classes"
item-title="name"
item-value="id"
:return-object="true"
:disabled="!lookupComplete"
:error-messages="submitForm.errors.flight_class_id"
/>
@@ -310,6 +317,9 @@ const aircraftOptionsData = ref<{ value: number; title: string }[]>(props.flight
v-model="form.seat_type"
label="Seat Type"
:items="seat_types"
item-title="name"
item-value="id"
:return-object="true"
:disabled="!lookupComplete"
:error-messages="submitForm.errors.seat_type_id"
/>
@@ -323,6 +333,9 @@ const aircraftOptionsData = ref<{ value: number; title: string }[]>(props.flight
v-model="form.flight_reason"
label="Flight Reason"
:items="flight_reasons"
item-title="name"
item-value="id"
:return-object="true"
:disabled="!lookupComplete"
:error-messages="submitForm.errors.flight_reason_id"
/>