b1c3952d09
| Numéro du ticket | Titre du ticket | |------------------|-----------------| | #271 | Créer une nouvelle expédition (étape 1) | ## Description de la PR ## Modification du .env ## Check list [x ] Pas de régression TU/TI/TF rédigée [x ] TU/TI/TF OK [x ] CHANGELOG modifié Co-authored-by: kevin <kevin@yuno.malio.fr> Reviewed-on: https://gitea.malio.fr/MALIO-DEV/Ferme/pulls/12 Reviewed-by: Autin <tristan@yuno.malio.fr> Co-authored-by: Matteo <matteo@yuno.malio.fr> Co-committed-by: Matteo <matteo@yuno.malio.fr>
59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
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
|
|
}
|
|
}
|
|
})
|