80 lines
2.9 KiB
TypeScript
80 lines
2.9 KiB
TypeScript
import { create } from 'zustand';
|
|
|
|
interface TourState {
|
|
currentTour: any;
|
|
legs: any[];
|
|
publicTours: any[];
|
|
userRole: string | null;
|
|
setTour: (tour: any) => void;
|
|
updateLegs: (legs: any[]) => void;
|
|
createTour: (tourData: any) => Promise<any>;
|
|
addLocation: (tourId: string, locationData: any) => Promise<void>;
|
|
optimizeRouting: (legId: string) => Promise<void>;
|
|
fetchTour: (id: string) => Promise<void>;
|
|
fetchPublicTours: () => Promise<void>;
|
|
}
|
|
|
|
export const useTourStore = create<TourState>((set, get) => ({
|
|
currentTour: null,
|
|
legs: [],
|
|
publicTours: [],
|
|
userRole: null,
|
|
setTour: (tour) => set({ currentTour: tour }),
|
|
updateLegs: (legs) => set({ legs }),
|
|
fetchTour: async (id: string) => {
|
|
const API_BASE = `http://${window.location.hostname}:3001`;
|
|
const response = await fetch(`${API_BASE}/api/v1/tours/${id}`);
|
|
const data = await response.json();
|
|
|
|
// Lấy role từ danh sách participants (dữ liệu mẫu từ seed)
|
|
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 });
|
|
},
|
|
createTour: async (tourData: any) => {
|
|
const API_BASE = `http://${window.location.hostname}:3001`;
|
|
const response = await fetch(`${API_BASE}/api/v1/tours`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${localStorage.getItem('token')}`
|
|
},
|
|
body: JSON.stringify(tourData),
|
|
});
|
|
return await response.json();
|
|
},
|
|
addLocation: async (tourId: string, locationData: any) => {
|
|
const API_BASE = `http://${window.location.hostname}:3001`;
|
|
const response = await fetch(`${API_BASE}/api/v1/tours/${tourId}/locations`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${localStorage.getItem('token')}`
|
|
},
|
|
body: JSON.stringify(locationData),
|
|
});
|
|
if (!response.ok) throw new Error('Lỗi khi thêm địa điểm');
|
|
// Làm mới dữ liệu tour hiện tại
|
|
get().fetchTour(tourId);
|
|
},
|
|
optimizeRouting: async (legId: string) => {
|
|
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: any) =>
|
|
l.id === legId ? { ...l, locations: locations, totalDistance: totalDistance } : l
|
|
);
|
|
set({ currentTour: { ...currentTour, legs: updatedLegs }, legs: updatedLegs });
|
|
}
|
|
},
|
|
})); |