From ef42a5517e442bf173c711c04ebbdb9224aeae30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=BAlio=20Assis?= Date: Tue, 30 Jun 2020 12:27:59 -0300 Subject: [PATCH] chore(useReportingWizardSteps): add handle next for location step --- .../useReportingWizardSteps/handleNext.js | 30 ++++++++++++++++++- .../handleNext.test.js | 24 +++++++++++++-- .../useReportingWizardSteps.js | 3 +- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/hooks/useReportingWizardSteps/handleNext.js b/src/hooks/useReportingWizardSteps/handleNext.js index 9948d57e..1ae8b477 100644 --- a/src/hooks/useReportingWizardSteps/handleNext.js +++ b/src/hooks/useReportingWizardSteps/handleNext.js @@ -1,4 +1,7 @@ -import { createParkingReport } from "../../services/parkings" +import { + createParkingReport, + completeParkingReport, +} from "../../services/parkings" export const handleCarFrontPhoto = (parkingReportState) => { const handleSuccess = ({ data }) => { @@ -27,3 +30,28 @@ export const handleCarFrontPhoto = (parkingReportState) => { .then(handleSuccess) .catch(handleError) } + +export const handleLocationStep = (parkingReportState) => { + const handleSuccess = ({ data }) => { + const uuid = data.data.uuid + + localStorage.removeItem("PARKING_REPORT") + + return { + uuid, + } + } + + const handleError = (error) => { + return { + currentPosition: null, + } + } + const { uuid, currentPosition } = parkingReportState + const coordinates = + currentPosition.coords.latitude + "," + currentPosition.coords.longitude + + return completeParkingReport(uuid, coordinates) + .then(handleSuccess) + .catch(handleError) +} diff --git a/src/hooks/useReportingWizardSteps/handleNext.test.js b/src/hooks/useReportingWizardSteps/handleNext.test.js index 25f9fab3..2b7ba712 100644 --- a/src/hooks/useReportingWizardSteps/handleNext.test.js +++ b/src/hooks/useReportingWizardSteps/handleNext.test.js @@ -1,4 +1,4 @@ -import { handleCarFrontPhoto } from "./handleNext" +import { handleCarFrontPhoto, handleLocationStep } from "./handleNext" jest.mock("../../services/parkings") @@ -29,5 +29,25 @@ describe("useStepsNavigation", () => { ) }) - it.todo("should remove uuid from localStorage in case of invalid photo") + it.todo("should not store uuid in localStorage when some error occurred") + + it("should remove uuid from localStorage on complete parking report", async () => { + const parkingReportState = { + uuid: "uuid", + currentPosition: { + coords: { + latitude: "LAT", + longitude: "LNG", + }, + }, + } + + await handleLocationStep(parkingReportState) + + expect(window.localStorage.__proto__.removeItem).toHaveBeenCalledWith( + "PARKING_REPORT" + ) + }) + + it.todo("should not remove uuid from localStorage when some error occurred") }) diff --git a/src/hooks/useReportingWizardSteps/useReportingWizardSteps.js b/src/hooks/useReportingWizardSteps/useReportingWizardSteps.js index 70e0064a..e91757e9 100644 --- a/src/hooks/useReportingWizardSteps/useReportingWizardSteps.js +++ b/src/hooks/useReportingWizardSteps/useReportingWizardSteps.js @@ -8,7 +8,7 @@ import { isCarFrontPhotoValid } from "../../validators/isCarFrontPhotoValid" import { isCarPlateValid } from "../../validators/isCarPlateValid" import { isCurrentPositionValid } from "../../validators/isCurrentPositionValid" -import { handleCarFrontPhoto } from "./handleNext" +import { handleCarFrontPhoto, handleLocationStep } from "./handleNext" export const useReportingWizardSteps = ({ toggles }) => { let result = [] @@ -35,6 +35,7 @@ export const useReportingWizardSteps = ({ toggles }) => { label: "Enviar localização", component: LocationStep, validator: isCurrentPositionValid, + handleNext: handleLocationStep, }, { label: "Denúncia realizada",