Skip to content

Commit

Permalink
Add validation when editing meal donation (modal) (#160)
Browse files Browse the repository at this point in the history
* Add validation when editing meal donation

* Lint backend
  • Loading branch information
ColinToft authored Aug 22, 2024
1 parent c7fe515 commit 27819ed
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
4 changes: 4 additions & 0 deletions backend/app/graphql/meal_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/graphql/test_meal_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
64 changes: 61 additions & 3 deletions frontend/src/pages/EditMealRequestForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -380,15 +435,17 @@ const EditMealRequestForm = ({
modified later)
</FormHelperText>
<Input
// TODO should we change this placeholder?
placeholder="Ex. 40 mac and cheeses with 9 gluten free ones. Also will donate 30 bags of cheetos."
value={mealDescription}
onChange={(e) => setMealDescription(e.target.value)}
ref={initialFocusRef}
isInvalid={attemptedSubmit && mealDescription === ""}
type="text"
/>
</FormControl>

<FormControl mt={3} isRequired>
<FormControl mt={3}>
<FormLabel
variant={{
base: "mobile-form-label-bold",
Expand All @@ -399,6 +456,7 @@ const EditMealRequestForm = ({
</FormLabel>
<Input
size="lg"
// Should we change this placeholder?
placeholder="Ex. A man with a beard will leave the food at the front door of the school."
value={additionalNotes}
onChange={(e) => setAdditionalNotes(e.target.value)}
Expand All @@ -408,7 +466,7 @@ const EditMealRequestForm = ({
<OnsiteContactsSection
onsiteInfo={mealDonorOnsiteContacts}
setOnsiteInfo={setMealDonorOnsiteContacts}
attemptedSubmit={false /* todo change */}
attemptedSubmit={attemptedSubmit}
availableStaff={availableOnsiteContacts}
dropdown
/>
Expand Down Expand Up @@ -533,7 +591,7 @@ const EditMealRequestForm = ({
<OnsiteContactsSection
onsiteInfo={onsiteContacts}
setOnsiteInfo={setOnsiteContacts}
attemptedSubmit={false /* todo change */}
attemptedSubmit={attemptedSubmit}
availableStaff={availableOnsiteContacts}
dropdown
/>
Expand Down

0 comments on commit 27819ed

Please sign in to comment.