ptitlutins/app/stores/voyagesLinksStore.ts
2025-09-27 04:05:48 +02:00

96 lines
3 KiB
TypeScript

import { defineStore } from "pinia"
import { voyagesLinksService } from "../services/voyagesLinksService"
import type { VoyageData, VoyageLink } from "../types"
export const useVoyagesLinksStore = defineStore("voyagesLinks", {
state: () => ({
voyagesLinks: [] as VoyageLink[],
currentVoyageLink: null as VoyageLink | null,
isLoading: false,
error: null as string | null,
}),
actions: {
async fetchVoyagesLinks() {
this.isLoading = true
this.error = null
try {
const voyagesLinks = await voyagesLinksService.getVoyagesLinks()
this.voyagesLinks = voyagesLinks
} catch (error: any) {
this.error = error.message || "Failed to fetch voyages"
} finally {
this.isLoading = false
}
},
async createVoyage(voyageData: VoyageData) {
this.isLoading = true
this.error = null
try {
const newVoyageLink = await voyagesLinksService.createVoyage(voyageData)
this.voyagesLinks.push(newVoyageLink)
this.currentVoyageLink = newVoyageLink
} catch (error: any) {
this.error = error.message || "Failed to create voyage"
} finally {
this.isLoading = false
}
},
async setCurrentVoyageLink(voyageLinkId: string) {
this.isLoading = true
this.error = null
try {
const voyageLink =
await voyagesLinksService.getVoyageLinkDetails(voyageLinkId)
this.currentVoyageLink = voyageLink
} catch (error: any) {
this.error = error.message || "Failed to fetch voyage details"
} finally {
this.isLoading = false
}
},
clearCurrentVoyageLink() {
this.currentVoyageLink = null
},
// async updateVoyage(voyageLinkId: string, voyageData: VoyageData) {
// this.isLoading = true;
// this.error = null;
// try {
// const updatedVoyage = await voyageService.updateVoyage(
// voyageId,
// voyageData
// );
// const index = this.voyages.findIndex((v) => v.id === voyageId);
// if (index !== -1) {
// this.voyages[index] = updatedVoyage;
// }
// if (this.currentVoyage && this.currentVoyage.id === voyageId) {
// this.currentVoyage = updatedVoyage;
// }
// } catch (error) {
// this.error = error.message || "Failed to update voyage";
// } finally {
// this.isLoading = false;
// }
// },
// async deleteVoyage(voyageId: string) {
// this.isLoading = true;
// this.error = null;
// try {
// await voyageService.deleteVoyage(voyageId);
// this.voyages = this.voyages.filter((v) => v.id !== voyageId);
// if (this.currentVoyage && this.currentVoyage.id === voyageId) {
// this.clearCurrentVoyage();
// }
// } catch (error) {
// this.error = error.message || "Failed to delete voyage";
// } finally {
// this.isLoading = false;
// }
// },
},
})