From 7a32f7f99871223def5e9d7d0ab0d6d292e68205 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 8 Oct 2024 20:02:10 -0400 Subject: [PATCH] Issue 628 - Fix public survey submission (#630) * Add lat long to public survey submit, remove saving snackbar for public, update test * Send null on getLocation error --- .../SurveyComponent/SurveyComponent.js | 50 +++++++---- .../__tests__/SurveyComponent.test.js | 87 ++++++++++--------- .../src/pages/Public/Pages/SurveyPage.js | 7 +- .../Surveyor/houseProfile/HouseProfile.js | 2 +- frontend/front/src/util/surveyUtils.js | 8 +- 5 files changed, 90 insertions(+), 64 deletions(-) diff --git a/frontend/front/src/components/SurveyComponent/SurveyComponent.js b/frontend/front/src/components/SurveyComponent/SurveyComponent.js index 3213146e..6a3ef276 100644 --- a/frontend/front/src/components/SurveyComponent/SurveyComponent.js +++ b/frontend/front/src/components/SurveyComponent/SurveyComponent.js @@ -7,7 +7,7 @@ import React, { useState, } from "react"; import { useForm } from "react-hook-form"; -import { useNavigate } from "react-router-dom"; +import { useLocation, useNavigate } from "react-router-dom"; import { useDebouncedCallback } from "use-debounce"; import { useGetSurveyStructureQuery } from "../../api/apiSlice"; import { @@ -15,6 +15,7 @@ import { buildSurveyCacheKey, surveyRenderRules, } from "../../util/surveyUtils"; +import { PUBLIC_ROUTE } from "../../routing/routes"; import { AddressComponent } from "../AddressUtils"; import Loader from "../Loader"; import ConditionalQuestion from "./ConditionalQuestion"; @@ -38,6 +39,8 @@ const SurveyComponent = ({ styles = {}, conditionalRender, }) => { + const location = useLocation(); + const isPublicSurvey = location.pathname.startsWith(PUBLIC_ROUTE); const navigate = useNavigate(); const [saving, setSaving] = useState(false); const [errSnackBarOpen, setErrSnackBarOpen] = useState(false); @@ -86,14 +89,18 @@ const SurveyComponent = ({ // to update the data in the cache const formSubscription = watch((value) => { localStorage.setItem(cacheKey, JSON.stringify(value)); - debouncedSetSaving(true); + if (!isPublicSurvey) { + debouncedSetSaving(true); + } }); return () => { formSubscription.unsubscribe(); - debouncedSetSaving(false); + if (!isPublicSurvey) { + debouncedSetSaving(false); + } }; - }, [cacheKey, watch, debouncedSetSaving]); + }, [cacheKey, watch, debouncedSetSaving, isPublicSurvey]); const closeSnackbar = (event, reason) => { if (reason === "clickaway") { @@ -144,22 +151,28 @@ const SurveyComponent = ({ ); const getLocationCoords = () => { - return new Promise((resolve, reject) => { - navigator.geolocation.getCurrentPosition( - (pos) => { - const crd = pos.coords; + return ( + !isPublicSurvey && + new Promise((resolve, reject) => { + navigator.geolocation.getCurrentPosition( + (pos) => { + const crd = pos.coords; - resolve({ latitude: crd.latitude, longitude: crd.longitude }); - }, - (err) => { - if (err.code === 1) { - reject({ error_code: err.code, message: err.message }); - } else { - resolve({ latitude: "not available", longitude: "not available" }); + resolve({ latitude: crd.latitude, longitude: crd.longitude }); + }, + (err) => { + if (err.code === 1) { + reject({ error_code: err.code, message: err.message }); + } else { + resolve({ + latitude: null, + longitude: null, + }); + } } - } - ); - }); + ); + }) + ); }; const surveySubmit = async (surveyData) => { @@ -169,7 +182,6 @@ const SurveyComponent = ({ surveyData, surveyId, activeHome.id, - clearCache, surveyorPosition ); if (!!data) { diff --git a/frontend/front/src/components/SurveyComponent/__tests__/SurveyComponent.test.js b/frontend/front/src/components/SurveyComponent/__tests__/SurveyComponent.test.js index aa20509d..c6cdaa6d 100644 --- a/frontend/front/src/components/SurveyComponent/__tests__/SurveyComponent.test.js +++ b/frontend/front/src/components/SurveyComponent/__tests__/SurveyComponent.test.js @@ -2,6 +2,7 @@ import * as router from "react-router"; import * as apiSlice from "../../../api/apiSlice"; import { fireEvent, render, screen, waitFor } from "@testing-library/react"; +import { MemoryRouter } from "react-router-dom"; // <-- Add MemoryRouter import { buildDefaultDataFromSurveyStructure, buildSurveyCacheKey, @@ -20,6 +21,7 @@ const DEFAULT_TEST_SURVEY_CACHE_KEY = buildSurveyCacheKey( ); const DEFAULT_TEST_DEFAULT_SURVEY_DATA = buildDefaultDataFromSurveyStructure(DEFAULT_TEST_SURVEY); + describe("SurveyComponent", () => { beforeEach(() => { jest.spyOn(router, "useNavigate").mockImplementation(() => jest.fn()); @@ -46,14 +48,15 @@ describe("SurveyComponent", () => { ); render( - , - {} + + + ); // eslint-disable-next-line testing-library/no-node-access @@ -65,14 +68,15 @@ describe("SurveyComponent", () => { it("should cache data when form is edited", () => { render( - , - {} + + + ); fireEvent.change(screen.getAllByRole("textbox")[0], { @@ -112,14 +116,15 @@ describe("SurveyComponent", () => { ); render( - , - {} + + + ); const submitButton = screen.getByRole("button", { name: /submit/i }); @@ -154,14 +159,15 @@ describe("SurveyComponent", () => { const mockSubmit = jest.fn(() => Promise.resolve({ error: "oh no!" })); render( - , - {} + + + ); const submitButton = screen.getByRole("button", { name: /submit/i }); @@ -175,14 +181,15 @@ describe("SurveyComponent", () => { const errSpy = jest.spyOn(console, "error"); const { container } = render( - , - {} + + + ); expect(container).toMatchSnapshot(); diff --git a/frontend/front/src/pages/Public/Pages/SurveyPage.js b/frontend/front/src/pages/Public/Pages/SurveyPage.js index 8e1170f2..a38e65cb 100644 --- a/frontend/front/src/pages/Public/Pages/SurveyPage.js +++ b/frontend/front/src/pages/Public/Pages/SurveyPage.js @@ -71,10 +71,13 @@ export const SurveyPage = () => { ); const handleAddSurveyVisit = useCallback( - async (answers, surveyId, homeId) => { + async (answers, surveyId, homeId, _) => { const recaptcha = await getReCaptchaToken("create_survey"); const surveyVisit = await addSurveyVisit({ - surveyVisit: buildSurveyVisitData(answers, homeId, surveyId), + surveyVisit: buildSurveyVisitData(answers, homeId, surveyId, null, { + latitude: null, + longitude: null, + }), recaptcha, }); return surveyVisit; diff --git a/frontend/front/src/pages/Surveyor/houseProfile/HouseProfile.js b/frontend/front/src/pages/Surveyor/houseProfile/HouseProfile.js index 6e6d8ff3..253d8baf 100644 --- a/frontend/front/src/pages/Surveyor/houseProfile/HouseProfile.js +++ b/frontend/front/src/pages/Surveyor/houseProfile/HouseProfile.js @@ -46,7 +46,7 @@ const HouseProfile = () => { }, [homeId, isSurveyVisitSuccess, navigate]); const submitSurvey = useCallback( - async (answers, surveyId, _, __, surveyorPosition) => { + async (answers, surveyId, _, surveyorPosition) => { const surveyVisit = await addSurveyVisit({ surveyVisit: buildSurveyVisitData( answers, diff --git a/frontend/front/src/util/surveyUtils.js b/frontend/front/src/util/surveyUtils.js index 952e5a77..c939df2d 100644 --- a/frontend/front/src/util/surveyUtils.js +++ b/frontend/front/src/util/surveyUtils.js @@ -58,8 +58,12 @@ export const buildSurveyVisitData = ( survey_visit: { home_id: homeId, surveyor_id: surveyorId, - latitude: `${surveyorPosition.latitude.toString()}`, - longitude: `${surveyorPosition.longitude.toString()}`, + latitude: surveyorPosition.latitude + ? `${surveyorPosition.latitude.toString()}` + : null, + longitude: surveyorPosition.longitude + ? `${surveyorPosition.longitude.toString()}` + : null, survey_response_attributes: { survey_id: surveyId, survey_answers_attributes: answersObject,