diff --git a/frontend/components/reception/update-bovin.vue b/frontend/components/reception/update-bovin.vue index f8f827b..264591b 100644 --- a/frontend/components/reception/update-bovin.vue +++ b/frontend/components/reception/update-bovin.vue @@ -1,13 +1,10 @@ diff --git a/frontend/components/reception/update-merchandise.vue b/frontend/components/reception/update-merchandise.vue index cb09c09..b9b3d4c 100644 --- a/frontend/components/reception/update-merchandise.vue +++ b/frontend/components/reception/update-merchandise.vue @@ -1,33 +1,29 @@ diff --git a/frontend/components/reception/update-weight.vue b/frontend/components/reception/update-weight.vue index e5ec053..4f05cf9 100644 --- a/frontend/components/reception/update-weight.vue +++ b/frontend/components/reception/update-weight.vue @@ -1,6 +1,22 @@ @@ -48,12 +33,17 @@ import type {ReceptionFormWeight} from '~/services/dto/reception-data' import {getReception} from '~/services/reception' import {updateWeight} from "~/services/weight"; import {useAuthStore} from "~/stores/auth"; +import {ref, watch} from "vue"; const props = defineProps<{ idReception: number + weightType: string + isValidate: boolean }>() const idReception = props.idReception +const weightType = props.weightType + const auth = useAuthStore() const form = reactive({ @@ -62,6 +52,10 @@ const form = reactive({ {id: 0, type: 'gross' as const, weight: 0, dsd: null, weighedAt: null} ] }) + +const weight = form.weights.find(w => w.type === weightType) +const initialWeight = ref<{ weight: number | null; dsd: number | null; weighedAt: string | null } | null>(null) + // DSD et date de pesée sont partagés entre tare et gross dans l'UI. const sharedWeightMeta = reactive<{ dsd: number | string | null @@ -71,8 +65,20 @@ const sharedWeightMeta = reactive<{ weighedAt: null }) -const getWeightLabel = (type: 'tare' | 'gross'): string => { - return type === 'tare' ? 'Pesée à vide' : 'Pesée à plein' +const normalizeMeta = () => { + const sharedDsd = + sharedWeightMeta.dsd === null || sharedWeightMeta.dsd === undefined || sharedWeightMeta.dsd === '' + ? null + : Number(sharedWeightMeta.dsd) + const sharedWeighedAt = + sharedWeightMeta.weighedAt === null || sharedWeightMeta.weighedAt === undefined || sharedWeightMeta.weighedAt === '' + ? null + : sharedWeightMeta.weighedAt + + return { + dsd: Number.isFinite(sharedDsd) ? sharedDsd : null, + weighedAt: sharedWeighedAt + } } const hydrateFromReception = (reception: ReceptionFormWeight) => { @@ -94,6 +100,15 @@ const hydrateFromReception = (reception: ReceptionFormWeight) => { sharedWeightMeta.dsd = weightWithMeta.dsd ?? null sharedWeightMeta.weighedAt = weightWithMeta.weighedAt ?? null } + + if (weight) { + const normalized = normalizeMeta() + initialWeight.value = { + weight: weight.weight ?? null, + dsd: normalized.dsd, + weighedAt: normalized.weighedAt + } + } } onMounted(async () => { @@ -101,24 +116,48 @@ onMounted(async () => { hydrateFromReception(reception) }) -async function validate() { - const sharedDsd = - sharedWeightMeta.dsd === null || sharedWeightMeta.dsd === undefined || sharedWeightMeta.dsd === '' - ? null - : Number(sharedWeightMeta.dsd) - const sharedWeighedAt = - sharedWeightMeta.weighedAt === null || sharedWeightMeta.weighedAt === undefined || sharedWeightMeta.weighedAt === '' - ? null - : sharedWeightMeta.weighedAt - for (const weight of form.weights) { - if (weight.id) { - await updateWeight(weight.id, { - weight: weight.weight, - dsd: Number.isFinite(sharedDsd) ? sharedDsd : null, - weighedAt: sharedWeighedAt - }) - } +watch( + () => props.isValidate, + async (val) => { + if (!val) return + await runValidate() + } +) + +const hasChanged = (current: { weight: number | null; dsd: number | null; weighedAt: string | null }) => { + if (!initialWeight.value) { + return true + } + return ( + (current.weight ?? null) !== (initialWeight.value.weight ?? null) || + (current.dsd ?? null) !== (initialWeight.value.dsd ?? null) || + (current.weighedAt ?? null) !== (initialWeight.value.weighedAt ?? null) + ) +} + +const runValidate = async () => { + if (!weight?.id) { + return } + const normalized = normalizeMeta() + const current = { + weight: weight.weight ?? null, + dsd: normalized.dsd, + weighedAt: normalized.weighedAt + } + + if (!hasChanged(current)) { + return + } + + await updateWeight(weight.id, { + weight: current.weight, + dsd: current.dsd, + weighedAt: current.weighedAt + }) + + initialWeight.value = current } + diff --git a/frontend/components/ui/UiCheckbox.vue b/frontend/components/ui/UiCheckbox.vue index 39770b0..ea923b7 100644 --- a/frontend/components/ui/UiCheckbox.vue +++ b/frontend/components/ui/UiCheckbox.vue @@ -1,14 +1,14 @@