From 36ff1c726883de4b97f75302dcf20285364e0343 Mon Sep 17 00:00:00 2001 From: Colin Toft Date: Mon, 19 Aug 2024 20:14:23 -0400 Subject: [PATCH 1/2] Add validation when editing meal donation --- backend/app/graphql/meal_request.py | 4 ++ frontend/src/pages/EditMealRequestForm.tsx | 64 +++++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/backend/app/graphql/meal_request.py b/backend/app/graphql/meal_request.py index 79036c6b..88e045b5 100644 --- a/backend/app/graphql/meal_request.py +++ b/backend/app/graphql/meal_request.py @@ -149,6 +149,10 @@ def mutate( "Requestor is not an admin or the donor of the meal request." ) + # For now, enforce that a meal description is required + if not meal_description: + raise Exception("Meal description is required.") + result = services["meal_request_service"].update_meal_request_donation( requestor_id=requestor_id, meal_request_id=meal_request_id, diff --git a/frontend/src/pages/EditMealRequestForm.tsx b/frontend/src/pages/EditMealRequestForm.tsx index f29fca4d..2aeb2a27 100644 --- a/frontend/src/pages/EditMealRequestForm.tsx +++ b/frontend/src/pages/EditMealRequestForm.tsx @@ -266,9 +266,54 @@ const EditMealRequestForm = ({ const [updateMealRequest] = useMutation(UPDATE_MEAL_REQUEST); const [updateMealDonation] = useMutation(UPDATE_MEAL_DONATION); + // For validation + const validateData = () => { + if ( + numberOfMeals <= 0 || + onsiteContacts.length === 0 || + onsiteContacts.some( + (contact) => + !contact || + contact.name === "" || + contact.email === "" || + contact.phone === "", + ) + ) { + setAttemptedSubmit(true); + return false; + } + + if (isEditDonation) { + if (mealDescription === "") { + setAttemptedSubmit(true); + return false; + } + } + + if (!isEditDonation) { + if (deliveryInstructions === "") { + setAttemptedSubmit(true); + return false; + } + } + + setAttemptedSubmit(false); + return true; + }; + async function submitEditMealRequest() { try { setLoading(true); + + // Validate the data + const valid = validateData(); + + // If there are any errors, return + if (!valid) { + setLoading(false); + return; + } + const response = await updateMealRequest({ variables: { requestorId, @@ -306,6 +351,16 @@ const EditMealRequestForm = ({ async function submitEditMealDonation() { try { setLoading(true); + + // Validate the data + const valid = validateData(); + + // If there are any errors, return + if (!valid) { + setLoading(false); + return; + } + const response = await updateMealDonation({ variables: { requestorId, @@ -380,15 +435,17 @@ const EditMealRequestForm = ({ modified later) setMealDescription(e.target.value)} ref={initialFocusRef} + isInvalid={attemptedSubmit && mealDescription === ""} type="text" /> - + setAdditionalNotes(e.target.value)} @@ -408,7 +466,7 @@ const EditMealRequestForm = ({ @@ -533,7 +591,7 @@ const EditMealRequestForm = ({ From 9d173d677a7eededfb1be6601c0ed87f328a2504 Mon Sep 17 00:00:00 2001 From: Colin Toft Date: Wed, 21 Aug 2024 20:44:24 -0400 Subject: [PATCH 2/2] Lint backend --- backend/tests/graphql/test_meal_request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/tests/graphql/test_meal_request.py b/backend/tests/graphql/test_meal_request.py index ad6c9c95..789c097a 100644 --- a/backend/tests/graphql/test_meal_request.py +++ b/backend/tests/graphql/test_meal_request.py @@ -261,7 +261,7 @@ def test_create_meal_request_fails_repeat_date( existing_date = datetime.strptime( meal_request.drop_off_datetime, "%Y-%m-%dT%H:%M:%S" ) - invalid_new_time = str((existing_date + timedelta(hours=3)).time())+ "Z" + invalid_new_time = str((existing_date + timedelta(hours=3)).time()) + "Z" counter_before = MealRequest.objects().count() mutation = f"""