-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Paper Application not saving lat long (#3828)
* fix: add map ui to include lat and long in paper application preference options * refactor: remove redundant class * fix: trigger geocoding validation on listing update
- Loading branch information
1 parent
0961242
commit 3ea47ed
Showing
7 changed files
with
245 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
sites/partners/src/components/applications/PaperApplicationForm/MultiselectQuestionsMap.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import React, { useEffect, useState } from "react" | ||
import { FieldGroup, LatitudeLongitude, ListingMap, t } from "@bloom-housing/ui-components" | ||
import { FieldValue, Grid } from "@bloom-housing/ui-seeds" | ||
import { useFormContext, useWatch } from "react-hook-form" | ||
import { GeocodeService as GeocodeServiceType } from "@mapbox/mapbox-sdk/services/geocoding" | ||
|
||
interface MapBoxFeature { | ||
center: number[] // Index 0: longitude, Index 1: latitude | ||
} | ||
|
||
interface MapboxApiResponseBody { | ||
features: MapBoxFeature[] | ||
} | ||
|
||
interface MapboxApiResponse { | ||
body: MapboxApiResponseBody | ||
} | ||
|
||
interface BuildingAddress { | ||
city: string | ||
state: string | ||
street: string | ||
zipCode: string | ||
longitude?: number | ||
latitude?: number | ||
} | ||
|
||
type MultiselectQuestionsMapProps = { | ||
geocodingClient: GeocodeServiceType | ||
dataKey: string | ||
} | ||
|
||
const MultiselectQuestionsMap = ({ geocodingClient, dataKey }: MultiselectQuestionsMapProps) => { | ||
const [customMapPositionChosen, setCustomMapPositionChosen] = useState(true) | ||
const formMethods = useFormContext() | ||
|
||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const { register, control, getValues, setValue, watch } = formMethods | ||
|
||
const buildingAddress: BuildingAddress = useWatch({ | ||
control, | ||
name: `${dataKey}-address`, | ||
}) | ||
const mapPinPosition = useWatch({ | ||
control, | ||
name: `${dataKey}-mapPinPosition`, | ||
}) | ||
|
||
const [latLong, setLatLong] = useState<LatitudeLongitude>({ | ||
latitude: buildingAddress?.latitude ?? null, | ||
longitude: buildingAddress?.longitude ?? null, | ||
}) | ||
|
||
const displayMapPreview = () => { | ||
return ( | ||
buildingAddress?.city && | ||
buildingAddress?.state && | ||
buildingAddress?.street && | ||
buildingAddress?.zipCode && | ||
buildingAddress?.zipCode.length >= 5 | ||
) | ||
} | ||
|
||
const getNewLatLong = () => { | ||
if ( | ||
buildingAddress?.city && | ||
buildingAddress?.state && | ||
buildingAddress?.street && | ||
buildingAddress?.zipCode && | ||
geocodingClient | ||
) { | ||
geocodingClient | ||
.forwardGeocode({ | ||
query: `${buildingAddress.street}, ${buildingAddress.city}, ${buildingAddress.state}, ${buildingAddress.zipCode}`, | ||
limit: 1, | ||
}) | ||
.send() | ||
.then((response: MapboxApiResponse) => { | ||
setLatLong({ | ||
latitude: response.body.features[0].center[1], | ||
longitude: response.body.features[0].center[0], | ||
}) | ||
}) | ||
.catch((err) => console.error(`Error calling Mapbox API: ${err}`)) | ||
} | ||
} | ||
|
||
if ( | ||
getValues(`${dataKey}-address.latitude`) !== latLong.latitude || | ||
getValues(`${dataKey}-address.longitude`) !== latLong.longitude | ||
) { | ||
setValue(`${dataKey}-address.latitude`, latLong.latitude) | ||
setValue(`${dataKey}-address.longitude`, latLong.longitude) | ||
} | ||
|
||
useEffect(() => { | ||
if (watch(dataKey)) { | ||
register(`${dataKey}-address.longitude`) | ||
register(`${dataKey}-address.latitude`) | ||
} | ||
}, [dataKey, register, setValue, watch]) | ||
|
||
useEffect(() => { | ||
let timeout | ||
if (!customMapPositionChosen || mapPinPosition === "automatic") { | ||
timeout = setTimeout(() => { | ||
getNewLatLong() | ||
}, 1000) | ||
} | ||
return () => { | ||
clearTimeout(timeout) | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [ | ||
buildingAddress?.city, | ||
buildingAddress?.state, | ||
buildingAddress?.street, | ||
buildingAddress?.zipCode, | ||
]) | ||
|
||
useEffect(() => { | ||
if (mapPinPosition === "automatic") { | ||
getNewLatLong() | ||
setCustomMapPositionChosen(false) | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [mapPinPosition]) | ||
|
||
return ( | ||
<> | ||
<Grid.Row> | ||
<FieldValue label={t("listings.mapPreview")}> | ||
{displayMapPreview() ? ( | ||
<ListingMap | ||
address={{ | ||
city: buildingAddress.city, | ||
state: buildingAddress.state, | ||
street: buildingAddress.street, | ||
zipCode: buildingAddress.zipCode, | ||
latitude: buildingAddress.latitude, | ||
longitude: buildingAddress.longitude, | ||
}} | ||
enableCustomPinPositioning={mapPinPosition === "custom"} | ||
setCustomMapPositionChosen={setCustomMapPositionChosen} | ||
setLatLong={setLatLong} | ||
/> | ||
) : ( | ||
<div | ||
className={"w-full bg-gray-400 p-3 flex items-center justify-center"} | ||
style={{ height: "400px" }} | ||
> | ||
{t("listings.mapPreviewNoAddress")} | ||
</div> | ||
)} | ||
</FieldValue> | ||
</Grid.Row> | ||
<Grid.Row> | ||
<p className="field-label m-4 ml-0">{t("listings.mapPinPosition")}</p> | ||
</Grid.Row> | ||
<Grid.Row> | ||
<FieldGroup | ||
name={`${dataKey}-mapPinPosition`} | ||
type="radio" | ||
fieldGroupClassName={"flex-col"} | ||
fieldClassName={"ml-0"} | ||
register={register} | ||
fields={[ | ||
{ | ||
label: t("t.automatic"), | ||
value: "automatic", | ||
id: `${dataKey}-mapPinPosition-automatic`, | ||
note: t("listings.mapPinAutomaticDescription"), | ||
defaultChecked: mapPinPosition === "automatic" || mapPinPosition === undefined, | ||
}, | ||
{ | ||
label: t("t.custom"), | ||
value: "custom", | ||
id: `${dataKey}-mapPinPosition-custom`, | ||
note: t("listings.mapPinCustomDescription"), | ||
defaultChecked: mapPinPosition === "custom", | ||
}, | ||
]} | ||
/> | ||
</Grid.Row> | ||
</> | ||
) | ||
} | ||
|
||
export default MultiselectQuestionsMap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters