Sửa lỗi nhấn + để thêm thành viên của tour

This commit is contained in:
2026-06-14 17:16:19 +07:00
parent c6d0fdd5d1
commit 81e715ddb3
24 changed files with 515 additions and 239 deletions
+40 -22
View File
@@ -13,30 +13,32 @@ export const useTourStore = create((set, get) => ({
fetchTour: async (id) => {
const API_BASE = `http://${window.location.hostname}:3001`;
const token = localStorage.getItem('token');
const response = await fetch(`${API_BASE}/api/v1/tours/${id}`, {
headers: { 'Authorization': `Bearer ${token}` }
});
const data = await response.json();
let role = 'VIEWER_ONLY';
if (token) {
try {
const payload = JSON.parse(atob(token.split('.')[1]));
const currentUserId = payload.sub;
const participant = data.participants?.find((p) => p.userId === currentUserId);
if (participant)
role = participant.role;
}
catch (e) {
console.error("Lỗi khi xác định vai trò người dùng:", e);
try {
const response = await fetch(`${API_BASE}/api/v1/tours/${id}`, {
headers: { 'Authorization': `Bearer ${token}` }
});
if (!response.ok)
throw new Error(`HTTP ${response.status}`);
const data = await response.json();
let role = 'VIEWER_ONLY';
if (token) {
try {
const payload = JSON.parse(atob(token.split('.')[1]));
const currentUserId = payload.sub;
const participant = data.participants?.find((p) => p.userId === currentUserId);
if (participant)
role = participant.role;
}
catch (e) {
console.error("Lỗi khi xác định vai trò người dùng:", e);
}
}
const legs = data.legs || [];
set({ currentTour: data, legs, userRole: role, activeLegId: legs.length > 0 ? legs[0].id : null });
}
catch (err) {
console.error('Không thể tải tour:', err);
}
const legs = data.legs || [];
set({
currentTour: data,
legs: legs,
userRole: role,
activeLegId: legs.length > 0 ? legs[0].id : null
});
},
fetchPublicTours: async () => {
const API_BASE = `http://${window.location.hostname}:3001`;
@@ -240,5 +242,21 @@ export const useTourStore = create((set, get) => ({
set({ currentTour: { ...currentTour, legs: updatedLegs }, legs: updatedLegs });
}
},
addMember: async (tourId, member) => {
const API_BASE = `http://${window.location.hostname}:3001`;
const response = await fetch(`${API_BASE}/api/v1/tours/${tourId}/members`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${localStorage.getItem('token')}`
},
body: JSON.stringify(member),
});
if (!response.ok)
throw new Error('Lỗi khi thêm thành viên');
const { currentTour } = get();
if (currentTour)
get().fetchTour(currentTour.id);
},
}));
//# sourceMappingURL=useTourStore.js.map