fix(dashboard): reservations quantity validations (#9393)

**What**
- validate quantity range when creating validation and handle form errors nicely

---

CLOSES CC-470
This commit is contained in:
Frane Polić authored and GitHub committed 2024-10-01 07:01:44 +00:00
1 parent b9e772b530
commit cab629556f
2 files changed
+30 -7

No files matched your search

@@ -735,7 +735,11 @@
"descriptionPlaceholder": "What type of reservation is this?", "descriptionPlaceholder": "What type of reservation is this?",
"successToast": "Reservation was successfully created.", "successToast": "Reservation was successfully created.",
"updateSuccessToast": "Reservation was successfully updated.", "updateSuccessToast": "Reservation was successfully updated.",
"deleteSuccessToast": "Reservation was successfully deleted." "deleteSuccessToast": "Reservation was successfully deleted.",
"errors": {
"noAvaliableQuantity": "Stock location doesn't have available quantity.",
"quantityOutOfRange": "Minimum quantity is 1 and maximum quantity is {{max}}"
}
}, },
"toast": { "toast": {
"updateLocations": "Locations updated successfully.", "updateLocations": "Locations updated successfully.",
@@ -91,6 +91,31 @@ export const ReservationCreateForm = (props: { inventoryItemId?: string }) => {
const { mutateAsync, isPending } = useCreateReservationItem() const { mutateAsync, isPending } = useCreateReservationItem()
const handleSubmit = form.handleSubmit(async (data) => { const handleSubmit = form.handleSubmit(async (data) => {
const min = 1
const max = selectedLocationLevel?.available_quantity
? selectedLocationLevel.available_quantity
: 0
if (!selectedLocationLevel?.available_quantity) {
form.setError("quantity", {
type: "manual",
message: t("inventory.reservation.errors.noAvaliableQuantity"),
})
return
}
if (data.quantity < min || data.quantity > max) {
form.setError("quantity", {
type: "manual",
message: t("inventory.reservation.errors.quantityOutOfRange", {
max: max,
}),
})
return
}
await mutateAsync(data, { await mutateAsync(data, {
onSuccess: ({ reservation }) => { onSuccess: ({ reservation }) => {
toast.success(t("inventory.reservation.successToast")) toast.success(t("inventory.reservation.successToast"))
@@ -219,12 +244,6 @@ export const ReservationCreateForm = (props: { inventoryItemId?: string }) => {
placeholder={t( placeholder={t(
"inventory.reservation.quantityPlaceholder" "inventory.reservation.quantityPlaceholder"
)} )}
min={1}
max={
selectedLocationLevel?.available_quantity
? selectedLocationLevel.available_quantity
: 0
}
value={value || ""} value={value || ""}
onChange={(e) => { onChange={(e) => {
const value = e.target.value const value = e.target.value