Skip to content

Commit

Permalink
Adjust base path depending on environment, formatting, removed excess…
Browse files Browse the repository at this point in the history
…ive comments
  • Loading branch information
dzole0311 committed Dec 10, 2024
1 parent 455a563 commit 52e4b15
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions app/scripts/components/common/map/controls/hooks/use-preset-aoi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ import { useCallback, useEffect, useState } from 'react';
import axios from 'axios';
import { getAoiAppropriateFeatures } from './use-custom-aoi';

const presetFilePath = `${process.env.PUBLIC_URL ?? ''}/public/geo-data/states/`;
const presetSuffix = `.geojson`;

function usePresetAOI(selectedState) {
const [error, setError] = useState<string|null>('');
const [error, setError] = useState<string | null>('');
const [isLoading, setIsLoading] = useState<boolean>(false);
const [features, setFeatures] = useState<Feature<Polygon>[] | null>(null);

useEffect(() => {
if (!selectedState) return;
const abortController = new AbortController(); // Create an instance of AbortController
const abortController = new AbortController();

async function loadData() {
setIsLoading(true);
try {
const res = await axios.get(`${presetFilePath}${selectedState}${presetSuffix}`, {
signal: abortController.signal // Pass the abort signal to Axios
// We're dynamically adjusting the base path based on the environment:
// 1. If running in Next.js, omit "/public" from the path
// 2. For legacy instances, we include "/public" as part of the path
const basePath =
typeof window !== 'undefined'
? `/geo-data/states/`
: `${process.env.PUBLIC_URL ?? ''}/public/geo-data/states/`;

const res = await axios.get(`${basePath}${selectedState}.geojson`, {
signal: abortController.signal
});
setIsLoading(false);
const geojson = res.data;
Expand All @@ -29,14 +34,15 @@ function usePresetAOI(selectedState) {
}
const { simplifiedFeatures } = getAoiAppropriateFeatures(geojson);

setFeatures(simplifiedFeatures.map((feat, i) => ({
id: `${new Date().getTime().toString().slice(-4)}${i}`,
...feat
})));
setFeatures(
simplifiedFeatures.map((feat, i) => ({
id: `${new Date().getTime().toString().slice(-4)}${i}`,
...feat
}))
);
} catch (error) {
if (axios.isCancel(error)) {
setIsLoading(false);
// Request canceled
setError(error.message);
} else {
setError('Error: Unable to load data');
Expand All @@ -62,7 +68,6 @@ function usePresetAOI(selectedState) {
error,
reset
};

}

export default usePresetAOI;

0 comments on commit 52e4b15

Please sign in to comment.