Sửa lỗi nhấn + để thêm thành viên của tour
This commit is contained in:
+37
-23
@@ -22,6 +22,7 @@ interface TourState {
|
||||
updateTourStartPoint: (tourId: string, data: any) => Promise<void>;
|
||||
updateTourEndPoint: (tourId: string, data: any) => Promise<void>;
|
||||
optimizeRouting: (legId: string) => Promise<void>;
|
||||
addMember: (tourId: string, member: { userId: string; role?: string }) => Promise<void>;
|
||||
setActiveLegId: (id: string | null) => void;
|
||||
setMapCenter: (pos: [number, number]) => void;
|
||||
fetchTour: (id: string) => Promise<void>;
|
||||
@@ -42,31 +43,30 @@ export const useTourStore = create<TourState>((set, get) => ({
|
||||
fetchTour: async (id: string) => {
|
||||
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();
|
||||
|
||||
// Xác định role của người dùng hiện tại dựa trên thông tin trong JWT
|
||||
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: any) => 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();
|
||||
// ...existing role detection...
|
||||
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: any) => 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: any) {
|
||||
console.error('Không thể tải tour:', err);
|
||||
// Không set null để tránh flash trắng; giữ nguyên state cũ nếu có
|
||||
}
|
||||
|
||||
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`;
|
||||
@@ -269,4 +269,18 @@ export const useTourStore = create<TourState>((set, get) => ({
|
||||
set({ currentTour: { ...currentTour, legs: updatedLegs }, legs: updatedLegs });
|
||||
}
|
||||
},
|
||||
addMember: async (tourId: string, member: { userId: string; role?: string }) => {
|
||||
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);
|
||||
},
|
||||
}));
|
||||
Reference in New Issue
Block a user