Files
Ferme/frontend/components/reception/update-weight.vue
2026-02-12 17:09:22 +01:00

103 lines
3.1 KiB
Vue

<template>
<form @submit.prevent="validate">
<div class="grid grid-cols-2 gap-x-40 gap-y-8 mb-8">
<UiTextInput
label="Dsd"
class="col-start-2"
v-model="form.weights[0].dsd"
:disabled="!auth.isAdmin"
/>
<UiDateInput
label="Date pesée"
v-model="form.weights[0].weighedAt"
:disabled="!auth.isAdmin"
/>
</div>
<div class="grid grid-cols-2 gap-x-40 mb-16">
<UiNumberInput
label="Pesée à vide"
labelClass="font-bold uppercase text-xl"
v-model="form.weights[0].weight"
wrapper-class="col-start-1 row-start-1"
:disabled="!auth.isAdmin"
/>
<UiNumberInput
label="Pesée à plein"
labelClass="font-bold uppercase text-xl"
v-model="form.weights[1].weight"
wrapper-class="col-start-2 row-start-1"
:disabled="!auth.isAdmin"
/>
</div>
<div class="flex justify-center">
<button
v-if="auth.isAdmin"
type="submit"
class="text-xl uppercase bg-primary-500 text-white h-[50px] w-[272px]"
>
Valider
</button>
</div>
</form>
</template>
<script setup lang="ts">
import type {ReceptionFormWeight} from '~/services/dto/reception-data'
import {getReception} from '~/services/reception'
import {updateWeight} from "~/services/weight";
import {useAuthStore} from "~/stores/auth";
const props = defineProps<{
idReception: number
}>()
const idReception = props.idReception
const auth = useAuthStore()
const form = reactive({
weights: [
{id: 0, type: 'tare' as const, weight: 0, dsd: null, weighedAt: null},
{id: 0, type: 'gross' as const, weight: 0, dsd: null, weighedAt: null}
]
})
const hydrateFromReception = (reception: ReceptionFormWeight) => {
const tare = reception.weights.find(weight => weight.type === 'tare')
const gross = reception.weights.find(weight => weight.type === 'gross')
if (tare) form.weights[0] = {...form.weights[0], ...tare}
if (gross) form.weights[1] = {...form.weights[1], ...gross}
}
onMounted(async () => {
const reception = await getReception(idReception)
hydrateFromReception(reception)
})
async function validate() {
const sharedDsd =
form.weights[0].dsd === null || form.weights[0].dsd === undefined || form.weights[0].dsd === ''
? null
: Number(form.weights[0].dsd)
const sharedWeighedAt =
form.weights[0].weighedAt === null || form.weights[0].weighedAt === undefined || form.weights[0].weighedAt === ''
? null
: form.weights[0].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
})
}
}
}
</script>