Skip to content

Commit

Permalink
Refactoring address component
Browse files Browse the repository at this point in the history
  • Loading branch information
avenmia committed Aug 24, 2024
1 parent 0d226ad commit 5182e71
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions src/components/AddressSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const AddressSearch: React.FC = () => {
const handleSubmit = () => {
console.log("Submitted!");
const planningRegionDhhl = `${planningRegion ?? ""} ${
dhhlRegion === "Yes" ? "- DHHL" : ""
dhhlRegion === "Yes" ? "-DHHL" : ""
}`;
updateUserCensusTract.mutate({ censusTract: censusTract ?? "" });
updateUserZipCode.mutate({ zipcode: zipCode ?? "" });
Expand Down Expand Up @@ -197,45 +197,49 @@ const AddressSearch: React.FC = () => {

const setDemographicData = (lat: number, lng: number) => {
const point = turf.point([lng, lat]);
let zip = "";
let censusTract = "";
let featurePlanningRegion = "";
let dhhlRegion = "";

turf.featureEach(
ZipCodesGeojson as FeatureCollection<Polygon>,
(currentFeature, featureIndex) => {
const getFeatureProperty = (
geojson: FeatureCollection<Polygon>,
propertyName: string,
defaultValue = "Not Found",
name?: string
): string => {
let propertyValue = defaultValue;
turf.featureEach(geojson, (currentFeature) => {
if (turf.booleanPointInPolygon(point, currentFeature)) {
zip = (currentFeature.properties?.geoid20 as string) ?? "Not Found";
if (name !== "dhhl") {
propertyValue =
(currentFeature.properties?.[propertyName] as string) ??
defaultValue;
} else {
propertyValue = "Yes";
}
}
}
});
return propertyValue;
};

const zip = getFeatureProperty(
ZipCodesGeojson as FeatureCollection<Polygon>,
"geoid20"
);
turf.featureEach(
const censusTract = getFeatureProperty(
CensusTractGeojson as FeatureCollection<Polygon>,
(currentFeature, featureIndex) => {
if (turf.booleanPointInPolygon(point, currentFeature)) {
censusTract =
(currentFeature.properties?.name20 as string) ?? "Not Found";
}
}
"name20"
);
turf.featureEach(
const featurePlanningRegion = getFeatureProperty(
PlanningAreaGeojson as FeatureCollection<Polygon>,
(currentFeature, featureIndex) => {
if (turf.booleanPointInPolygon(point, currentFeature)) {
featurePlanningRegion =
(currentFeature.properties?.Name as string) ?? "Not Found";
}
}
);
turf.featureEach(
DHHLGeojson as FeatureCollection<Polygon>,
(currentFeature, featureIndex) => {
if (turf.booleanPointInPolygon(point, currentFeature)) {
dhhlRegion = "Yes";
}
}
"Name"
);
const dhhlRegion =
getFeatureProperty(
DHHLGeojson as FeatureCollection<Polygon>,
"Name",
"Not Found",
"dhhl"
) === "Not Found"
? "No"
: "Yes";

setZipCode(zip);
setCensusTract(censusTract);
Expand Down

0 comments on commit 5182e71

Please sign in to comment.