import { defineStore } from 'pinia' import type {ShipmentData, ShipmentPayload} from "~/services/dto/shipment-data"; import {createShipment, getShipment, updateShipment} from "~/services/shipment"; const isShipmentData = (value: unknown): value is ShipmentData => { return Boolean(value && typeof value === 'object' && 'id' in value) } export const useShipmentStore = defineStore('shipment', { state: () => ({ current: null as ShipmentData | null, isLoading: false }), actions: { setCurrent(shipment: ShipmentData | null) { this.current = shipment }, clearCurrent() { this.current = null }, async loadShipment(id: number) { this.isLoading = true const result = await getShipment(id).finally(() => { this.isLoading = false }) if (!isShipmentData(result)) { this.current = null return null } this.current = result return result }, async createShipment(payload: ShipmentPayload = {}) { this.isLoading = true const result = await createShipment(payload).finally(() => { this.isLoading = false }) if (!isShipmentData(result)) { return null } this.current = result return result }, async updateShipment(id: number, payload: ShipmentPayload) { this.isLoading = true const result = await updateShipment(id, payload).finally(() => { this.isLoading = false }) if (!isShipmentData(result)) { return null } this.current = result return result } } })