Skip to content

Commit

Permalink
Issue 628 - Fix public survey submission (#630)
Browse files Browse the repository at this point in the history
* Add lat long to public survey submit, remove saving snackbar for public, update test

* Send null on getLocation error
  • Loading branch information
Andrewy-gh authored Oct 9, 2024
1 parent 3980ca2 commit 7a32f7f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 64 deletions.
50 changes: 31 additions & 19 deletions frontend/front/src/components/SurveyComponent/SurveyComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ 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 {
buildDefaultDataFromSurveyStructure,
buildSurveyCacheKey,
surveyRenderRules,
} from "../../util/surveyUtils";
import { PUBLIC_ROUTE } from "../../routing/routes";
import { AddressComponent } from "../AddressUtils";
import Loader from "../Loader";
import ConditionalQuestion from "./ConditionalQuestion";
Expand All @@ -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);
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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) => {
Expand All @@ -169,7 +182,6 @@ const SurveyComponent = ({
surveyData,
surveyId,
activeHome.id,
clearCache,
surveyorPosition
);
if (!!data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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());
Expand All @@ -46,14 +48,15 @@ describe("SurveyComponent", () => {
);

render(
<SurveyComponent
submitSurvey={jest.fn()}
isLoading={false}
activeHome={DEFAULT_TEST_HOME}
surveyId={DEFAULT_TEST_SURVEY.id}
formSpacing={5}
/>,
{}
<MemoryRouter>
<SurveyComponent
submitSurvey={jest.fn()}
isLoading={false}
activeHome={DEFAULT_TEST_HOME}
surveyId={DEFAULT_TEST_SURVEY.id}
formSpacing={5}
/>
</MemoryRouter>
);

// eslint-disable-next-line testing-library/no-node-access
Expand All @@ -65,14 +68,15 @@ describe("SurveyComponent", () => {

it("should cache data when form is edited", () => {
render(
<SurveyComponent
submitSurvey={jest.fn()}
isLoading={false}
activeHome={DEFAULT_TEST_HOME}
surveyId={DEFAULT_TEST_SURVEY.id}
formSpacing={5}
/>,
{}
<MemoryRouter>
<SurveyComponent
submitSurvey={jest.fn()}
isLoading={false}
activeHome={DEFAULT_TEST_HOME}
surveyId={DEFAULT_TEST_SURVEY.id}
formSpacing={5}
/>
</MemoryRouter>
);

fireEvent.change(screen.getAllByRole("textbox")[0], {
Expand Down Expand Up @@ -112,14 +116,15 @@ describe("SurveyComponent", () => {
);

render(
<SurveyComponent
submitSurvey={mockSubmit}
isLoading={false}
activeHome={DEFAULT_TEST_HOME}
surveyId={DEFAULT_TEST_SURVEY.id}
formSpacing={5}
/>,
{}
<MemoryRouter>
<SurveyComponent
submitSurvey={mockSubmit}
isLoading={false}
activeHome={DEFAULT_TEST_HOME}
surveyId={DEFAULT_TEST_SURVEY.id}
formSpacing={5}
/>
</MemoryRouter>
);

const submitButton = screen.getByRole("button", { name: /submit/i });
Expand Down Expand Up @@ -154,14 +159,15 @@ describe("SurveyComponent", () => {
const mockSubmit = jest.fn(() => Promise.resolve({ error: "oh no!" }));

render(
<SurveyComponent
submitSurvey={mockSubmit}
isLoading={false}
activeHome={DEFAULT_TEST_HOME}
surveyId={DEFAULT_TEST_SURVEY.id}
formSpacing={5}
/>,
{}
<MemoryRouter>
<SurveyComponent
submitSurvey={mockSubmit}
isLoading={false}
activeHome={DEFAULT_TEST_HOME}
surveyId={DEFAULT_TEST_SURVEY.id}
formSpacing={5}
/>
</MemoryRouter>
);

const submitButton = screen.getByRole("button", { name: /submit/i });
Expand All @@ -175,14 +181,15 @@ describe("SurveyComponent", () => {
const errSpy = jest.spyOn(console, "error");

const { container } = render(
<SurveyComponent
submitSurvey={jest.fn()}
isLoading={false}
activeHome={DEFAULT_TEST_HOME}
surveyId={DEFAULT_TEST_SURVEY.id}
formSpacing={5}
/>,
{}
<MemoryRouter>
<SurveyComponent
submitSurvey={jest.fn()}
isLoading={false}
activeHome={DEFAULT_TEST_HOME}
surveyId={DEFAULT_TEST_SURVEY.id}
formSpacing={5}
/>
</MemoryRouter>
);

expect(container).toMatchSnapshot();
Expand Down
7 changes: 5 additions & 2 deletions frontend/front/src/pages/Public/Pages/SurveyPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions frontend/front/src/util/surveyUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7a32f7f

Please sign in to comment.