Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: isochrone #236

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/api/indicators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@ import type { AxiosError } from "axios";

import { instance } from "@utils";

export function getIsochrone(data: IsochroneParams): Promise<ArrayBuffer> | null {
export function calculateIsochrone(data: IsochroneParams) {
return instance
.post("indicators/isochrone", data, {
responseType: "arraybuffer",
headers: {
Accept: "application/pbf",
Accept: "application/json",
},
})
.then((response) => response.data)
.catch((err: AxiosError) => {
throw err;
});
}

export function getIsochroneResult(task_id: string, cancelToken) {
return instance.get(`indicators/result/${task_id}?return_type=grid`, {
responseType: "arraybuffer",
cancelToken,
});
}
41 changes: 38 additions & 3 deletions src/context/isochrones/isochrones-action.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type LngLat } from "react-map-gl";
import axios from "axios";

import * as Api from "@api/indicators";

Expand All @@ -13,13 +14,47 @@ import { ISOCHRONE_REQUEST_DEFAULTS } from "@constants";

import { setTravelTimeSurface } from "./isochrones-reducer";

const MAX_TRIES = 20;

let isochroneCancelToken = null;

export function fetch_isochrone_result(
task_id: string,
currentTry,
dispatch: CallableFunction,
_cancelToken
) {
if (currentTry < MAX_TRIES) {
Api.getIsochroneResult(task_id, _cancelToken).then((result) => {
if (result.status === 202) {
setTimeout(() => {
fetch_isochrone_result(task_id, currentTry + 1, dispatch, _cancelToken);
}, 1000);
} else {
if (!result) {
console.error("No result");
}
dispatch(setTravelTimeSurface(result.data));
}
});
}
}

export function fetch_isochrone(isochrone: IsochroneParams) {
if (isochroneCancelToken instanceof Function) {
isochroneCancelToken("cancelled");
isochroneCancelToken = null;
}
const CancelToken = axios.CancelToken;
const _cancelToken = new CancelToken((c) => {
isochroneCancelToken = c;
});
return (dispatch: CallableFunction) =>
dispatch(
networkStateHandler(async () => {
const response = await Api.getIsochrone(isochrone);
if (response) {
dispatch(setTravelTimeSurface(response));
const response = await Api.calculateIsochrone(isochrone);
if (response && response.task_id) {
fetch_isochrone_result(response.task_id, 1, dispatch, _cancelToken);
}
})
);
Expand Down
Loading