Skip to content

Commit

Permalink
Don't allow negative values for quantity in Manage Inventory and Mini…
Browse files Browse the repository at this point in the history
…mum Quantity Required page (ohcnetwork#7455)

* add negative value check

* add validation for minimum quantity required page
  • Loading branch information
Pranshu1902 authored Mar 27, 2024
1 parent 0402261 commit ee803ce
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Components/Facility/AddInventoryForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ export const AddInventoryForm = (props: any) => {
if (!state.form[field]?.length) {
errors[field] = "Please select a quantity";
invalidForm = true;
} else if (state.form[field] <= 0) {
errors[field] = "Quantity must be more than 0";
invalidForm = true;
}
return;
case "unit":
Expand Down
30 changes: 30 additions & 0 deletions src/Components/Facility/SetInventoryForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ const initForm = {
id: "",
quantity: "",
};
const initError = Object.assign(
{},
...Object.keys(initForm).map((k) => ({ [k]: "" }))
);
const initialState = {
form: { ...initForm },
errors: { ...initError },
};

const inventoryFormReducer = (state = initialState, action: any) => {
Expand Down Expand Up @@ -99,8 +104,32 @@ export const SetInventoryForm = (props: any) => {
}
}, [state.form.id]);

const validateForm = () => {
const errors = { ...initError };
let invalidForm = false;

Object.keys(state.form).forEach((field) => {
switch (field) {
case "quantity":
if (!state.form[field]?.length) {
errors[field] = "Please select a quantity";
invalidForm = true;
} else if (state.form[field] < 0) {
errors[field] = "Quantity can't be negative";
invalidForm = true;
}
return;
}
});

dispatch({ type: "set_error", errors });
return !invalidForm;
};

const handleSubmit = async (e: any) => {
e.preventDefault();
const validated = validateForm();
if (!validated) return;
await request(routes.setMinQuantity, {
pathParams: { facilityId },
body: {
Expand Down Expand Up @@ -159,6 +188,7 @@ export const SetInventoryForm = (props: any) => {
type="number"
value={state.form.quantity}
onChange={handleChange}
error={state.errors.quantity}
/>

<TextFormField
Expand Down

0 comments on commit ee803ce

Please sign in to comment.