Skip to content

Commit 451a626

Browse files
committed
Fix chrome history on 3 step forms (meal creation and donation form)
1 parent fea1a4e commit 451a626

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

frontend/src/components/asp/requests/CreateMealRequest.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ const CreateMealRequest = (): React.ReactElement => {
117117
header1="Scheduling"
118118
header2="Meal donation information"
119119
header3="Review & submit"
120+
shouldGoBackToStep1={(currentStep) => {
121+
if(currentStep > 0 && mealRequestDates.length === 0) {
122+
return true;
123+
}
124+
if(currentStep > 1 && (onsiteContact.length === 0 || onsiteContact[0].name === "")) {
125+
return true;
126+
}
127+
128+
return false;
129+
}}
120130
panel1={
121131
isWeeklyInput ? (
122132
<SchedulingFormWeekly

frontend/src/components/common/ThreeStepForm.tsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ import {
1212
Text,
1313
useBreakpointValue,
1414
} from "@chakra-ui/react";
15-
import React, { useState } from "react";
15+
import React, { useEffect, useState } from "react";
16+
import { useLocation, useNavigate } from "react-router-dom";
17+
18+
19+
type PageNavigationState = { tabIndex: number }
20+
21+
function getTabIndex(state: unknown) : number {
22+
if (!state) return 0; // Makes sure it's not null
23+
if (typeof state !== "object") return 0;
24+
if (typeof (state as PageNavigationState).tabIndex !== "number") return 0;
25+
return (state as PageNavigationState).tabIndex;
26+
}
1627

1728
/**
1829
* A three-step form with tabs at the top.
@@ -26,26 +37,46 @@ const ThreeStepForm = ({
2637
panel1,
2738
panel2,
2839
panel3,
40+
shouldGoBackToStep1,
2941
}: {
3042
header1: string;
3143
header2: string;
3244
header3: string;
3345
panel1: React.ReactElement;
3446
panel2: React.ReactElement;
3547
panel3: React.ReactElement;
48+
shouldGoBackToStep1: (currentStep : number) => boolean;
3649
}): React.ReactElement => {
3750
const fontSize = useBreakpointValue({ base: "12px", sm: "16px", md: "20px" });
51+
const { state, pathname, search } = useLocation();
3852

3953
const [tabIndex, setTabIndex] = useState(0);
54+
const navigate = useNavigate();
55+
56+
// Get state:
57+
useEffect(() => {
58+
let newTabIndex = getTabIndex(state);
59+
60+
if (shouldGoBackToStep1(newTabIndex)) {
61+
newTabIndex = 0;
62+
navigate(pathname + search, { state: { tabIndex: 0 } });
63+
window.history.replaceState({}, '', '')
64+
}
65+
66+
setTabIndex(newTabIndex);
67+
}, [navigate, pathname, search, shouldGoBackToStep1, state])
4068

4169
const handleNext = () => {
4270
const thisTab = tabIndex;
4371
setTabIndex((prevIndex) => prevIndex + 1);
72+
navigate(pathname + search, { state: { tabIndex: thisTab + 1 } });
4473
};
4574

75+
4676
const handleBack = () => {
4777
const thisTab = tabIndex;
4878
setTabIndex((prevIndex) => prevIndex - 1);
79+
navigate(pathname + search, { state: { tabIndex: thisTab - 1 } });
4980
};
5081

5182
return (

frontend/src/components/meal_donor/donation_form/MealDonationForm.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const MealDonationForm = (): React.ReactElement => {
3030
]);
3131

3232
// Step 2: Meal details
33+
const location = useLocation();
34+
const searchParams = new URLSearchParams(location.search);
35+
const idsParam = searchParams.get("ids");
36+
const aspId = searchParams.get("aspId");
37+
3338
const [mealDescription, setMealDescription] = useState<string>("");
3439
const [additionalInfo, setAdditionalInfo] = useState<string>("");
3540

@@ -62,10 +67,6 @@ const MealDonationForm = (): React.ReactElement => {
6267
};
6368

6469
const requestorId = authenticatedUser?.id || "";
65-
const location = useLocation();
66-
const searchParams = new URLSearchParams(location.search);
67-
const idsParam = searchParams.get("ids");
68-
const aspId = searchParams.get("aspId");
6970
// Split the idsParam by dot to get an array of ids
7071
const ids = idsParam ? idsParam.split(",") : [];
7172

@@ -158,6 +159,15 @@ const MealDonationForm = (): React.ReactElement => {
158159
header1="Contact Information"
159160
header2="Meal Details"
160161
header3="Review & Submit"
162+
shouldGoBackToStep1={(currentStep) => {
163+
if (currentStep > 0 && (onsiteContacts.length === 0 || onsiteContacts[0].name === "")) {
164+
return true;
165+
}
166+
if (currentStep > 1 && mealDescription === "") {
167+
return true;
168+
}
169+
return false
170+
}}
161171
panel1={
162172
<MealDonationFormContactInfo
163173
onsiteContact={onsiteContacts}

0 commit comments

Comments
 (0)