Skip to content

Commit

Permalink
Refactor code related to confirmation checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
zgong-gov committed Oct 8, 2024
1 parent 4391a5e commit b0cbfd1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ export const AmendPermitReview = () => {
const trailerSubTypesQuery = useTrailerSubTypesQuery();

// For the confirmation checkboxes
const [isChecked, setIsChecked] = useState(false);
const [isSubmitted, setIsSubmitted] = useState(false);
const [allConfirmed, setAllConfirmed] = useState(false);
const [hasAttemptedSubmission, setHasAttemptedSubmission] = useState(false);

const onSubmit = async () => {
setIsSubmitted(true);
if (!isChecked) return;
setHasAttemptedSubmission(true);
if (!allConfirmed) return;

if (!amendmentApplication) {
return navigate(ERROR_ROUTES.UNEXPECTED);
Expand Down Expand Up @@ -133,9 +133,9 @@ export const AmendPermitReview = () => {
continueBtnText="Continue"
onEdit={back}
onContinue={onSubmit}
allChecked={isChecked}
setAllChecked={setIsChecked}
hasAttemptedCheckboxes={isSubmitted}
allConfirmed={allConfirmed}
setAllConfirmed={setAllConfirmed}
hasAttemptedCheckboxes={hasAttemptedSubmission}
powerUnitSubTypes={powerUnitSubTypesQuery.data}
trailerSubTypes={trailerSubTypesQuery.data}
vehicleDetails={amendmentApplication?.permitData?.vehicleDetails}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export const ApplicationReview = () => {
const methods = useForm<Application>();

// For the confirmation checkboxes
const [isChecked, setIsChecked] = useState(false);
const [isSubmitted, setIsSubmitted] = useState(false);
const [allConfirmed, setAllConfirmed] = useState(false);
const [hasAttemptedSubmission, setHasAttemptedSubmission] = useState(false);

// Send data to the backend API
const saveApplicationMutation = useSaveApplicationMutation();
Expand Down Expand Up @@ -96,9 +96,9 @@ export const ApplicationReview = () => {
};

const handleAddToCart = async () => {
setIsSubmitted(true);
setHasAttemptedSubmission(true);

if (!isChecked) return;
if (!allConfirmed) return;

const companyId = applicationData?.companyId;
const permitId = applicationData?.permitId;
Expand Down Expand Up @@ -166,9 +166,9 @@ export const ApplicationReview = () => {
contactDetails={applicationData?.permitData?.contactDetails}
onEdit={back}
onAddToCart={handleAddToCart}
allChecked={isChecked}
setAllChecked={setIsChecked}
hasAttemptedCheckboxes={isSubmitted}
allConfirmed={allConfirmed}
setAllConfirmed={setAllConfirmed}
hasAttemptedCheckboxes={hasAttemptedSubmission}
powerUnitSubTypes={powerUnitSubTypesQuery.data}
trailerSubTypes={trailerSubTypesQuery.data}
vehicleDetails={applicationData?.permitData?.vehicleDetails}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,84 +1,93 @@
import { Box, Checkbox, Typography } from "@mui/material";
import { Dispatch, SetStateAction, useState } from "react";
import { Nullable } from "../../../../../../common/types/common";
import { CustomInputHTMLAttributes } from "../../../../../../common/types/formElements";
import { useState } from "react";

import "./ConfirmationCheckboxes.scss";
import { CustomInputHTMLAttributes } from "../../../../../../common/types/formElements";

export const ConfirmationCheckboxes = ({
isSubmitted,
isChecked,
setIsChecked,
isDisabled,
hasAttemptedSubmission,
areAllChecked,
setAreAllChecked,
shouldDisableCheckboxes,
}: {
isSubmitted: boolean;
isChecked: boolean;
setIsChecked: Dispatch<SetStateAction<boolean>>;
isDisabled?: Nullable<boolean>;
hasAttemptedSubmission: boolean;
areAllChecked: boolean;
setAreAllChecked: (allChecked: boolean) => void;
shouldDisableCheckboxes: boolean;
}) => {
const checkboxes = [
{
description:
"Confirm that this permit is issued to the registered owner (or lessee) of the vehicle being permitted.",
checked: false,
checked: shouldDisableCheckboxes,
},
{
description:
"Confirm compliance with the appropriate policy for the selected commodity(s).",
checked: false,
checked: shouldDisableCheckboxes,
},
{
description: "Confirm the information in this application is correct.",
checked: false,
checked: shouldDisableCheckboxes,
},
];
const [checked, setChecked] = useState(checkboxes);

const [confirmationCheckboxes, setConfirmationCheckboxes] = useState(checkboxes);

const handleCheck = (checkedName: string) => {
let isValid = true;
const updated = checked.map((item) => {
if (shouldDisableCheckboxes) return;

const updatedCheckboxes = confirmationCheckboxes.map((item) => {
if (shouldDisableCheckboxes) return item;

if (item.description === checkedName) {
item.checked = !item.checked;
return {
description: item.description,
checked: !item.checked,
};
}
if (!item.checked) isValid = false;

return item;
});
setChecked(updated);
setIsChecked(isValid);
setConfirmationCheckboxes(updatedCheckboxes);
setAreAllChecked(!updatedCheckboxes.some(updated => !updated.checked));
};

return (
<Box className="confirmation-checkboxes">
<Typography className="confirmation-checkboxes__header" variant="h3">
Please read the following carefully and check all to proceed.
</Typography>
{checked.map((x) => (

{confirmationCheckboxes.map(({ description, checked }) => (
<Box
key={x.description}
key={description}
className="confirmation-checkboxes__attestation"
>
<Checkbox
className={
"confirmation-checkboxes__checkbox " +
`${
isSubmitted && !x.checked
hasAttemptedSubmission && !checked
? "confirmation-checkboxes__checkbox--invalid"
: ""
}`
}
key={x.description}
checked={isDisabled ? true : x.checked}
disabled={Boolean(isDisabled)}
onChange={() => handleCheck(x.description)}
key={description}
checked={checked}
disabled={shouldDisableCheckboxes}
onChange={() => handleCheck(description)}
inputProps={
{
"data-testid": "permit-attestation-checkbox",
} as CustomInputHTMLAttributes
}
/>
{x.description}
{description}
</Box>
))}
{isSubmitted && !isChecked ? (

{hasAttemptedSubmission && !areAllChecked ? (
<Typography
className="confirmation-checkboxes__error"
data-testid="permit-attestation-checkbox-error"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Box } from "@mui/material";
import { Dayjs } from "dayjs";
import { Dispatch, SetStateAction } from "react";

import "./PermitReview.scss";
import { WarningBcGovBanner } from "../../../../../../common/components/banners/WarningBcGovBanner";
Expand All @@ -24,7 +23,7 @@ import {
} from "../../../../types/PermitReviewContext";

interface PermitReviewProps {
reviewContext?: Nullable<PermitReviewContext>;
reviewContext: PermitReviewContext;
permitType?: Nullable<PermitType>;
permitNumber?: Nullable<string>;
applicationNumber?: Nullable<string>;
Expand All @@ -40,8 +39,8 @@ interface PermitReviewProps {
isAmendAction: boolean;
children?: React.ReactNode;
hasAttemptedCheckboxes: boolean;
allChecked: boolean;
setAllChecked: Dispatch<SetStateAction<boolean>>;
allConfirmed: boolean;
setAllConfirmed: (confirmed: boolean) => void;
powerUnitSubTypes?: Nullable<VehicleSubType[]>;
trailerSubTypes?: Nullable<VehicleSubType[]>;
vehicleDetails?: Nullable<PermitVehicleDetails>;
Expand Down Expand Up @@ -104,9 +103,9 @@ export const PermitReview = (props: PermitReviewProps) => {
/>

<ReviewFeeSummary
isSubmitted={props.hasAttemptedCheckboxes}
isChecked={props.allChecked}
setIsChecked={props.setAllChecked}
hasAttemptedSubmission={props.hasAttemptedCheckboxes}
areAllConfirmed={props.allConfirmed}
setAreAllConfirmed={props.setAllConfirmed}
permitType={props.permitType}
fee={props.calculatedFee}
reviewContext={props.reviewContext}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { Box, Typography } from "@mui/material";
import { Dispatch, SetStateAction } from "react";

import "./ReviewFeeSummary.scss";
import { ConfirmationCheckboxes } from "./ConfirmationCheckboxes";
import { FeeSummary } from "../../../../components/feeSummary/FeeSummary";
import { PermitType } from "../../../../types/PermitType";
import { Nullable } from "../../../../../../common/types/common";
import { PermitReviewContext } from "../../../../types/PermitReviewContext";
import { PERMIT_REVIEW_CONTEXTS, PermitReviewContext } from "../../../../types/PermitReviewContext";

export const ReviewFeeSummary = ({
isSubmitted,
isChecked,
setIsChecked,
hasAttemptedSubmission,
areAllConfirmed,
setAreAllConfirmed,
permitType,
fee,
reviewContext,
}: {
isSubmitted: boolean;
isChecked: boolean;
setIsChecked: Dispatch<SetStateAction<boolean>>;
hasAttemptedSubmission: boolean;
areAllConfirmed: boolean;
setAreAllConfirmed: (allConfirmed: boolean) => void;
permitType?: Nullable<PermitType>;
fee: string;
reviewContext?: Nullable<PermitReviewContext>;
reviewContext: PermitReviewContext;
}) => {
return (
<Box className="review-fee-summary">
Expand All @@ -33,10 +32,10 @@ export const ReviewFeeSummary = ({
<FeeSummary permitType={permitType} feeSummary={fee} />

<ConfirmationCheckboxes
isSubmitted={isSubmitted}
isChecked={isChecked}
setIsChecked={setIsChecked}
isDisabled={reviewContext === "QUEUE"}
hasAttemptedSubmission={hasAttemptedSubmission}
areAllChecked={areAllConfirmed}
setAreAllChecked={setAreAllConfirmed}
shouldDisableCheckboxes={reviewContext === PERMIT_REVIEW_CONTEXTS.QUEUE}
/>
</Box>
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ export const ApplicationInQueueReview = ({
const methods = useForm<Application>();

// For the confirmation checkboxes
const [isChecked, setIsChecked] = useState(false);
const [isSubmitted] = useState(false);
// For Applications in Queue review, confirmation checkboxes are checked and disabled by default
const [allConfirmed, setAllConfirmed] = useState(true);
const [hasAttemptedSubmission, setHasAttemptedSubmission] = useState(false);

const handleEdit = () => {
navigate(APPLICATION_QUEUE_ROUTES.EDIT(companyId, applicationData?.permitId));
Expand All @@ -69,6 +70,9 @@ export const ApplicationInQueueReview = ({
} = useApproveApplicationInQueueMutation();

const handleApprove = async (): Promise<void> => {
setHasAttemptedSubmission(true);
if (!allConfirmed) return;

await approveApplication({
applicationId: applicationData?.permitId,
companyId,
Expand All @@ -88,6 +92,9 @@ export const ApplicationInQueueReview = ({
} = useRejectApplicationInQueueMutation();

const handleReject = async (): Promise<void> => {
setHasAttemptedSubmission(true);
if (!allConfirmed) return;

await rejectApplication({
applicationId: applicationData?.permitId,
companyId,
Expand Down Expand Up @@ -131,9 +138,9 @@ export const ApplicationInQueueReview = ({
approveApplicationMutationPending={approveApplicationMutationPending}
onReject={handleReject}
rejectApplicationMutationPending={rejectApplicationMutationPending}
allChecked={isChecked}
setAllChecked={setIsChecked}
hasAttemptedCheckboxes={isSubmitted}
allConfirmed={allConfirmed}
setAllConfirmed={setAllConfirmed}
hasAttemptedCheckboxes={hasAttemptedSubmission}
powerUnitSubTypes={powerUnitSubTypesQuery.data}
trailerSubTypes={trailerSubTypesQuery.data}
vehicleDetails={applicationData?.permitData?.vehicleDetails}
Expand Down

0 comments on commit b0cbfd1

Please sign in to comment.