fix: guest and admin logic
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import React, { useEffect, useState, useRef } from 'react';
|
||||
import { Compass, Loader2, LogIn, UserPlus, AlertCircle, CheckCircle } from 'lucide-react';
|
||||
import { LoginModal } from '../components/LoginModal';
|
||||
import { JoinTourLoginModal } from '../components/JoinTourLoginModal';
|
||||
|
||||
interface JoinTourPageProps {
|
||||
onLoginSuccess: (user: any) => void;
|
||||
@@ -43,6 +43,7 @@ export const JoinTourPage: React.FC<JoinTourPageProps> = ({ onLoginSuccess, onGo
|
||||
setLoading(true);
|
||||
setError('');
|
||||
try {
|
||||
console.log('[JoinTour] Attempting to join with token');
|
||||
const res = await fetch('/api/v1/tours/join-by-token', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -54,28 +55,31 @@ export const JoinTourPage: React.FC<JoinTourPageProps> = ({ onLoginSuccess, onGo
|
||||
|
||||
const data = await res.json();
|
||||
if (!res.ok) {
|
||||
throw new Error(data.message || 'Không thể gia nhập tour.');
|
||||
console.error('[JoinTour] Join failed with status', res.status, 'message:', data.message);
|
||||
// Show real backend error message
|
||||
throw new Error(data.message || `Không thể gia nhập tour (${res.status}). Vui lòng kiểm tra lại mã lời mời.`);
|
||||
}
|
||||
|
||||
console.log('[JoinTour] Join successful, message:', data.message);
|
||||
setSuccessMsg(data.message || 'Bạn đã tham gia tour thành công!');
|
||||
setTourId(data.tourId);
|
||||
// Đợi 500ms để đảm bảo các thay đổi cache ở backend hoàn tất trước khi tiếp tục
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
localStorage.removeItem('pendingInviteToken');
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Đã xảy ra lỗi.');
|
||||
console.error('[JoinTour] Error:', err.message);
|
||||
setError(err.message || 'Đã xảy ra lỗi khi gia nhập tour.');
|
||||
// Keep the pendingInviteToken in localStorage so user can retry
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSuccessLogin = (user: any) => {
|
||||
const handleJoinSuccess = (user: any, tourData: any) => {
|
||||
console.log('[JoinTour] Join successful via modal, tourId:', tourData.tourId);
|
||||
setTourId(tourData.tourId);
|
||||
setSuccessMsg(tourData.message || 'Bạn đã tham gia tour thành công!');
|
||||
onLoginSuccess(user);
|
||||
const token = localStorage.getItem('token');
|
||||
const pendingToken = localStorage.getItem('pendingInviteToken') || inviteToken;
|
||||
if (token && pendingToken) {
|
||||
handleJoinTour(pendingToken, token);
|
||||
}
|
||||
};
|
||||
|
||||
const isLoggedIn = !!localStorage.getItem('token');
|
||||
@@ -175,11 +179,12 @@ export const JoinTourPage: React.FC<JoinTourPageProps> = ({ onLoginSuccess, onGo
|
||||
|
||||
</div>
|
||||
|
||||
<LoginModal
|
||||
<JoinTourLoginModal
|
||||
isOpen={isLoginOpen}
|
||||
onClose={() => setIsLoginOpen(false)}
|
||||
inviteToken={inviteToken || ''}
|
||||
onSwitchToSignup={onGoToSignup}
|
||||
onLoginSuccess={handleSuccessLogin}
|
||||
onJoinSuccess={handleJoinSuccess}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user