Skip to content

Commit

Permalink
chore(useReportingWizardSteps): add handle next for location step
Browse files Browse the repository at this point in the history
  • Loading branch information
tuliooassis committed Jun 30, 2020
1 parent 0960594 commit ef42a55
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
30 changes: 29 additions & 1 deletion src/hooks/useReportingWizardSteps/handleNext.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { createParkingReport } from "../../services/parkings"
import {
createParkingReport,
completeParkingReport,
} from "../../services/parkings"

export const handleCarFrontPhoto = (parkingReportState) => {
const handleSuccess = ({ data }) => {
Expand Down Expand Up @@ -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)
}
24 changes: 22 additions & 2 deletions src/hooks/useReportingWizardSteps/handleNext.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handleCarFrontPhoto } from "./handleNext"
import { handleCarFrontPhoto, handleLocationStep } from "./handleNext"

jest.mock("../../services/parkings")

Expand Down Expand Up @@ -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")
})
3 changes: 2 additions & 1 deletion src/hooks/useReportingWizardSteps/useReportingWizardSteps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -35,6 +35,7 @@ export const useReportingWizardSteps = ({ toggles }) => {
label: "Enviar localização",
component: LocationStep,
validator: isCurrentPositionValid,
handleNext: handleLocationStep,
},
{
label: "Denúncia realizada",
Expand Down

0 comments on commit ef42a55

Please sign in to comment.