Skip to content

Commit

Permalink
fix test and merge mapbox geocoder function
Browse files Browse the repository at this point in the history
  • Loading branch information
dancingfrog committed Jan 17, 2025
2 parents cdd1ab1 + 88c449b commit 9d7b205
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
7 changes: 6 additions & 1 deletion lib/@cori-risi/contexts/ApiContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { AuthTokens, JWT } from "@aws-amplify/auth";

import "./styles/ApiContextProvider.css";
import { User } from "../models";
import { autoSignOut } from "../utils";

const BASE_URL = "https://cori-data-api.ruralinnovation.us/"; // `${import.meta.env.VITE_CORI_DATA_API}`;
// TODO: From now on must pass dev/prod API url in as param to ApiContextProvider because:
Expand All @@ -42,6 +41,12 @@ type AuthSession = {
userSub?: string;
};

function autoSignOut (signOut: Function){
signOut();
window.alert("Please refresh this session by clicking the browser's reload button!");
(window as any).location = window.location.protocol + "//" + window.location.host + window.location.pathname;
}

/**
* This is the interface/type of the [`ApiContext`](../variables/ApiContext.md).
*
Expand Down
50 changes: 50 additions & 0 deletions lib/@cori-risi/mapbox/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import axios from "axios";

export async function mapboxGeocode (api_key: string, text: string) {

const search_request = 'https://api.mapbox.com/search/geocode/v6/forward'

// console.log("GEOCODE EARTH API: ", import.meta.env.VITE_GEOCODE_TOKEN);
console.log("MAPBOX API TOKEN: ", api_key);

const res = await axios.get(search_request, //auto_request,
{
params: {
access_token: api_key, // api_key
q: text // text
}
}
)

// console.log("GEOCODE EARTH API: ", res);
console.log("MAPBOX API: ", res);

const found_features = res.data.features
.filter((f: any) => (f.hasOwnProperty("geometry") && f.geometry.type === "Point"));

console.log("found_features: ", found_features);

return found_features
.filter((item: any) => {
return item.hasOwnProperty("properties")
&& item["properties"].hasOwnProperty("full_address")
&& item["properties"].hasOwnProperty("context")
&& item["properties"]["context"].hasOwnProperty("country")
&& item["properties"]["context"]["country"].hasOwnProperty("country_code")
&& item["properties"]["context"]["country"]["country_code"]
.match(/(?:US|AS|GU|MP|PR|VI)/) !== null
&& "region" in item["properties"]["context"]
&& "postcode" in item["properties"]["context"];
})
.map((item: any) => {
console.log(item);
const properties = item["properties"];
properties["label"] = item["properties"]["full_address"];
properties['props'] = JSON.stringify(properties);

return {
...item,
properties
};
});
}
6 changes: 0 additions & 6 deletions lib/@cori-risi/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,9 +697,3 @@ export const getMetricText = (data: d3.DSVRowArray<string>, metric: string): {wh
}

}

export function autoSignOut (signOut: Function){
signOut();
window.alert("Please refresh this session by clicking the browser's reload button!");
// (window as any).location = window.location.protocol + "//" + window.location.host + window.location.pathname;
}
1 change: 1 addition & 0 deletions lib/cori.data.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export { default as CategoricalLegend } from "./@cori-risi/components/Categorica
export { default as GeocoderControl } from "./@cori-risi/components/GeocoderControl";
export { default as HoverInfo, HoverObject } from "./@cori-risi/components/HoverInfo";

export { mapboxGeocode } from "./@cori-risi/mapbox";
import MAP_STYLE from "./@cori-risi/mapbox/styles/ruralinno/cl010e7b7001p15pe3l0306hv/style.json";

/**
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"autoprefixer": "^10.4.19",
"d3": "^7.9.0",
"identity-obj-proxy": "^3.0.0",
"jest-environment-jsdom": "^29.7.0",
"path": "^0.12.7",
Expand Down

0 comments on commit 9d7b205

Please sign in to comment.