Skip to content

Commit

Permalink
Merge branch 'main' into tbardini/i18n-portuguese-review
Browse files Browse the repository at this point in the history
  • Loading branch information
mzagaja authored Oct 16, 2024
2 parents a5c3f90 + 1a7cb4d commit 9eec169
Showing 1 changed file with 25 additions and 58 deletions.
83 changes: 25 additions & 58 deletions frontend/front/src/pages/Public/Pages/SurveyPage.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { Alert, Box, Container, Snackbar, Stack } from "@mui/material";
import React, { useCallback, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";

import {
RECAPTCHA_ACTION_PUBLIC_SURVEY,
useGetReCAPTCHAToken,
} from "../../../components/ReCaptcha";
import {
useCreateHomeMutation,
useCreateSurveyVisitMutation,
} from "../../../api/apiSlice";
import {
RECAPTCHA_ACTION_PUBLIC_SURVEY,
useGetReCAPTCHAToken,
} from "../../../components/ReCaptcha";

import { AddressValidatorComponent } from "../Components/AddressValidatorComponent";
import Heading1BlueBgGround from "../Components/Typography/Heading1BlueBgGround";
import { HeatPumpFade } from "../../../components/HeatPumpFade";
import { HeatPumpSlide } from "../../../components/HeatPumpSlide";
import { buildSurveyVisitData } from "../../../util/surveyUtils";
import { AddressValidatorComponent } from "../Components/AddressValidatorComponent";
import { PublicSurvey } from "../Components/PublicSurvey";
import { ThanksForSubmission } from "../Components/ThanksForSubmission";
import { buildSurveyVisitData } from "../../../util/surveyUtils";
import CanonicalizationLoader, {
CANONICALIZED,
UNCANONICALIZED,
UNRECOGNIZED,
VALIDATION_ERROR,
} from "../Components/CanonicalizationLoader";
import Heading1BlueBgGround from "../Components/Typography/Heading1BlueBgGround";

const STEP_ADDRESS = "PHASE_ADDRESS";
const STEP_SURVEY = "PHASE_SURVEY";
Expand All @@ -35,7 +29,7 @@ const STEP_THANKS = "PHASE_THANKS";
* This page should handle all API calls so that component switching is easier to control
*/
export const SurveyPage = () => {
const [validationStatus, setValidationStatus] = useState();
const [step, setStep] = useState(STEP_ADDRESS);

const { t } = useTranslation();

Expand All @@ -49,6 +43,7 @@ export const SurveyPage = () => {
data: createHomeData,
error: createHomeError,
isLoading: isCreateHomeLoading,
isSuccess: isCreateHomeSucccess,
},
] = useCreateHomeMutation();

Expand All @@ -57,7 +52,7 @@ export const SurveyPage = () => {
{
isLoading: isSurveyVisitLoading,
error: surveyVisitError,
isSuccess: isSurveyVisitSuccess,
isSuccess: isSurveyVisitSucess,
},
] = useCreateSurveyVisitMutation();

Expand All @@ -70,6 +65,15 @@ export const SurveyPage = () => {
[createHome, getReCaptchaToken]
);

useEffect(() => {
if (isCreateHomeSucccess) {
setStep(STEP_SURVEY);
}
if (isSurveyVisitSucess) {
setStep(STEP_THANKS);
}
}, [isCreateHomeSucccess, isSurveyVisitSucess]);

const handleAddSurveyVisit = useCallback(
async (answers, surveyId, homeId, _) => {
const recaptcha = await getReCaptchaToken("create_survey");
Expand All @@ -80,36 +84,14 @@ export const SurveyPage = () => {
}),
recaptcha,
});
if (isSurveyVisitSucess) {
setStep(STEP_THANKS);
}
return surveyVisit;
},
[addSurveyVisit, getReCaptchaToken]
[addSurveyVisit, isSurveyVisitSucess, getReCaptchaToken]
);

const isCanonicalized = validationStatus === CANONICALIZED;

const step = useMemo(() => {
if (!isCanonicalized && !isSurveyVisitSuccess) {
return STEP_ADDRESS;
} else if (isSurveyVisitSuccess) {
return STEP_THANKS;
} else if (!!createHomeData && isCanonicalized) {
return STEP_SURVEY;
}
}, [createHomeData, isSurveyVisitSuccess, isCanonicalized]);

const showValidationLoader = useMemo(() => {
if (!createHomeData?.id) return false;
if (step !== STEP_ADDRESS) return false;
if (validationStatus === UNCANONICALIZED || validationStatus === undefined)
return true;
return false; // For completed validations (unrecognized or canonicalized).
}, [createHomeData, validationStatus, step]);

// Reset validation status on resubmission.
useEffect(() => {
if (isCreateHomeLoading) setValidationStatus(undefined);
}, [isCreateHomeLoading]);

const pageContent = useCallback(
() => (
<>
Expand Down Expand Up @@ -139,25 +121,12 @@ export const SurveyPage = () => {
<ThanksForSubmission />
</HeatPumpSlide>

<HeatPumpFade show={showValidationLoader}>
<CanonicalizationLoader
homeId={createHomeData?.id}
onResolved={setValidationStatus}
/>
</HeatPumpFade>

<Snackbar open={!!createHomeError}>
<Alert severity="error">{t("public.survey.alert1")}</Alert>
</Snackbar>
<Snackbar open={!!surveyVisitError}>
<Alert severity="error">{t("public.survey.alert2")}</Alert>
</Snackbar>
<Snackbar open={validationStatus === UNRECOGNIZED}>
<Alert severity="error">{t("public.survey.alert3")}</Alert>
</Snackbar>
<Snackbar open={validationStatus === VALIDATION_ERROR}>
<Alert severity="error">{t("public.survey.alert4")}</Alert>
</Snackbar>
</>
),
[
Expand All @@ -169,8 +138,6 @@ export const SurveyPage = () => {
isSurveyVisitLoading,
step,
surveyVisitError,
showValidationLoader,
validationStatus,
t,
]
);
Expand Down

0 comments on commit 9eec169

Please sign in to comment.