Cho phép xóa tour và xóa chặng trong Tour Dashboard

This commit is contained in:
2026-06-13 21:48:53 +07:00
parent 578b03d808
commit 5e3f10631f
18 changed files with 580 additions and 42 deletions
+81
View File
@@ -9,6 +9,11 @@ interface TourState {
setTour: (tour: any) => void;
updateLegs: (legs: any[]) => void;
createTour: (tourData: any) => Promise<any>;
updateTour: (id: string, data: any) => Promise<void>;
deleteTour: (id: string) => Promise<void>;
addLeg: (tourId: string, data: any) => Promise<void>;
updateLeg: (legId: string, data: any) => Promise<void>;
deleteLeg: (legId: string) => Promise<void>;
addLocation: (tourId: string, locationData: any) => Promise<void>;
optimizeRouting: (legId: string) => Promise<void>;
setActiveLegId: (id: string | null) => void;
@@ -59,6 +64,82 @@ export const useTourStore = create<TourState>((set, get) => ({
});
return await response.json();
},
updateTour: async (id: string, data: any) => {
const API_BASE = `http://${window.location.hostname}:3001`;
const response = await fetch(`${API_BASE}/api/v1/tours/${id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('token')}`
},
body: JSON.stringify(data),
});
if (!response.ok) throw new Error('Lỗi khi cập nhật Tour');
// Làm mới danh sách khám phá
get().fetchPublicTours();
},
deleteTour: async (id: string) => {
const API_BASE = `http://${window.location.hostname}:3001`;
const response = await fetch(`${API_BASE}/api/v1/tours/${id}`, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
},
});
if (!response.ok) {
const data = await response.json();
throw new Error(data.message || 'Lỗi khi xóa Tour');
}
// Reset tour hiện tại nếu đang xem đúng tour vừa xóa
if (get().currentTour?.id === id) set({ currentTour: null });
get().fetchPublicTours();
},
addLeg: async (tourId: string, data: any) => {
const API_BASE = `http://${window.location.hostname}:3001`;
const response = await fetch(`${API_BASE}/api/v1/tours/${tourId}/legs`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('token')}`
},
body: JSON.stringify(data),
});
if (!response.ok) throw new Error('Lỗi khi thêm chặng');
get().fetchTour(tourId);
},
updateLeg: async (legId: string, data: any) => {
const API_BASE = `http://${window.location.hostname}:3001`;
const response = await fetch(`${API_BASE}/api/v1/legs/${legId}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('token')}`
},
body: JSON.stringify(data),
});
if (!response.ok) throw new Error('Lỗi khi cập nhật chặng');
const { currentTour } = get();
if (currentTour) get().fetchTour(currentTour.id);
},
deleteLeg: async (legId: string) => {
const API_BASE = `http://${window.location.hostname}:3001`;
const response = await fetch(`${API_BASE}/api/v1/legs/${legId}`, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
},
});
const result = await response.json();
if (!response.ok) {
throw new Error(result.message || 'Lỗi khi xóa chặng');
}
const { currentTour } = get();
if (currentTour) get().fetchTour(currentTour.id);
},
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`, {