diff --git a/skyroster-frontend/src/stores/aircraft.js b/skyroster-frontend/src/stores/aircraft.js index a38496b..1e0408f 100644 --- a/skyroster-frontend/src/stores/aircraft.js +++ b/skyroster-frontend/src/stores/aircraft.js @@ -1,19 +1,45 @@ -import { ref, computed } from 'vue' -import { defineStore } from 'pinia' -import { initialAircraft, AIRCRAFT_TYPES, BASES, AVAILABILITY_STATUSES } from '../data/mockData' +import {computed, ref} from 'vue' +import {defineStore} from 'pinia' +import apiClient from '../api/axios' +import {AIRCRAFT_TYPES, AVAILABILITY_STATUSES, BASES} from '../data/mockData' + +async function getAircraftList() { + const res = await apiClient.get('/aircraft', { + headers: { + 'Content-Type': 'application/json' + } + }); + + return res.data; +} export const useAircraftStore = defineStore('aircraft', () => { - const aircraft = ref([...initialAircraft]) + const aircraft = ref([]); const aircraftTypeOptions = AIRCRAFT_TYPES const baseOptions = BASES const availabilityOptions = AVAILABILITY_STATUSES const aircraftCount = computed(() => aircraft.value.length) - - const availableAircraft = computed(() => + + const availableAircraft = computed(() => aircraft.value.filter(a => a.dostepnosc === 'dostepny') ) + async function fetchAircraftList() { + try { + const response = await getAircraftList(); + + aircraft.value = response.map(aircraft => ({ + id: aircraft.id, + rejestracja: aircraft.registrationNumber, + typ: aircraft.aircraftType.icaoCode, + baza: aircraft.operationalBase.icaoCode + })); + } catch (error) { + console.error(error); + } + } + function generateId() { const maxId = aircraft.value.reduce((max, a) => { const num = parseInt(a.id.replace('A', '')) @@ -40,13 +66,43 @@ export const useAircraftStore = defineStore('aircraft', () => { return null } - function deleteAircraft(id) { - const index = aircraft.value.findIndex(a => a.id === id) - if (index !== -1) { - aircraft.value.splice(index, 1) - return true + async function deleteAircraft(id) { + try { + const res = await apiClient.delete( + `/aircraft/${id}`, + { + headers: { + 'Content-Type': 'application/json' + } + } + ); + + if (res.status >= 200 && res.status < 300) { + const index = aircraft.value.findIndex(a => a.id === id); + + if (index !== -1) { + aircraft.value.splice(index, 1); + } + + return { + success: true + }; + } + } catch (error) { + console.error(error); + + return { + success: false, + message: + error.response?.data?.message || + 'Failed to delete aircraft' + }; } - return false + + return { + success: false, + message: 'Unexpected error occurred' + }; } function getAircraftById(id) { @@ -74,6 +130,7 @@ export const useAircraftStore = defineStore('aircraft', () => { deleteAircraft, getAircraftById, getAircraftDisplay, - getAvailabilityInfo + getAvailabilityInfo, + fetchAircraftList } }) diff --git a/skyroster-frontend/src/views/AircraftView.vue b/skyroster-frontend/src/views/AircraftView.vue index 8afc6da..5ebd602 100644 --- a/skyroster-frontend/src/views/AircraftView.vue +++ b/skyroster-frontend/src/views/AircraftView.vue @@ -1,14 +1,14 @@ @@ -106,11 +126,11 @@ function confirmDelete(aircraft) { {{ getBaseLabel(data.baza) }} - +