-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import cityData from "public/swiss-city-topo.json"; | ||
import { useQuery } from "react-query"; | ||
import { previewSourceUrl } from "src/shared"; | ||
import * as topojson from "topojson"; | ||
import { Value } from "../components/Generator/context"; | ||
import { MultiPolygon } from "geojson"; | ||
|
||
const getGeoData = (json: any) => { | ||
const geoData = { | ||
country: json.objects?.country | ||
? topojson.feature(json, json.objects.country) | ||
: undefined, | ||
cantons: json.objects?.cantons | ||
? topojson.feature(json, json.objects.cantons) | ||
: undefined, | ||
neighbors: json.objects?.cantons | ||
? topojson.neighbors(json.objects.cantons.geometries) | ||
: undefined, | ||
municipalities: json.objects?.municipalities | ||
? topojson.feature(json, json.objects.municipalities as MultiPolygon) | ||
: undefined, | ||
lakes: json.objects?.lakes | ||
? topojson.feature(json, json.objects.lakes) | ||
: undefined, | ||
city: cityData | ||
? topojson.feature(cityData as any, cityData.objects["swiss-city"] as any) | ||
: undefined, | ||
}; | ||
return geoData; | ||
}; | ||
|
||
const fetchGeoData = (options: Value["state"]["options"]) => { | ||
return fetch(previewSourceUrl(options, "v0")) | ||
.then((res) => res.json()) | ||
.then((json) => { | ||
return getGeoData(json); | ||
}); | ||
}; | ||
|
||
export const useGeoData = ( | ||
options: Omit<Value["state"]["options"], "year"> & { | ||
year: undefined | string; | ||
}, | ||
queryOptions?: { | ||
enabled?: boolean; | ||
} | ||
) => { | ||
const query = useQuery({ | ||
queryKey: ["geoData", options.year!, options.simplify, ...options.shapes], | ||
queryFn: () => | ||
options ? fetchGeoData(options as Value["state"]["options"]) : undefined, | ||
enabled: options && options.year ? queryOptions?.enabled : false, | ||
}); | ||
|
||
return { | ||
...query, | ||
// TODO replace by initialData once react-query is upgraded to v4 | ||
// https://github.com/TanStack/query/discussions/1331#discussioncomment-4802682 | ||
data: query.data ?? { | ||
country: undefined, | ||
cantons: undefined, | ||
neighbors: undefined as Array<number[]> | undefined, | ||
municipalities: undefined, | ||
lakes: undefined, | ||
city: undefined, | ||
}, | ||
}; | ||
}; |