Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Resource Forms to Enhance Validation #9062

Open
wants to merge 30 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
46ec289
Update Resource Forms to Enhance Validation
JavidSumra Nov 8, 2024
9256e70
fix resource update test suit
JavidSumra Nov 9, 2024
41c1b1c
Merge branch 'develop' of github.com:JavidSumra/care_fe into issues/9…
JavidSumra Nov 9, 2024
5017f20
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
JavidSumra Nov 9, 2024
f68d453
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
JavidSumra Nov 11, 2024
9d3a9ba
Updated Handle Input to avoid Negative Value
JavidSumra Nov 19, 2024
098ba35
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
JavidSumra Nov 19, 2024
a5d862d
Remove Unnecessary Comments
JavidSumra Nov 19, 2024
7f5e000
Merge remote-tracking branch 'refs/remotes/origin/issues/9053/prevent…
JavidSumra Nov 19, 2024
fa5e529
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
JavidSumra Nov 20, 2024
fe78ab4
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
nihal467 Nov 20, 2024
de7d829
Add Requested Changes
JavidSumra Nov 21, 2024
6440d4b
Merge remote-tracking branch 'refs/remotes/origin/issues/9053/prevent…
JavidSumra Nov 21, 2024
2c98d02
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
JavidSumra Nov 22, 2024
bfaf9ce
Fix Min Value issue in Resource Create/Update Form
JavidSumra Nov 26, 2024
8c9ae68
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
JavidSumra Nov 26, 2024
f3d396d
Merge branch 'develop' of https://github.com/JavidSumra/care_fe into …
JavidSumra Nov 26, 2024
0dcc585
Fix sanitize function
JavidSumra Nov 26, 2024
32d5b35
Merge branch 'issues/9053/prevent-negative-quantity-and-empty-descrip…
JavidSumra Nov 26, 2024
5a0b57f
Fix Min Value Error issue in Resource Form Fields
JavidSumra Nov 26, 2024
e98f12d
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
JavidSumra Nov 27, 2024
13d19e0
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
JavidSumra Nov 27, 2024
03f2fcb
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
JavidSumra Nov 27, 2024
706c5b0
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
JavidSumra Nov 28, 2024
356f516
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
JavidSumra Nov 28, 2024
c910c84
Merge branch 'develop' into issues/9053/prevent-negative-quantity-and…
JavidSumra Nov 29, 2024
47804a5
Merge branch 'develop' of https://github.com/JavidSumra/care_fe into …
JavidSumra Nov 30, 2024
83d3113
Fix 0 Value on Resourse Request
JavidSumra Nov 30, 2024
27847a9
Merge branch 'ohcnetwork:develop' into issues/9053/prevent-negative-q…
JavidSumra Nov 30, 2024
d7e44c0
Merge branch 'issues/9053/prevent-negative-quantity-and-empty-descrip…
JavidSumra Nov 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cypress/e2e/resource_spec/ResourcesHomepage.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ describe("Resource Page", () => {
cy.visit(createdResource);
resourcePage.clickUpdateStatus();
resourcePage.updateStatus("APPROVED");
cy.clickAndSelectOption(
"input[name='assigned_facility_object']",
"Dummy Request Fulfilment Center, Ernakulam",
);
resourcePage.clickSubmitButton();
resourcePage.verifySuccessNotification(
"Resource request updated successfully",
Expand Down
10 changes: 10 additions & 0 deletions src/Utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,13 @@ export const fahrenheitToCelsius = (fahrenheit: number) => {
export const keysOf = <T extends object>(obj: T) => {
return Object.keys(obj) as (keyof T)[];
};

export const handleNegativeValue = (
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
event: React.FormEvent<HTMLInputElement>,
): void => {
const input = event.currentTarget;
input.value = input.value.replace(/-/g, "");
if (isNaN(Number(input.value))) {
input.value = "";
}
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
};
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions src/components/Form/FormFields/TextFormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export type TextFormFieldProps = FormFieldBaseProps<string> &
trailingPadding?: string | undefined;
leadingPadding?: string | undefined;
suggestions?: string[];
min?: number | string;
max?: number | string;
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
clearable?: boolean | undefined;
};

Expand Down Expand Up @@ -100,6 +102,8 @@ const TextFormField = forwardRef((props: TextFormFieldProps, ref) => {
type={props.type === "password" ? getPasswordFieldType() : props.type}
name={field.name}
value={field.value}
min={Number(props?.min || Number.MIN_SAFE_INTEGER)}
max={Number(props?.max || Number.MAX_SAFE_INTEGER)}
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
required={field.required}
onChange={(e) => field.handleChange(e.target.value)}
/>
Expand Down
5 changes: 4 additions & 1 deletion src/components/Resource/ResourceCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import * as Notification from "@/Utils/Notifications";
import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import useQuery from "@/Utils/request/useQuery";
import { parsePhoneNumber } from "@/Utils/utils";
import { handleNegativeValue, parsePhoneNumber } from "@/Utils/utils";

interface resourceProps {
facilityId: number;
Expand Down Expand Up @@ -300,8 +300,11 @@ export default function ResourceCreate(props: resourceProps) {
<TextFormField
label={t("required_quantity")}
name="requested_quantity"
type="number"
min={1}
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved
value={state.form.required_quantity}
onChange={handleChange}
onInput={handleNegativeValue}
/>

<div className="md:col-span-2">
Expand Down
37 changes: 29 additions & 8 deletions src/components/Resource/ResourceDetailsUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import * as Notification from "@/Utils/Notifications";
import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import useQuery from "@/Utils/request/useQuery";
import { handleNegativeValue } from "@/Utils/utils";

interface resourceProps {
id: string;
Expand All @@ -48,9 +49,15 @@ const requiredFields: any = {
approving_facility_object: {
errorText: "Resource approving facility can not be empty.",
},
assigned_facility_type: {
assigned_facility_object: {
errorText: "Please Select Facility Type",
},
title: {
errorText: "Title is required.",
},
reason: {
errorText: "Description is required.",
},
};

const initError = Object.assign(
Expand Down Expand Up @@ -101,7 +108,15 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
const errors = { ...initError };
let isInvalidForm = false;
Object.keys(requiredFields).forEach((field) => {
if (!state.form[field] || !state.form[field].length) {
if (
field === "approving_facility_object" ||
field === "assigned_facility_object"
) {
if (!state.form[field] || !state.form[field]?.name) {
errors[field] = requiredFields[field].errorText;
isInvalidForm = true;
}
} else if (!state.form[field] || state.form[field].trim().length === 0) {
errors[field] = requiredFields[field].errorText;
isInvalidForm = true;
}
Expand Down Expand Up @@ -144,7 +159,7 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
});

const handleSubmit = async () => {
const validForm = validateForm();
const validForm = !validateForm();
JavidSumra marked this conversation as resolved.
Show resolved Hide resolved

if (validForm) {
setIsLoading(true);
Expand Down Expand Up @@ -233,7 +248,7 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
setSelected={(obj) =>
setFacility(obj, "approving_facility_object")
}
errors={state.errors.approving_facility}
errors={state.errors.approving_facility_object}
/>
</div>

Expand All @@ -243,44 +258,49 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
</FieldLabel>
<FacilitySelect
multiple={false}
name="assigned_facility"
name="assigned_facility_object"
facilityType={1510}
selected={state.form.assigned_facility_object}
setSelected={(obj) =>
setFacility(obj, "assigned_facility_object")
}
errors={state.errors.assigned_facility}
errors={state.errors.assigned_facility_object}
/>
</div>
<div>
<TextFormField
label="Required Quantity"
name="requested_quantity"
type="number"
min={1}
value={state.form.requested_quantity}
onChange={handleChange}
onInput={handleNegativeValue}
/>
</div>
<div>
<TextFormField
name="assigned_quantity"
type="number"
min={1}
label="Approved Quantity"
value={state.form.assigned_quantity}
onChange={handleChange}
disabled={state.form.status !== "PENDING"}
onInput={handleNegativeValue}
/>
</div>

<div className="md:col-span-2">
<TextFormField
name="title"
type="text"
label="Request Title*"
label="Request Title"
placeholder="Type your title here"
value={state.form.title}
onChange={handleChange}
error={state.errors.title}
required
/>
</div>

Expand All @@ -291,8 +311,9 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
placeholder="Type your description here"
value={state.form.reason}
onChange={handleChange}
label="Description of request*"
label="Description of request"
error={state.errors.reason}
required
/>
</div>

Expand Down
Loading