Skip to content

Commit 1f31b46

Browse files
committed
Refactor PlaceAddress.tsx
1 parent 2d2dbfa commit 1f31b46

File tree

6 files changed

+33
-52
lines changed

6 files changed

+33
-52
lines changed

package-lock.json

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
"ts-node": "^10.9.1",
153153
"ts-node-dev": "^2.0.0",
154154
"ttag-cli": "^1.10.11",
155-
"typescript": "^5.3.3"
155+
"typescript": "^5.5.3"
156156
},
157157
"scripts": {
158158
"dev": "npx ts-node-dev --watch 'src/server/*.ts,src/lib/env.ts,src/app/router.ts' --ignore-watch 'src/.next/*' -P src/server/tsconfig.json --respawn --icu-data-dir=node_modules/full-icu src/server/server.ts",

src/components/NodeToolbar/IconButtonList/PlaceAddress.tsx

+15-23
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,10 @@ import getAddressString from "../../../lib/getAddressString";
99
import getAddressStringFromA11yJSONFeature from "../../../lib/getAddressStringFromA11yJSONFeature";
1010
import openButtonCaption from "../../../lib/openButtonCaption";
1111
import { UAResult } from "../../../lib/userAgent";
12-
import { fetchJSON, generateGetPlaceInfoURL } from "../../helper";
12+
import fetchJSON from "../../fetchJSON";
1313
import PlaceIcon from "../../icons/actions/Place";
1414
import RouteIcon from "../../icons/actions/Route";
1515

16-
function useParentPlaceInfo(parentPlaceInfoId: string) {
17-
const appContext = React.useContext(AppContext);
18-
const baseUrl = generateGetPlaceInfoURL({
19-
baseUrl: appContext.baseUrl,
20-
placeInfoId: parentPlaceInfoId,
21-
appToken: appContext.app.tokenString,
22-
});
23-
const { data, error } = useSWR<any>(baseUrl, fetchJSON);
24-
return { data, error };
25-
}
26-
2716
function getAddressForACProperties(properties: AccessibilityCloudProperties): string | null {
2817
if (typeof properties.address === "string") return properties.address;
2918
if (typeof properties.address === "object") {
@@ -41,14 +30,7 @@ function getAddressForACProperties(properties: AccessibilityCloudProperties): st
4130
return null;
4231
}
4332

44-
function useAddressForProperties(properties: NodeProperties): string | null {
45-
if ((properties as any)?.parentPlaceInfo) {
46-
const parentPlaceInfo = useParentPlaceInfo((properties as any)?.parentPlaceInfo);
47-
// Use parentPlaceInfo to get the address, if necessary
48-
console.log("data: " + parentPlaceInfo.data);
49-
console.log("error: " + parentPlaceInfo.error);
50-
// return getAddressForACProperties(properties);
51-
}
33+
function getAddressForProperties(properties: NodeProperties): string | null {
5234
if (!isWheelmapProperties(properties)) {
5335
return getAddressForACProperties(properties);
5436
}
@@ -61,11 +43,21 @@ type Props = {
6143
userAgent: UAResult;
6244
};
6345

64-
const PlaceAddress: React.FC<Props> = ({ feature, category, userAgent }) => {
46+
function PlaceAddress({ feature, category, userAgent }: Props) {
6547
const placeName = placeNameFor(feature.properties, category);
6648
const openInMaps = generateMapsUrl(userAgent, feature, placeName);
6749
const showOnOsmUrl = generateShowOnOsmUrl(feature);
68-
const address = useAddressForProperties(feature.properties);
50+
51+
const appContext = React.useContext(AppContext);
52+
const baseUrl = appContext?.baseUrl;
53+
const appToken = appContext.app.tokenString;
54+
55+
const { properties } = feature;
56+
const parentPlaceInfoId = isWheelmapProperties(properties) ? undefined : properties.parentPlaceInfoId;
57+
const url = parentPlaceInfoId && `${baseUrl}/place-infos/${parentPlaceInfoId}.json?appToken=${appToken}`;
58+
const { data: parentPlaceInfo } = useSWR<any>(url, fetchJSON);
59+
const propertiesToUseForAddress = parentPlaceInfo?.properties ?? feature.properties;
60+
const address = getAddressForProperties(propertiesToUseForAddress);
6961

7062
if (!feature || !feature.properties) return null;
7163
const addressString = address && address.replace(/,$/, "").replace(/^,/, "");
@@ -86,6 +78,6 @@ const PlaceAddress: React.FC<Props> = ({ feature, category, userAgent }) => {
8678
)}
8779
</React.Fragment>
8880
);
89-
};
81+
}
9082

9183
export default PlaceAddress;

src/components/fetchJSON.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default async function fetchJSON(url: RequestInfo | URL) {
2+
const response = await fetch(url);
3+
if (!response.ok) {
4+
throw new Error(`Network response was not OK (${response.status}).`);
5+
}
6+
const json = await response.json();
7+
return json;
8+
}

src/components/helper.tsx

-22
This file was deleted.

tsconfig.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
],
99
"allowJs": true,
1010
"skipLibCheck": true,
11-
"strict": false,
11+
"strict": true,
1212
"forceConsistentCasingInFileNames": true,
1313
"noEmit": true,
1414
"incremental": true,
@@ -17,7 +17,9 @@
1717
"resolveJsonModule": true,
1818
"isolatedModules": true,
1919
"jsx": "preserve",
20-
"moduleResolution": "node"
20+
"moduleResolution": "node",
21+
"strictNullChecks": true,
22+
"strictFunctionTypes": true,
2123
},
2224
"include": [
2325
"next-env.d.ts",
@@ -27,4 +29,4 @@
2729
"exclude": [
2830
"node_modules"
2931
]
30-
}
32+
}

0 commit comments

Comments
 (0)