Files
travelplanning/dist/useTourStore.js
T

35 lines
1.5 KiB
JavaScript

import { create } from 'zustand';
export const useTourStore = create((set, get) => ({
currentTour: null,
legs: [],
publicTours: [],
userRole: null,
setTour: (tour) => set({ currentTour: tour }),
updateLegs: (legs) => set({ legs }),
fetchTour: async (id) => {
const API_BASE = `http://${window.location.hostname}:3001`;
const response = await fetch(`${API_BASE}/api/v1/tours/${id}`);
const data = await response.json();
const role = data.participants?.[0]?.role || 'VIEWER_ONLY';
set({ currentTour: data, legs: data.legs || [], userRole: role });
},
fetchPublicTours: async () => {
const API_BASE = `http://${window.location.hostname}:3001`;
const response = await fetch(`${API_BASE}/api/v1/tours/explore`);
const data = await response.json();
set({ publicTours: data });
},
optimizeRouting: async (legId) => {
const API_BASE = `http://${window.location.hostname}:3001`;
const response = await fetch(`${API_BASE}/api/v1/routing/optimize/${legId}`, {
method: 'POST'
});
const { locations, totalDistance } = await response.json();
const { currentTour } = get();
if (currentTour) {
const updatedLegs = currentTour.legs.map((l) => l.id === legId ? { ...l, locations: locations, totalDistance: totalDistance } : l);
set({ currentTour: { ...currentTour, legs: updatedLegs }, legs: updatedLegs });
}
},
}));
//# sourceMappingURL=useTourStore.js.map