4a77449a41
| Numéro du ticket | Titre du ticket | |------------------|-----------------| | #203 | Réceptions — Parcours de pesée multi-étapes | ## Description de la PR [#203] Réceptions — Parcours de pesée multi-étapes ## Modification du .env ## Check list - [x] Pas de régression - [x] TU/TI/TF rédigée - [x] TU/TI/TF OK - [x] CHANGELOG modifié Reviewed-on: https://gitea.malio.fr/MALIO-DEV/Ferme/pulls/3 Reviewed-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr> Co-authored-by: AUTIN Tristan <tristan@yuno.malio.fr> Co-committed-by: AUTIN Tristan <tristan@yuno.malio.fr>
73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import type { ReceptionData } from '~/services/dto/reception-data'
|
|
import { createReception, getReception, updateReception } from '~/services/reception'
|
|
|
|
const isReceptionData = (value: unknown): value is ReceptionData => {
|
|
return Boolean(value && typeof value === 'object' && 'id' in value)
|
|
}
|
|
|
|
export const useReceptionStore = defineStore('reception', {
|
|
state: () => ({
|
|
current: null as ReceptionData | null,
|
|
isLoading: false,
|
|
errorMessage: null as string | null
|
|
}),
|
|
actions: {
|
|
setCurrent(reception: ReceptionData | null) {
|
|
this.current = reception
|
|
},
|
|
clearError() {
|
|
this.errorMessage = null
|
|
},
|
|
async loadReception(id: number) {
|
|
this.isLoading = true
|
|
this.errorMessage = null
|
|
try {
|
|
const result = await getReception(id)
|
|
if (!isReceptionData(result)) {
|
|
this.errorMessage = 'Réception introuvable.'
|
|
this.current = null
|
|
return null
|
|
}
|
|
|
|
this.current = result
|
|
return result
|
|
} finally {
|
|
this.isLoading = false
|
|
}
|
|
},
|
|
async createReception() {
|
|
this.isLoading = true
|
|
this.errorMessage = null
|
|
try {
|
|
const result = await createReception()
|
|
if (!isReceptionData(result)) {
|
|
this.errorMessage = 'Impossible de créer la réception.'
|
|
return null
|
|
}
|
|
|
|
this.current = result
|
|
return result
|
|
} finally {
|
|
this.isLoading = false
|
|
}
|
|
},
|
|
async updateReception(id: number, payload: Partial<ReceptionData>) {
|
|
this.isLoading = true
|
|
this.errorMessage = null
|
|
try {
|
|
const result = await updateReception(id, payload)
|
|
if (!isReceptionData(result)) {
|
|
this.errorMessage = 'Impossible de mettre à jour la réception.'
|
|
return null
|
|
}
|
|
|
|
this.current = result
|
|
return result
|
|
} finally {
|
|
this.isLoading = false
|
|
}
|
|
}
|
|
}
|
|
})
|