Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,25 @@ function FormPagination (props) {
let [ activePage, setActivePage ] = useState(0);
let [ direction, setDirection ] = useState(1);
let [ nextActivePage, setNextActivePage ] = useState();
let [ numberOfSteps, setNumberOfSteps ] = useState();
let [ activeStep, setActiveStep ] = useState();
let [ progress, setProgress ] = useState(0);
const DIRECTION_NEXT = 1, DIRECTION_PREV = -1;
// The amount of the progress bar that should be complete on page 1
// Expressed as a multiple of a normal page size.
// The remainder (1 - stub size) will be used as a completion buffer on the last page
const INITIAL_PROGRESS_STUB = 0.7;

let previousEntryType;
let questionIndex = 0;
let pagesResults = {};
let pagesArray = [];

useEffect(() => {
setNumberOfSteps(pages.filter(page => page.canBeVisible).length);
setActiveStep(pages.slice(0, activePage).filter(page => page.canBeVisible).length);
}, [activePage, pages])

useEffect(() => {
setPagesCallback(null);
Object.entries(questionnaireData)
Expand Down Expand Up @@ -207,6 +219,17 @@ function FormPagination (props) {
}
}, [saveInProgress, pendingSubmission, disableProgress, nextActivePage, direction, activePage]);

useEffect(() => {
if (activeStep != null && numberOfSteps != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (activeStep != null && numberOfSteps != null) {
if (activeStep != null && numberOfSteps > 0) {

^ Still ensures numberOfSteps is defined and also avoids division by 0

// The MaterialUI progress bar expects progress to be out of 100
const pageSize = 100 / (numberOfSteps);
// Use some of 1 "page" worth of progression for the initial stub on the first page
// The rest will be used for the completion buffer on the last page
const stubSize = pageSize * INITIAL_PROGRESS_STUB;
setProgress(stubSize + (pageSize * activeStep) + (savedLastPage ? pageSize - stubSize : 0))
}
}, [activeStep, numberOfSteps, savedLastPage])

let saveButton =
<Button
startIcon={activePage === lastValidPage() ? doneIcon : undefined}
Expand Down Expand Up @@ -246,8 +269,6 @@ function FormPagination (props) {
}
stepperClasses = stepperClasses.join(' ');

let progressAdjustment = (condition) => (condition && variant == "progress" ? 1 : 0);

return (
enabled
?
Expand All @@ -265,23 +286,15 @@ function FormPagination (props) {
?
<MobileStepper
variant={variant}
// Offset back bar 1 to create a "current page" region.
// If the final page has been saved, progress the front bar to complete
activeStep={activePage + progressAdjustment(lastSaveStatus && savedLastPage)}
// Change the color of the back bar
activeStep={activeStep}
slotProps={{
progress: {
classes: {
bar2Buffer: classes.formStepperBufferBar,
dashed: classes.formStepperBackgroundBar,
},
variant: "buffer",
valueBuffer: (activePage + 1) / (lastValidPage() + 1) * 100,
// Manually control the progress bar value
value: progress
}
}}
className={stepperClasses}
// base 0 to base 1, plus 1 for the "current page" region when variant is "progress"
steps={lastValidPage() + 1 + progressAdjustment(true)}
steps={numberOfSteps}
nextButton={saveButton}
backButton={backButton}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,16 +496,6 @@ const questionnaireStyle = theme => ({
margin: theme.spacing(1),
minWidth: "fit-content",
},
formStepperBufferBar: {
backgroundColor: theme.palette.primary.main,
opacity: "0.3",
},
formStepperBackgroundBar: {
backgroundColor: theme.palette.primary.light,
opacity: "0.2",
animation: "none",
backgroundImage: "none",
},
actionsMenu: {
border: "1px solid " + theme.palette.divider,
borderRadius: theme.spacing(3),
Expand Down