| 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>
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
import { computed, ref } from 'vue'
|
||||
import type { Ref } from 'vue'
|
||||
import type { ReceptionData, WeightEntryData } from '~/services/dto/reception-data'
|
||||
import type { WeightData } from '~/services/dto/weight-data'
|
||||
import { getWeight } from '~/services/reception'
|
||||
import { createWeight, updateWeight } from '~/services/weight'
|
||||
|
||||
export type WeighingMode = 'gross' | 'tare'
|
||||
|
||||
type UseWeighingOptions = {
|
||||
mode: WeighingMode
|
||||
reception: Ref<ReceptionData | null>
|
||||
updateReception: (id: number, payload: Partial<ReceptionData>) => Promise<ReceptionData | null>
|
||||
loadReception?: (id: number) => Promise<ReceptionData | null>
|
||||
storeError?: Ref<string | null>
|
||||
}
|
||||
|
||||
export const useWeighing = ({
|
||||
mode,
|
||||
reception,
|
||||
updateReception,
|
||||
loadReception,
|
||||
storeError
|
||||
}: UseWeighingOptions) => {
|
||||
const weightData = ref<WeightData | null>(null)
|
||||
const localErrorMessage = ref<string | null>(null)
|
||||
|
||||
const currentWeightEntry = computed<WeightEntryData | null>(() => {
|
||||
const weights = reception.value?.weights ?? []
|
||||
return weights.find((entry) => entry.type === mode) ?? null
|
||||
})
|
||||
|
||||
const displayWeight = computed(() => weightData.value?.weight ?? currentWeightEntry.value?.weight ?? null)
|
||||
const displayDsd = computed(() => weightData.value?.dsd ?? currentWeightEntry.value?.dsd ?? '-')
|
||||
const title = computed(() => (mode === 'gross' ? 'Pesée à plein' : 'Pesée à vide'))
|
||||
const errorMessage = computed(() => localErrorMessage.value ?? storeError?.value ?? null)
|
||||
const showLoadingBox = computed(() => displayWeight.value === null && !errorMessage.value)
|
||||
|
||||
const fetchWeight = async () => {
|
||||
localErrorMessage.value = null
|
||||
try {
|
||||
weightData.value = await getWeight()
|
||||
} catch (error) {
|
||||
localErrorMessage.value = error?.data?.error ?? error?.message ?? 'Erreur inconnue.'
|
||||
}
|
||||
}
|
||||
|
||||
const saveWeight = async () => {
|
||||
localErrorMessage.value = null
|
||||
if (!reception.value) {
|
||||
localErrorMessage.value = 'Réception introuvable.'
|
||||
return
|
||||
}
|
||||
|
||||
const existingEntry = currentWeightEntry.value
|
||||
const baseDsd = weightData.value?.dsd ?? existingEntry?.dsd ?? null
|
||||
const baseWeight = weightData.value?.weight ?? existingEntry?.weight ?? null
|
||||
const baseWeighedAt = weightData.value?.weighedAt ?? existingEntry?.weighedAt ?? null
|
||||
|
||||
if (baseWeight === null) {
|
||||
localErrorMessage.value = 'Veuillez d’abord peser.'
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
if (existingEntry?.id) {
|
||||
await updateWeight(existingEntry.id, {
|
||||
type: mode,
|
||||
dsd: baseDsd,
|
||||
weight: baseWeight,
|
||||
weighedAt: baseWeighedAt
|
||||
})
|
||||
} else {
|
||||
await createWeight({
|
||||
reception: `/receptions/${reception.value.id}`,
|
||||
type: mode,
|
||||
dsd: baseDsd,
|
||||
weight: baseWeight,
|
||||
weighedAt: baseWeighedAt
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
localErrorMessage.value = error?.data?.error ?? error?.message ?? 'Erreur inconnue.'
|
||||
return
|
||||
}
|
||||
|
||||
const nextStep = reception.value.currentStep + 1
|
||||
await updateReception(reception.value.id, {
|
||||
currentStep: nextStep,
|
||||
isValid: mode === 'tare' ? true : reception.value.isValid
|
||||
})
|
||||
|
||||
if (loadReception) {
|
||||
await loadReception(reception.value.id)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
weightData,
|
||||
currentWeightEntry,
|
||||
displayWeight,
|
||||
displayDsd,
|
||||
title,
|
||||
errorMessage,
|
||||
showLoadingBox,
|
||||
fetchWeight,
|
||||
saveWeight
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user