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
+76
View File
@@ -39,6 +39,82 @@ export const useTourStore = create((set, get) => ({
});
return await response.json();
},
updateTour: async (id, data) => {
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');
get().fetchPublicTours();
},
deleteTour: async (id) => {
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');
}
if (get().currentTour?.id === id)
set({ currentTour: null });
get().fetchPublicTours();
},
addLeg: async (tourId, data) => {
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, data) => {
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) => {
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, locationData) => {
const API_BASE = `http://${window.location.hostname}:3001`;
const response = await fetch(`${API_BASE}/api/v1/tours/${tourId}/locations`, {