Skip to content

Commit

Permalink
feat: add fetchTripPlan function to fetch trip planning api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmedhossamdev committed Nov 13, 2024
1 parent 320ebd0 commit 434893f
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/components/trip-planner/TripPlan.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
}
}, 500);
async function geocodeLocation(location) {
async function geocodeLocation(locationName) {
const response = await fetch(
`/api/oba/google-geocode-location?query=${encodeURIComponent(location)}`
`/api/oba/google-geocode-location?query=${encodeURIComponent(locationName)}`
);
if (!response.ok) {
Expand Down Expand Up @@ -103,6 +103,23 @@
}
}
async function fetchTripPlan(from, to) {
try {
const response = await fetch(
`/api/otp/plan?fromPlace=${from.lat},${from.lng}&toPlace=${to.lat},${to.lng}`
);
if (!response.ok) {
throw new Error(`Error planning trip: ${response.statusText}`);
}
return await response.json();
} catch (error) {
console.error(error.message);
return null;
}
}
async function planTrip() {
if (!selectedFrom || !selectedTo) {
return;
Expand All @@ -120,19 +137,11 @@
fromMarker = mapProvider.addPinMarker(selectedFrom, 'From');
toMarker = mapProvider.addPinMarker(selectedTo, 'To');
await geocodeLocation(selectedFrom);
const response = await fetch(
`/api/otp/plan?fromPlace=${selectedFrom.lat},${selectedFrom.lng}&toPlace=${selectedTo.lat},${selectedTo.lng}`
);
const data = await fetchTripPlan(selectedFrom, selectedTo);
if (!response.ok) {
console.error('Error planning trip:', response.statusText);
return;
if (data) {
dispatch('tripPlanned', { data, fromMarker, toMarker });
}
const data = await response.json();
dispatch('tripPlanned', { data, fromMarker, toMarker });
} finally {
loading = false;
}
Expand Down

0 comments on commit 434893f

Please sign in to comment.