Backup: 2025-06-08 00:29 - User and admin online ready for dockerhub
[Restore from backup: vip-coordinator-backup-2025-06-08-00-29-user and admin online ready for dockerhub]
This commit is contained in:
@@ -1,187 +1 @@
|
||||
/* Modern App-specific styles using Tailwind utilities */
|
||||
|
||||
/* Enhanced button styles */
|
||||
@layer components {
|
||||
.btn-modern {
|
||||
@apply px-6 py-3 rounded-xl font-semibold text-sm transition-all duration-200 shadow-lg hover:shadow-xl transform hover:-translate-y-0.5;
|
||||
}
|
||||
|
||||
.btn-gradient-blue {
|
||||
@apply bg-gradient-to-r from-blue-500 to-blue-600 hover:from-blue-600 hover:to-blue-700 text-white;
|
||||
}
|
||||
|
||||
.btn-gradient-green {
|
||||
@apply bg-gradient-to-r from-green-500 to-green-600 hover:from-green-600 hover:to-green-700 text-white;
|
||||
}
|
||||
|
||||
.btn-gradient-purple {
|
||||
@apply bg-gradient-to-r from-purple-500 to-purple-600 hover:from-purple-600 hover:to-purple-700 text-white;
|
||||
}
|
||||
|
||||
.btn-gradient-amber {
|
||||
@apply bg-gradient-to-r from-amber-500 to-amber-600 hover:from-amber-600 hover:to-amber-700 text-white;
|
||||
}
|
||||
}
|
||||
|
||||
/* Status badges */
|
||||
@layer components {
|
||||
.status-badge {
|
||||
@apply inline-flex items-center px-3 py-1 rounded-full text-xs font-semibold;
|
||||
}
|
||||
|
||||
.status-scheduled {
|
||||
@apply bg-blue-100 text-blue-800 border border-blue-200;
|
||||
}
|
||||
|
||||
.status-in-progress {
|
||||
@apply bg-amber-100 text-amber-800 border border-amber-200;
|
||||
}
|
||||
|
||||
.status-completed {
|
||||
@apply bg-green-100 text-green-800 border border-green-200;
|
||||
}
|
||||
|
||||
.status-cancelled {
|
||||
@apply bg-red-100 text-red-800 border border-red-200;
|
||||
}
|
||||
}
|
||||
|
||||
/* Card enhancements */
|
||||
@layer components {
|
||||
.card-modern {
|
||||
@apply bg-white rounded-2xl shadow-lg border border-slate-200/60 overflow-hidden backdrop-blur-sm;
|
||||
}
|
||||
|
||||
.card-header {
|
||||
@apply bg-gradient-to-r from-slate-50 to-slate-100 px-6 py-4 border-b border-slate-200/60;
|
||||
}
|
||||
|
||||
.card-content {
|
||||
@apply p-6;
|
||||
}
|
||||
}
|
||||
|
||||
/* Loading states */
|
||||
@layer components {
|
||||
.loading-spinner {
|
||||
@apply animate-spin rounded-full border-4 border-blue-600 border-t-transparent;
|
||||
}
|
||||
|
||||
.loading-text {
|
||||
@apply text-slate-600 animate-pulse;
|
||||
}
|
||||
|
||||
.skeleton {
|
||||
@apply animate-pulse bg-slate-200 rounded;
|
||||
}
|
||||
}
|
||||
|
||||
/* Form enhancements */
|
||||
@layer components {
|
||||
.form-modern {
|
||||
@apply space-y-6;
|
||||
}
|
||||
|
||||
.form-group-modern {
|
||||
@apply space-y-2;
|
||||
}
|
||||
|
||||
.form-label-modern {
|
||||
@apply block text-sm font-semibold text-slate-700;
|
||||
}
|
||||
|
||||
.form-input-modern {
|
||||
@apply w-full px-4 py-3 border border-slate-300 rounded-xl shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200;
|
||||
}
|
||||
|
||||
.form-select-modern {
|
||||
@apply w-full px-4 py-3 border border-slate-300 rounded-xl shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white transition-all duration-200;
|
||||
}
|
||||
}
|
||||
|
||||
/* Animation utilities */
|
||||
@layer utilities {
|
||||
.animate-fade-in {
|
||||
animation: fadeIn 0.5s ease-in-out;
|
||||
}
|
||||
|
||||
.animate-slide-up {
|
||||
animation: slideUp 0.3s ease-out;
|
||||
}
|
||||
|
||||
.animate-scale-in {
|
||||
animation: scaleIn 0.2s ease-out;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes slideUp {
|
||||
from {
|
||||
transform: translateY(10px);
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
transform: translateY(0);
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes scaleIn {
|
||||
from {
|
||||
transform: scale(0.95);
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
transform: scale(1);
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Responsive utilities */
|
||||
@media (max-width: 768px) {
|
||||
.mobile-stack {
|
||||
@apply flex-col space-y-4 space-x-0;
|
||||
}
|
||||
|
||||
.mobile-full {
|
||||
@apply w-full;
|
||||
}
|
||||
|
||||
.mobile-text-center {
|
||||
@apply text-center;
|
||||
}
|
||||
}
|
||||
|
||||
/* Glass morphism effect */
|
||||
@layer utilities {
|
||||
.glass {
|
||||
@apply bg-white/80 backdrop-blur-lg border border-white/20;
|
||||
}
|
||||
|
||||
.glass-dark {
|
||||
@apply bg-slate-900/80 backdrop-blur-lg border border-slate-700/20;
|
||||
}
|
||||
}
|
||||
|
||||
/* Hover effects */
|
||||
@layer utilities {
|
||||
.hover-lift {
|
||||
@apply transition-transform duration-200 hover:-translate-y-1;
|
||||
}
|
||||
|
||||
.hover-glow {
|
||||
@apply transition-shadow duration-200 hover:shadow-2xl;
|
||||
}
|
||||
|
||||
.hover-scale {
|
||||
@apply transition-transform duration-200 hover:scale-105;
|
||||
}
|
||||
}
|
||||
/* Modern App-specific styles - Component classes moved to inline Tailwind */
|
||||
@@ -1,57 +1,68 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom';
|
||||
import { apiCall } from './config/api';
|
||||
import { apiCall } from './utils/api';
|
||||
import VipList from './pages/VipList';
|
||||
import VipDetails from './pages/VipDetails';
|
||||
import DriverList from './pages/DriverList';
|
||||
import DriverDashboard from './pages/DriverDashboard';
|
||||
import Dashboard from './pages/Dashboard';
|
||||
import AdminDashboard from './pages/AdminDashboard';
|
||||
import PendingApproval from './pages/PendingApproval';
|
||||
import UserManagement from './components/UserManagement';
|
||||
import Login from './components/Login';
|
||||
import OAuthCallback from './components/OAuthCallback';
|
||||
import './App.css';
|
||||
import { User } from './types';
|
||||
|
||||
function App() {
|
||||
const [user, setUser] = useState<any>(null);
|
||||
const [user, setUser] = useState<User | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
// Check if user is already authenticated
|
||||
const token = localStorage.getItem('authToken');
|
||||
if (token) {
|
||||
const savedUser = localStorage.getItem('user');
|
||||
|
||||
if (token && savedUser) {
|
||||
// Use saved user data for faster initial load
|
||||
setUser(JSON.parse(savedUser));
|
||||
setLoading(false);
|
||||
|
||||
// Then verify with server
|
||||
apiCall('/auth/me', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
})
|
||||
.then(res => {
|
||||
if (res.ok) {
|
||||
return res.json();
|
||||
.then(({ data }) => {
|
||||
if (data) {
|
||||
setUser(data as User);
|
||||
localStorage.setItem('user', JSON.stringify(data));
|
||||
} else {
|
||||
// Token is invalid, remove it
|
||||
localStorage.removeItem('authToken');
|
||||
throw new Error('Invalid token');
|
||||
localStorage.removeItem('user');
|
||||
setUser(null);
|
||||
}
|
||||
})
|
||||
.then(userData => {
|
||||
setUser(userData);
|
||||
setLoading(false);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Auth check failed:', error);
|
||||
setLoading(false);
|
||||
localStorage.removeItem('authToken');
|
||||
localStorage.removeItem('user');
|
||||
setUser(null);
|
||||
});
|
||||
} else {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleLogin = (userData: any) => {
|
||||
const handleLogin = (userData: User) => {
|
||||
setUser(userData);
|
||||
};
|
||||
|
||||
const handleLogout = () => {
|
||||
localStorage.removeItem('authToken');
|
||||
localStorage.removeItem('user');
|
||||
setUser(null);
|
||||
// Optionally call logout endpoint
|
||||
apiCall('/auth/logout', { method: 'POST' })
|
||||
@@ -71,13 +82,52 @@ function App() {
|
||||
|
||||
// Handle OAuth callback route even when not logged in
|
||||
if (window.location.pathname === '/auth/callback' || window.location.pathname === '/auth/google/callback') {
|
||||
return <Login onLogin={handleLogin} />;
|
||||
return (
|
||||
<Router>
|
||||
<Routes>
|
||||
<Route path="*" element={<OAuthCallback />} />
|
||||
</Routes>
|
||||
</Router>
|
||||
);
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return <Login onLogin={handleLogin} />;
|
||||
}
|
||||
|
||||
// Check if user is pending approval
|
||||
if (user.role !== 'administrator' && (!user.status || user.status === 'pending')) {
|
||||
return (
|
||||
<Router>
|
||||
<Routes>
|
||||
<Route path="*" element={<PendingApproval />} />
|
||||
</Routes>
|
||||
</Router>
|
||||
);
|
||||
}
|
||||
|
||||
// Check if user is deactivated
|
||||
if (user.status === 'deactivated') {
|
||||
return (
|
||||
<div className="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 flex items-center justify-center p-4">
|
||||
<div className="bg-white rounded-2xl shadow-xl max-w-md w-full p-8 text-center">
|
||||
<div className="w-16 h-16 bg-red-100 rounded-full flex items-center justify-center mx-auto mb-4">
|
||||
<svg className="w-8 h-8 text-red-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
|
||||
</svg>
|
||||
</div>
|
||||
<h1 className="text-2xl font-bold text-slate-800 mb-2">Account Deactivated</h1>
|
||||
<p className="text-slate-600 mb-6">
|
||||
Your account has been deactivated. Please contact an administrator for assistance.
|
||||
</p>
|
||||
<button onClick={handleLogout} className="btn btn-secondary w-full">
|
||||
Logout
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Router>
|
||||
<div className="min-h-screen bg-gradient-to-br from-slate-50 via-blue-50 to-indigo-50">
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { apiCall } from '../config/api';
|
||||
import { apiCall } from '../utils/api';
|
||||
|
||||
interface DriverAvailability {
|
||||
driverId: string;
|
||||
@@ -60,7 +60,7 @@ const DriverSelector: React.FC<DriverSelectorProps> = ({
|
||||
setLoading(true);
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall('/api/drivers/availability', {
|
||||
const { data } = await apiCall('/api/drivers/availability', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
@@ -69,8 +69,7 @@ const DriverSelector: React.FC<DriverSelectorProps> = ({
|
||||
body: JSON.stringify(eventTime),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
if (data) {
|
||||
setAvailability(data);
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,115 +1,58 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { apiCall } from '../config/api';
|
||||
import { apiCall } from '../utils/api';
|
||||
import GoogleLogin from './GoogleLogin';
|
||||
import './Login.css';
|
||||
import { User } from '../types';
|
||||
|
||||
interface LoginProps {
|
||||
onLogin: (user: any) => void;
|
||||
onLogin: (user: User) => void;
|
||||
}
|
||||
|
||||
interface SetupStatus {
|
||||
ready: boolean;
|
||||
hasUsers: boolean;
|
||||
missingEnvVars?: string[];
|
||||
}
|
||||
|
||||
const Login: React.FC<LoginProps> = ({ onLogin }) => {
|
||||
const [setupStatus, setSetupStatus] = useState<any>(null);
|
||||
const [setupStatus, setSetupStatus] = useState<SetupStatus | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
// Check system setup status
|
||||
apiCall('/auth/setup')
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
.then(({ data }) => {
|
||||
setSetupStatus(data);
|
||||
setLoading(false);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error checking setup status:', error);
|
||||
setSetupStatus({ ready: true, hasUsers: false }); // Assume ready if can't check
|
||||
setLoading(false);
|
||||
});
|
||||
}, []);
|
||||
|
||||
// Check for OAuth callback code in URL
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const code = urlParams.get('code');
|
||||
const error = urlParams.get('error');
|
||||
const token = urlParams.get('token');
|
||||
const handleGoogleSuccess = (user: any, token: string) => {
|
||||
// Store the token and user data
|
||||
localStorage.setItem('authToken', token);
|
||||
localStorage.setItem('user', JSON.stringify(user));
|
||||
|
||||
// Call onLogin with the user data
|
||||
onLogin(user);
|
||||
};
|
||||
|
||||
if (code && (window.location.pathname === '/auth/google/callback' || window.location.pathname === '/auth/callback')) {
|
||||
// Exchange code for token
|
||||
apiCall('/auth/google/exchange', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ code })
|
||||
})
|
||||
.then(res => {
|
||||
if (!res.ok) {
|
||||
throw new Error('Failed to exchange code for token');
|
||||
}
|
||||
return res.json();
|
||||
})
|
||||
.then(({ token, user }) => {
|
||||
localStorage.setItem('authToken', token);
|
||||
onLogin(user);
|
||||
// Clean up URL and redirect to dashboard
|
||||
window.history.replaceState({}, document.title, '/');
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('OAuth exchange failed:', error);
|
||||
alert('Login failed. Please try again.');
|
||||
// Clean up URL
|
||||
window.history.replaceState({}, document.title, '/');
|
||||
});
|
||||
} else if (token && (window.location.pathname === '/auth/callback' || window.location.pathname === '/auth/google/callback')) {
|
||||
// Direct token from URL (from backend redirect)
|
||||
localStorage.setItem('authToken', token);
|
||||
|
||||
apiCall('/auth/me', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
})
|
||||
.then(res => {
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to get user info: ${res.status} ${res.statusText}`);
|
||||
}
|
||||
return res.json();
|
||||
})
|
||||
.then(user => {
|
||||
onLogin(user);
|
||||
// Clean up URL and redirect to dashboard
|
||||
window.history.replaceState({}, document.title, '/');
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error getting user info:', error);
|
||||
alert('Login failed. Please try again.');
|
||||
localStorage.removeItem('authToken');
|
||||
// Clean up URL
|
||||
window.history.replaceState({}, document.title, '/');
|
||||
});
|
||||
} else if (error) {
|
||||
console.error('Authentication error:', error);
|
||||
alert(`Login error: ${error}`);
|
||||
// Clean up URL
|
||||
window.history.replaceState({}, document.title, '/');
|
||||
}
|
||||
}, [onLogin]);
|
||||
|
||||
const handleGoogleLogin = async () => {
|
||||
try {
|
||||
// Get OAuth URL from backend
|
||||
const response = await apiCall('/auth/google/url');
|
||||
const { url } = await response.json();
|
||||
|
||||
// Redirect to Google OAuth
|
||||
window.location.href = url;
|
||||
} catch (error) {
|
||||
console.error('Failed to get OAuth URL:', error);
|
||||
alert('Login failed. Please try again.');
|
||||
}
|
||||
const handleGoogleError = (errorMessage: string) => {
|
||||
setError(errorMessage);
|
||||
setTimeout(() => setError(null), 5000); // Clear error after 5 seconds
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="login-container">
|
||||
<div className="login-card">
|
||||
<div className="loading">Loading...</div>
|
||||
<div className="login-box">
|
||||
<h1 className="login-title">VIP Coordinator</h1>
|
||||
<p>Loading...</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
@@ -117,68 +60,33 @@ const Login: React.FC<LoginProps> = ({ onLogin }) => {
|
||||
|
||||
return (
|
||||
<div className="login-container">
|
||||
<div className="login-card">
|
||||
<div className="login-header">
|
||||
<h1>VIP Coordinator</h1>
|
||||
<p>Secure access required</p>
|
||||
</div>
|
||||
|
||||
{!setupStatus?.firstAdminCreated && (
|
||||
<div className="setup-notice">
|
||||
<h3>🚀 First Time Setup</h3>
|
||||
<p>The first person to log in will become the system administrator.</p>
|
||||
<div className="login-box">
|
||||
<h1 className="login-title">VIP Coordinator</h1>
|
||||
<p className="login-subtitle">Transportation Management System</p>
|
||||
|
||||
{error && (
|
||||
<div className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="login-content">
|
||||
<button
|
||||
className="google-login-btn"
|
||||
onClick={handleGoogleLogin}
|
||||
disabled={false}
|
||||
>
|
||||
<svg className="google-icon" viewBox="0 0 24 24">
|
||||
<path fill="#4285F4" d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"/>
|
||||
<path fill="#34A853" d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/>
|
||||
<path fill="#FBBC05" d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"/>
|
||||
<path fill="#EA4335" d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"/>
|
||||
</svg>
|
||||
Continue with Google
|
||||
</button>
|
||||
|
||||
<div className="login-info">
|
||||
<p>
|
||||
{setupStatus?.firstAdminCreated
|
||||
? "Sign in with your Google account to access the VIP Coordinator."
|
||||
: "Sign in with Google to set up your administrator account."
|
||||
}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{setupStatus && !setupStatus.setupCompleted && (
|
||||
<div style={{
|
||||
marginTop: '1rem',
|
||||
padding: '1rem',
|
||||
backgroundColor: '#fff3cd',
|
||||
borderRadius: '6px',
|
||||
border: '1px solid #ffeaa7',
|
||||
fontSize: '0.9rem'
|
||||
}}>
|
||||
<strong>⚠️ Setup Required:</strong>
|
||||
<p style={{ margin: '0.5rem 0 0 0' }}>
|
||||
Google OAuth credentials need to be configured. If the login doesn't work,
|
||||
please follow the setup guide in <code>GOOGLE_OAUTH_SETUP.md</code> to configure
|
||||
your Google Cloud Console credentials in the admin dashboard.
|
||||
<GoogleLogin
|
||||
onSuccess={handleGoogleSuccess}
|
||||
onError={handleGoogleError}
|
||||
/>
|
||||
|
||||
<div className="setup-info">
|
||||
{setupStatus && !setupStatus.hasUsers && (
|
||||
<p className="text-sm text-amber-600 mt-4">
|
||||
First user to log in will become an administrator
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="login-footer">
|
||||
<p>Secure authentication powered by Google OAuth</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Login;
|
||||
export default Login;
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { apiCall } from '../config/api';
|
||||
import { apiCall } from '../utils/api';
|
||||
import DriverSelector from './DriverSelector';
|
||||
|
||||
interface ScheduleEvent {
|
||||
@@ -33,15 +33,14 @@ const ScheduleManager: React.FC<ScheduleManagerProps> = ({ vipId, vipName }) =>
|
||||
const fetchSchedule = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall(`/api/vips/${vipId}/schedule`, {
|
||||
const { data } = await apiCall(`/api/vips/${vipId}/schedule`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
if (data) {
|
||||
setSchedule(data);
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -52,15 +51,14 @@ const ScheduleManager: React.FC<ScheduleManagerProps> = ({ vipId, vipName }) =>
|
||||
const fetchDrivers = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall('/api/drivers', {
|
||||
const { data } = await apiCall('/api/drivers', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
if (data) {
|
||||
setDrivers(data);
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -305,7 +303,7 @@ const ScheduleManager: React.FC<ScheduleManagerProps> = ({ vipId, vipName }) =>
|
||||
async function handleAddEvent(eventData: any) {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall(`/api/vips/${vipId}/schedule`, {
|
||||
const { data } = await apiCall(`/api/vips/${vipId}/schedule`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
@@ -314,12 +312,11 @@ const ScheduleManager: React.FC<ScheduleManagerProps> = ({ vipId, vipName }) =>
|
||||
body: JSON.stringify(eventData),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
if (data) {
|
||||
await fetchSchedule();
|
||||
setShowAddForm(false);
|
||||
} else {
|
||||
const errorData = await response.json();
|
||||
throw errorData;
|
||||
throw new Error('Failed to add event');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error adding event:', error);
|
||||
@@ -330,7 +327,7 @@ const ScheduleManager: React.FC<ScheduleManagerProps> = ({ vipId, vipName }) =>
|
||||
async function handleEditEvent(eventData: any) {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall(`/api/vips/${vipId}/schedule/${eventData.id}`, {
|
||||
const { data } = await apiCall(`/api/vips/${vipId}/schedule/${eventData.id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
@@ -339,12 +336,11 @@ const ScheduleManager: React.FC<ScheduleManagerProps> = ({ vipId, vipName }) =>
|
||||
body: JSON.stringify(eventData),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
if (data) {
|
||||
await fetchSchedule();
|
||||
setEditingEvent(null);
|
||||
} else {
|
||||
const errorData = await response.json();
|
||||
throw errorData;
|
||||
throw new Error('Failed to update event');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error updating event:', error);
|
||||
|
||||
@@ -1,488 +1,458 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { API_BASE_URL } from '../config/api';
|
||||
|
||||
interface User {
|
||||
id: string;
|
||||
email: string;
|
||||
name: string;
|
||||
picture: string;
|
||||
role: string;
|
||||
created_at: string;
|
||||
last_sign_in_at?: string;
|
||||
provider: string;
|
||||
}
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { apiCall } from '../utils/api';
|
||||
import { User } from '../types';
|
||||
import { useToast } from '../contexts/ToastContext';
|
||||
import { LoadingSpinner } from './LoadingSpinner';
|
||||
|
||||
interface UserManagementProps {
|
||||
currentUser: any;
|
||||
currentUserId: string;
|
||||
}
|
||||
|
||||
const UserManagement: React.FC<UserManagementProps> = ({ currentUser }) => {
|
||||
const UserManagement: React.FC<UserManagementProps> = ({ currentUserId }) => {
|
||||
const { showToast } = useToast();
|
||||
const [users, setUsers] = useState<User[]>([]);
|
||||
const [pendingUsers, setPendingUsers] = useState<User[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [activeTab, setActiveTab] = useState<'all' | 'pending'>('all');
|
||||
const [updatingUser, setUpdatingUser] = useState<string | null>(null);
|
||||
const [searchTerm, setSearchTerm] = useState('');
|
||||
const [filterRole, setFilterRole] = useState<string>('all');
|
||||
const [filterStatus, setFilterStatus] = useState<string>('all');
|
||||
const [selectedUser, setSelectedUser] = useState<User | null>(null);
|
||||
const [showEditModal, setShowEditModal] = useState(false);
|
||||
|
||||
// Check if current user is admin
|
||||
if (currentUser?.role !== 'administrator') {
|
||||
return (
|
||||
<div className="p-6 bg-red-50 border border-red-200 rounded-lg">
|
||||
<h2 className="text-xl font-semibold text-red-800 mb-2">Access Denied</h2>
|
||||
<p className="text-red-600">You need administrator privileges to access user management.</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
useEffect(() => {
|
||||
fetchUsers();
|
||||
}, []);
|
||||
|
||||
const fetchUsers = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await fetch(`${API_BASE_URL}/auth/users`, {
|
||||
const { data } = await apiCall('/auth/users', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch users');
|
||||
if (data) {
|
||||
setUsers(data);
|
||||
} else {
|
||||
showToast('Failed to load users', 'error');
|
||||
}
|
||||
|
||||
const userData = await response.json();
|
||||
setUsers(userData);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to fetch users');
|
||||
} catch (error) {
|
||||
showToast('Error loading users', 'error');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const fetchPendingUsers = async () => {
|
||||
const handleApprove = async (userEmail: string, role: string) => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await fetch(`${API_BASE_URL}/auth/users/pending/list`, {
|
||||
const { data } = await apiCall(`/auth/users/${userEmail}/approve`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch pending users');
|
||||
}
|
||||
|
||||
const pendingData = await response.json();
|
||||
setPendingUsers(pendingData);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to fetch pending users');
|
||||
}
|
||||
};
|
||||
|
||||
const updateUserRole = async (userEmail: string, newRole: string) => {
|
||||
setUpdatingUser(userEmail);
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await fetch(`${API_BASE_URL}/auth/users/${userEmail}/role`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ role: newRole })
|
||||
body: JSON.stringify({ role }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to update user role');
|
||||
if (data) {
|
||||
showToast('User approved successfully!', 'success');
|
||||
fetchUsers();
|
||||
} else {
|
||||
showToast('Failed to approve user', 'error');
|
||||
}
|
||||
|
||||
// Refresh users list
|
||||
await fetchUsers();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to update user role');
|
||||
} finally {
|
||||
setUpdatingUser(null);
|
||||
} catch (error) {
|
||||
showToast('Error approving user', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
const deleteUser = async (userEmail: string, userName: string) => {
|
||||
if (!confirm(`Are you sure you want to delete user "${userName}"? This action cannot be undone.`)) {
|
||||
return;
|
||||
}
|
||||
const handleReject = async (userEmail: string) => {
|
||||
if (!confirm('Are you sure you want to reject this user?')) return;
|
||||
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await fetch(`${API_BASE_URL}/auth/users/${userEmail}`, {
|
||||
method: 'DELETE',
|
||||
const { data } = await apiCall(`/auth/users/${userEmail}/reject`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to delete user');
|
||||
}
|
||||
|
||||
// Refresh users list
|
||||
await fetchUsers();
|
||||
await fetchPendingUsers();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to delete user');
|
||||
}
|
||||
};
|
||||
|
||||
const approveUser = async (userEmail: string) => {
|
||||
setUpdatingUser(userEmail);
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await fetch(`${API_BASE_URL}/auth/users/${userEmail}/approval`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ status: 'approved' })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to approve user');
|
||||
if (data) {
|
||||
showToast('User rejected', 'success');
|
||||
fetchUsers();
|
||||
} else {
|
||||
showToast('Failed to reject user', 'error');
|
||||
}
|
||||
|
||||
// Refresh both lists
|
||||
await fetchUsers();
|
||||
await fetchPendingUsers();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to approve user');
|
||||
} finally {
|
||||
setUpdatingUser(null);
|
||||
} catch (error) {
|
||||
showToast('Error rejecting user', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
const denyUser = async (userEmail: string, userName: string) => {
|
||||
if (!confirm(`Are you sure you want to deny access for "${userName}"?`)) {
|
||||
return;
|
||||
}
|
||||
const handleDeactivate = async (userEmail: string) => {
|
||||
if (!confirm('Are you sure you want to deactivate this user?')) return;
|
||||
|
||||
setUpdatingUser(userEmail);
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await fetch(`${API_BASE_URL}/auth/users/${userEmail}/approval`, {
|
||||
method: 'PATCH',
|
||||
const { data } = await apiCall(`/auth/users/${userEmail}/deactivate`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ status: 'denied' })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to deny user');
|
||||
if (data) {
|
||||
showToast('User deactivated', 'success');
|
||||
fetchUsers();
|
||||
} else {
|
||||
showToast('Failed to deactivate user', 'error');
|
||||
}
|
||||
|
||||
// Refresh both lists
|
||||
await fetchUsers();
|
||||
await fetchPendingUsers();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to deny user');
|
||||
} finally {
|
||||
setUpdatingUser(null);
|
||||
} catch (error) {
|
||||
showToast('Error deactivating user', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchUsers();
|
||||
fetchPendingUsers();
|
||||
}, []);
|
||||
const handleReactivate = async (userEmail: string) => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const { data } = await apiCall(`/auth/users/${userEmail}/reactivate`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (activeTab === 'pending') {
|
||||
fetchPendingUsers();
|
||||
}
|
||||
}, [activeTab]);
|
||||
|
||||
const formatDate = (dateString: string) => {
|
||||
return new Date(dateString).toLocaleDateString('en-US', {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
};
|
||||
|
||||
const getRoleBadgeColor = (role: string) => {
|
||||
switch (role) {
|
||||
case 'administrator':
|
||||
return 'bg-red-100 text-red-800 border-red-200';
|
||||
case 'coordinator':
|
||||
return 'bg-blue-100 text-blue-800 border-blue-200';
|
||||
case 'driver':
|
||||
return 'bg-green-100 text-green-800 border-green-200';
|
||||
default:
|
||||
return 'bg-gray-100 text-gray-800 border-gray-200';
|
||||
if (data) {
|
||||
showToast('User reactivated', 'success');
|
||||
fetchUsers();
|
||||
} else {
|
||||
showToast('Failed to reactivate user', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Error reactivating user', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
const handleRoleChange = async (userEmail: string, newRole: string) => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const { data } = await apiCall(`/auth/users/${userEmail}/role`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ role: newRole }),
|
||||
});
|
||||
|
||||
if (data) {
|
||||
showToast('Role updated successfully', 'success');
|
||||
fetchUsers();
|
||||
setShowEditModal(false);
|
||||
} else {
|
||||
showToast('Failed to update role', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
showToast('Error updating role', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
// Filter users
|
||||
const filteredUsers = users.filter(user => {
|
||||
const matchesSearch = searchTerm === '' ||
|
||||
user.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
||||
user.email.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
||||
user.organization?.toLowerCase().includes(searchTerm.toLowerCase());
|
||||
|
||||
const matchesRole = filterRole === 'all' || user.role === filterRole;
|
||||
const matchesStatus = filterStatus === 'all' || user.status === filterStatus;
|
||||
|
||||
return matchesSearch && matchesRole && matchesStatus;
|
||||
});
|
||||
|
||||
// Separate pending users
|
||||
const pendingUsers = filteredUsers.filter(u => u.status === 'pending');
|
||||
const activeUsers = filteredUsers.filter(u => u.status !== 'pending');
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="p-6">
|
||||
<div className="animate-pulse">
|
||||
<div className="h-8 bg-gray-200 rounded-lg w-1/4 mb-6"></div>
|
||||
<div className="space-y-4">
|
||||
{[1, 2, 3].map(i => (
|
||||
<div key={i} className="h-20 bg-gray-200 rounded-lg"></div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-center items-center h-64">
|
||||
<LoadingSpinner size="lg" message="Loading users..." />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<div className="mb-6">
|
||||
<h2 className="text-2xl font-bold text-gray-900 mb-2">User Management</h2>
|
||||
<p className="text-gray-600">Manage user accounts and permissions (PostgreSQL Database)</p>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-6 p-4 bg-red-50 border border-red-200 rounded-lg">
|
||||
<p className="text-red-600">{error}</p>
|
||||
<button
|
||||
onClick={() => setError(null)}
|
||||
className="mt-2 text-sm text-red-500 hover:text-red-700"
|
||||
>
|
||||
Dismiss
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Tab Navigation */}
|
||||
<div className="mb-6">
|
||||
<div className="border-b border-gray-200">
|
||||
<nav className="-mb-px flex space-x-8">
|
||||
<button
|
||||
onClick={() => setActiveTab('all')}
|
||||
className={`py-2 px-1 border-b-2 font-medium text-sm ${
|
||||
activeTab === 'all'
|
||||
? 'border-blue-500 text-blue-600'
|
||||
: 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'
|
||||
}`}
|
||||
<div className="space-y-6">
|
||||
{/* Filters */}
|
||||
<div className="bg-white rounded-xl shadow-sm border border-slate-200 p-6">
|
||||
<div className="grid grid-cols-1 md:grid-cols-4 gap-4">
|
||||
<div className="md:col-span-2">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Search users by name, email, or organization..."
|
||||
value={searchTerm}
|
||||
onChange={(e) => setSearchTerm(e.target.value)}
|
||||
className="form-input w-full"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<select
|
||||
value={filterRole}
|
||||
onChange={(e) => setFilterRole(e.target.value)}
|
||||
className="form-select w-full"
|
||||
>
|
||||
👥 All Users ({users.length})
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setActiveTab('pending')}
|
||||
className={`py-2 px-1 border-b-2 font-medium text-sm ${
|
||||
activeTab === 'pending'
|
||||
? 'border-orange-500 text-orange-600'
|
||||
: 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300'
|
||||
}`}
|
||||
<option value="all">All Roles</option>
|
||||
<option value="administrator">Administrator</option>
|
||||
<option value="coordinator">Coordinator</option>
|
||||
<option value="driver">Driver</option>
|
||||
<option value="viewer">Viewer</option>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<select
|
||||
value={filterStatus}
|
||||
onChange={(e) => setFilterStatus(e.target.value)}
|
||||
className="form-select w-full"
|
||||
>
|
||||
⏳ Pending Approval ({pendingUsers.length})
|
||||
{pendingUsers.length > 0 && (
|
||||
<span className="ml-2 inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-orange-100 text-orange-800">
|
||||
{pendingUsers.length}
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
</nav>
|
||||
<option value="all">All Status</option>
|
||||
<option value="pending">Pending</option>
|
||||
<option value="active">Active</option>
|
||||
<option value="deactivated">Deactivated</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Content based on active tab */}
|
||||
{activeTab === 'all' && (
|
||||
<div className="bg-white shadow-sm border border-gray-200 rounded-lg overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-gray-200 bg-gray-50">
|
||||
<h3 className="text-lg font-medium text-gray-900">
|
||||
All Users ({users.length})
|
||||
</h3>
|
||||
</div>
|
||||
|
||||
<div className="divide-y divide-gray-200">
|
||||
{users.map((user) => (
|
||||
<div key={user.email} className="p-6 hover:bg-gray-50">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center space-x-4">
|
||||
{user.picture ? (
|
||||
<img
|
||||
src={user.picture}
|
||||
alt={user.name}
|
||||
className="w-12 h-12 rounded-full"
|
||||
/>
|
||||
) : (
|
||||
<div className="w-12 h-12 rounded-full bg-gray-300 flex items-center justify-center">
|
||||
<span className="text-gray-600 font-medium">
|
||||
{user.name.charAt(0).toUpperCase()}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div>
|
||||
<h4 className="text-lg font-medium text-gray-900">{user.name}</h4>
|
||||
<p className="text-gray-600">{user.email}</p>
|
||||
<div className="flex items-center space-x-4 mt-1 text-sm text-gray-500">
|
||||
<span>Joined: {formatDate(user.created_at)}</span>
|
||||
{user.last_sign_in_at && (
|
||||
<span>Last login: {formatDate(user.last_sign_in_at)}</span>
|
||||
)}
|
||||
<span className="capitalize">via {user.provider}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center space-x-4">
|
||||
<div className="flex items-center space-x-2">
|
||||
<span className="text-sm text-gray-600">Role:</span>
|
||||
<select
|
||||
value={user.role}
|
||||
onChange={(e) => updateUserRole(user.email, e.target.value)}
|
||||
disabled={updatingUser === user.email || user.email === currentUser.email}
|
||||
className={`px-3 py-1 border rounded-md text-sm font-medium ${getRoleBadgeColor(user.role)} ${
|
||||
updatingUser === user.email ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer hover:bg-opacity-80'
|
||||
}`}
|
||||
>
|
||||
<option value="coordinator">Coordinator</option>
|
||||
<option value="administrator">Administrator</option>
|
||||
<option value="driver">Driver</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{user.email !== currentUser.email && (
|
||||
<button
|
||||
onClick={() => deleteUser(user.email, user.name)}
|
||||
className="px-3 py-1 text-sm text-red-600 hover:text-red-800 hover:bg-red-50 rounded-md border border-red-200 transition-colors"
|
||||
>
|
||||
🗑️ Delete
|
||||
</button>
|
||||
)}
|
||||
|
||||
{user.email === currentUser.email && (
|
||||
<span className="px-3 py-1 text-sm text-blue-600 bg-blue-50 rounded-md border border-blue-200">
|
||||
👤 You
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{users.length === 0 && (
|
||||
<div className="p-6 text-center text-gray-500">
|
||||
No users found.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Pending Users Tab */}
|
||||
{activeTab === 'pending' && (
|
||||
<div className="bg-white shadow-sm border border-gray-200 rounded-lg overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-gray-200 bg-orange-50">
|
||||
<h3 className="text-lg font-medium text-gray-900">
|
||||
{/* Pending Users */}
|
||||
{pendingUsers.length > 0 && (
|
||||
<div className="bg-white rounded-xl shadow-sm border border-slate-200">
|
||||
<div className="p-6 border-b border-slate-200">
|
||||
<h3 className="text-lg font-semibold text-slate-800">
|
||||
Pending Approval ({pendingUsers.length})
|
||||
</h3>
|
||||
<p className="text-sm text-gray-600 mt-1">
|
||||
Users waiting for administrator approval to access the system
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="divide-y divide-gray-200">
|
||||
{pendingUsers.map((user) => (
|
||||
<div key={user.email} className="p-6 hover:bg-gray-50">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center space-x-4">
|
||||
{user.picture ? (
|
||||
<img
|
||||
src={user.picture}
|
||||
alt={user.name}
|
||||
className="w-12 h-12 rounded-full"
|
||||
/>
|
||||
) : (
|
||||
<div className="w-12 h-12 rounded-full bg-gray-300 flex items-center justify-center">
|
||||
<span className="text-gray-600 font-medium">
|
||||
{user.name.charAt(0).toUpperCase()}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="divide-y divide-slate-200">
|
||||
{pendingUsers.map(user => (
|
||||
<div key={user.id} className="p-6 hover:bg-slate-50">
|
||||
<div className="flex items-start justify-between">
|
||||
<div className="flex items-start space-x-4">
|
||||
<div className="w-12 h-12 bg-amber-100 rounded-full flex items-center justify-center">
|
||||
<span className="text-lg font-semibold text-amber-700">
|
||||
{user.name.charAt(0).toUpperCase()}
|
||||
</span>
|
||||
</div>
|
||||
<div>
|
||||
<h4 className="text-lg font-medium text-gray-900">{user.name}</h4>
|
||||
<p className="text-gray-600">{user.email}</p>
|
||||
<div className="flex items-center space-x-4 mt-1 text-sm text-gray-500">
|
||||
<span>Requested: {formatDate(user.created_at)}</span>
|
||||
<span className="capitalize">via {user.provider}</span>
|
||||
<span className={`px-2 py-1 rounded-full text-xs font-medium ${
|
||||
getRoleBadgeColor(user.role)
|
||||
}`}>
|
||||
{user.role}
|
||||
</span>
|
||||
<h4 className="font-semibold text-slate-800">{user.name}</h4>
|
||||
<p className="text-sm text-slate-600">{user.email}</p>
|
||||
<div className="mt-2 space-y-1 text-sm">
|
||||
<p><span className="text-slate-500">Organization:</span> {user.organization || 'Not provided'}</p>
|
||||
<p><span className="text-slate-500">Phone:</span> {user.phone || 'Not provided'}</p>
|
||||
<p><span className="text-slate-500">Requested Role:</span>
|
||||
<span className="ml-1 font-medium capitalize">{user.onboardingData?.requestedRole}</span>
|
||||
</p>
|
||||
<p className="mt-2 p-2 bg-slate-50 rounded text-slate-700">
|
||||
<span className="font-medium">Reason:</span> {user.onboardingData?.reason}
|
||||
</p>
|
||||
{user.onboardingData?.vehicleType && (
|
||||
<div className="mt-2 p-2 bg-blue-50 rounded">
|
||||
<p className="font-medium text-blue-900 mb-1">Driver Details:</p>
|
||||
<p className="text-sm text-blue-800">
|
||||
Vehicle: {user.onboardingData.vehicleType}
|
||||
({user.onboardingData.vehicleCapacity} passengers) -
|
||||
{user.onboardingData.licensePlate}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center space-x-3">
|
||||
<button
|
||||
onClick={() => approveUser(user.email)}
|
||||
disabled={updatingUser === user.email}
|
||||
className={`px-4 py-2 text-sm font-medium text-white bg-green-600 hover:bg-green-700 rounded-md transition-colors ${
|
||||
updatingUser === user.email ? 'opacity-50 cursor-not-allowed' : ''
|
||||
}`}
|
||||
<div className="flex space-x-2">
|
||||
<select
|
||||
className="form-select text-sm"
|
||||
defaultValue={user.onboardingData?.requestedRole}
|
||||
onChange={(e) => {
|
||||
const role = e.target.value;
|
||||
if (confirm(`Approve ${user.name} as ${role}?`)) {
|
||||
handleApprove(user.email, role);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{updatingUser === user.email ? '⏳ Approving...' : '✅ Approve'}
|
||||
</button>
|
||||
<option value="">Select role to approve</option>
|
||||
<option value="administrator">Approve as Administrator</option>
|
||||
<option value="coordinator">Approve as Coordinator</option>
|
||||
<option value="driver">Approve as Driver</option>
|
||||
<option value="viewer">Approve as Viewer</option>
|
||||
</select>
|
||||
<button
|
||||
onClick={() => denyUser(user.email, user.name)}
|
||||
disabled={updatingUser === user.email}
|
||||
className={`px-4 py-2 text-sm font-medium text-white bg-red-600 hover:bg-red-700 rounded-md transition-colors ${
|
||||
updatingUser === user.email ? 'opacity-50 cursor-not-allowed' : ''
|
||||
}`}
|
||||
onClick={() => handleReject(user.email)}
|
||||
className="btn btn-danger btn-sm"
|
||||
>
|
||||
{updatingUser === user.email ? '⏳ Denying...' : '❌ Deny'}
|
||||
Reject
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{pendingUsers.length === 0 && (
|
||||
<div className="p-6 text-center text-gray-500">
|
||||
<div className="text-6xl mb-4">✅</div>
|
||||
<p className="text-lg font-medium mb-2">No pending approvals</p>
|
||||
<p className="text-sm">All users have been processed.</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="mt-6 p-4 bg-blue-50 border border-blue-200 rounded-lg">
|
||||
<h4 className="font-medium text-blue-900 mb-2">Role Descriptions:</h4>
|
||||
<ul className="text-sm text-blue-800 space-y-1">
|
||||
<li><strong>Administrator:</strong> Full access to all features including user management</li>
|
||||
<li><strong>Coordinator:</strong> Can manage VIPs, drivers, and schedules</li>
|
||||
<li><strong>Driver:</strong> Can view assigned schedules and update status</li>
|
||||
</ul>
|
||||
{/* Active/All Users */}
|
||||
<div className="bg-white rounded-xl shadow-sm border border-slate-200">
|
||||
<div className="p-6 border-b border-slate-200">
|
||||
<h3 className="text-lg font-semibold text-slate-800">
|
||||
Users ({activeUsers.length})
|
||||
</h3>
|
||||
</div>
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
<thead className="bg-slate-50 border-b border-slate-200">
|
||||
<tr>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">
|
||||
User
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">
|
||||
Role
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">
|
||||
Organization
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">
|
||||
Status
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">
|
||||
Approved By
|
||||
</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-slate-500 uppercase tracking-wider">
|
||||
Actions
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-slate-200">
|
||||
{activeUsers.map(user => (
|
||||
<tr key={user.id} className="hover:bg-slate-50">
|
||||
<td className="px-6 py-4 whitespace-nowrap">
|
||||
<div className="flex items-center">
|
||||
<div className="w-10 h-10 bg-slate-200 rounded-full flex items-center justify-center">
|
||||
<span className="text-sm font-semibold text-slate-700">
|
||||
{user.name.charAt(0).toUpperCase()}
|
||||
</span>
|
||||
</div>
|
||||
<div className="ml-4">
|
||||
<div className="text-sm font-medium text-slate-900">{user.name}</div>
|
||||
<div className="text-sm text-slate-500">{user.email}</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap">
|
||||
<span className="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-blue-100 text-blue-800 capitalize">
|
||||
{user.role}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm text-slate-500">
|
||||
{user.organization || '-'}
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap">
|
||||
<span className={`px-2 inline-flex text-xs leading-5 font-semibold rounded-full ${
|
||||
user.status === 'active'
|
||||
? 'bg-green-100 text-green-800'
|
||||
: 'bg-red-100 text-red-800'
|
||||
}`}>
|
||||
{user.status}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm text-slate-500">
|
||||
{user.approvedBy || '-'}
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm font-medium">
|
||||
<button
|
||||
onClick={() => {
|
||||
setSelectedUser(user);
|
||||
setShowEditModal(true);
|
||||
}}
|
||||
className="text-amber-600 hover:text-amber-900 mr-3"
|
||||
disabled={user.id === currentUserId}
|
||||
>
|
||||
Edit
|
||||
</button>
|
||||
{user.status === 'active' ? (
|
||||
<button
|
||||
onClick={() => handleDeactivate(user.email)}
|
||||
className="text-red-600 hover:text-red-900"
|
||||
disabled={user.id === currentUserId}
|
||||
>
|
||||
Deactivate
|
||||
</button>
|
||||
) : (
|
||||
<button
|
||||
onClick={() => handleReactivate(user.email)}
|
||||
className="text-green-600 hover:text-green-900"
|
||||
>
|
||||
Reactivate
|
||||
</button>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 p-4 bg-orange-50 border border-orange-200 rounded-lg">
|
||||
<h4 className="font-medium text-orange-900 mb-2">🔐 User Approval System:</h4>
|
||||
<p className="text-sm text-orange-800">
|
||||
New users (except the first administrator) require approval before accessing the system.
|
||||
Users with pending approval will see a "pending approval" message when they try to sign in.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 p-4 bg-green-50 border border-green-200 rounded-lg">
|
||||
<h4 className="font-medium text-green-900 mb-2">✅ PostgreSQL Database:</h4>
|
||||
<p className="text-sm text-green-800">
|
||||
User data is stored in your PostgreSQL database with proper indexing and relationships.
|
||||
All user management operations are transactional and fully persistent across server restarts.
|
||||
</p>
|
||||
</div>
|
||||
{/* Edit Modal */}
|
||||
{showEditModal && selectedUser && (
|
||||
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4">
|
||||
<div className="bg-white rounded-xl max-w-md w-full p-6">
|
||||
<h3 className="text-lg font-semibold text-slate-800 mb-4">
|
||||
Edit User: {selectedUser.name}
|
||||
</h3>
|
||||
<div className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-slate-700 mb-2">
|
||||
Role
|
||||
</label>
|
||||
<select
|
||||
value={selectedUser.role}
|
||||
onChange={(e) => handleRoleChange(selectedUser.id, e.target.value)}
|
||||
className="form-select w-full"
|
||||
disabled={selectedUser.id === currentUserId}
|
||||
>
|
||||
<option value="administrator">Administrator</option>
|
||||
<option value="coordinator">Coordinator</option>
|
||||
<option value="driver">Driver</option>
|
||||
<option value="viewer">Viewer</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="bg-slate-50 rounded-lg p-4 text-sm">
|
||||
<h4 className="font-medium text-slate-800 mb-2">Audit Information:</h4>
|
||||
<p className="text-slate-600">Created: {new Date(selectedUser.createdAt || '').toLocaleString()}</p>
|
||||
{selectedUser.approvedBy && (
|
||||
<p className="text-slate-600">Approved by: {selectedUser.approvedBy}</p>
|
||||
)}
|
||||
{selectedUser.approvedAt && (
|
||||
<p className="text-slate-600">Approved at: {new Date(selectedUser.approvedAt).toLocaleString()}</p>
|
||||
)}
|
||||
{selectedUser.lastLogin && (
|
||||
<p className="text-slate-600">Last login: {new Date(selectedUser.lastLogin).toLocaleString()}</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end space-x-3 mt-6">
|
||||
<button
|
||||
onClick={() => setShowEditModal(false)}
|
||||
className="btn btn-secondary"
|
||||
>
|
||||
Close
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default UserManagement;
|
||||
export default UserManagement;
|
||||
@@ -1,23 +1,13 @@
|
||||
import React, { useState } from 'react';
|
||||
import { VipFormData } from '../types';
|
||||
import { useToast } from '../contexts/ToastContext';
|
||||
|
||||
interface Flight {
|
||||
flightNumber: string;
|
||||
flightDate: string;
|
||||
segment: number;
|
||||
validated?: boolean;
|
||||
validationData?: any;
|
||||
}
|
||||
|
||||
interface VipFormData {
|
||||
name: string;
|
||||
organization: string;
|
||||
department: 'Office of Development' | 'Admin';
|
||||
transportMode: 'flight' | 'self-driving';
|
||||
flights?: Flight[];
|
||||
expectedArrival?: string;
|
||||
needsAirportPickup?: boolean;
|
||||
needsVenueTransport: boolean;
|
||||
notes: string;
|
||||
validationData?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
interface VipFormProps {
|
||||
@@ -26,6 +16,7 @@ interface VipFormProps {
|
||||
}
|
||||
|
||||
const VipForm: React.FC<VipFormProps> = ({ onSubmit, onCancel }) => {
|
||||
const { showToast } = useToast();
|
||||
const [formData, setFormData] = useState<VipFormData>({
|
||||
name: '',
|
||||
organization: '',
|
||||
|
||||
@@ -1,13 +1,79 @@
|
||||
// API Configuration
|
||||
// VITE_API_URL must be set at build time - no fallback to prevent production issues
|
||||
export const API_BASE_URL = (import.meta as any).env.VITE_API_URL;
|
||||
// Use relative URLs by default so it works with any domain/reverse proxy
|
||||
export const API_BASE_URL = (import.meta as any).env.VITE_API_URL || '';
|
||||
|
||||
if (!API_BASE_URL) {
|
||||
throw new Error('VITE_API_URL environment variable is required');
|
||||
// API Error class
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public status?: number,
|
||||
public code?: string,
|
||||
public details?: unknown
|
||||
) {
|
||||
super(message);
|
||||
this.name = 'ApiError';
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function for API calls
|
||||
export const apiCall = (endpoint: string, options?: RequestInit) => {
|
||||
// Helper function for API calls with error handling
|
||||
export const apiCall = async (endpoint: string, options?: RequestInit) => {
|
||||
const url = endpoint.startsWith('/') ? `${API_BASE_URL}${endpoint}` : endpoint;
|
||||
return fetch(url, options);
|
||||
|
||||
// Get auth token from localStorage
|
||||
const authToken = localStorage.getItem('authToken');
|
||||
|
||||
// Build headers
|
||||
const headers: HeadersInit = {
|
||||
'Content-Type': 'application/json',
|
||||
...options?.headers,
|
||||
};
|
||||
|
||||
// Add authorization header if token exists
|
||||
if (authToken) {
|
||||
headers['Authorization'] = `Bearer ${authToken}`;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
|
||||
// Handle non-2xx responses
|
||||
if (!response.ok) {
|
||||
let errorData;
|
||||
try {
|
||||
errorData = await response.json();
|
||||
} catch {
|
||||
errorData = { error: { message: response.statusText } };
|
||||
}
|
||||
|
||||
throw new ApiError(
|
||||
errorData.error?.message || `Request failed with status ${response.status}`,
|
||||
response.status,
|
||||
errorData.error?.code,
|
||||
errorData.error?.details
|
||||
);
|
||||
}
|
||||
|
||||
// Try to parse JSON response
|
||||
const contentType = response.headers.get('content-type');
|
||||
if (contentType && contentType.includes('application/json')) {
|
||||
const data = await response.json();
|
||||
return { response, data };
|
||||
}
|
||||
|
||||
return { response, data: null };
|
||||
} catch (error) {
|
||||
// Network errors or other issues
|
||||
if (error instanceof ApiError) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw new ApiError(
|
||||
error instanceof Error ? error.message : 'Network request failed',
|
||||
undefined,
|
||||
'NETWORK_ERROR'
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
@import "tailwindcss";
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
/* Custom base styles */
|
||||
@layer base {
|
||||
@@ -10,341 +12,81 @@
|
||||
text-rendering: optimizeLegibility;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
-webkit-text-size-adjust: 100%;
|
||||
|
||||
/* Color scheme variables */
|
||||
color-scheme: light dark;
|
||||
--color-primary: #2563eb;
|
||||
--color-primary-hover: #1d4ed8;
|
||||
--color-secondary: #10b981;
|
||||
--color-secondary-hover: #059669;
|
||||
--color-danger: #ef4444;
|
||||
--color-danger-hover: #dc2626;
|
||||
--color-text: #1f2937;
|
||||
--color-text-secondary: #6b7280;
|
||||
--color-bg: #ffffff;
|
||||
--color-bg-secondary: #f9fafb;
|
||||
--color-border: #e5e7eb;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--color-text: #f9fafb;
|
||||
--color-text-secondary: #d1d5db;
|
||||
--color-bg: #111827;
|
||||
--color-bg-secondary: #1f2937;
|
||||
--color-border: #374151;
|
||||
}
|
||||
}
|
||||
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
min-width: 320px;
|
||||
color: var(--color-text);
|
||||
background-color: var(--color-bg);
|
||||
min-height: 100vh;
|
||||
background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
|
||||
color: #1e293b;
|
||||
}
|
||||
|
||||
#root {
|
||||
width: 100%;
|
||||
margin: 0 auto;
|
||||
text-align: left;
|
||||
|
||||
h1, h2, h3, h4, h5, h6 {
|
||||
font-weight: 600;
|
||||
line-height: 1.3;
|
||||
margin-bottom: 0.5em;
|
||||
}
|
||||
|
||||
/* Smooth scrolling */
|
||||
html {
|
||||
scroll-behavior: smooth;
|
||||
|
||||
a {
|
||||
color: var(--color-primary);
|
||||
text-decoration: none;
|
||||
transition: color 0.2s;
|
||||
}
|
||||
|
||||
/* Focus styles */
|
||||
*:focus {
|
||||
outline: 2px solid #3b82f6;
|
||||
outline-offset: 2px;
|
||||
|
||||
a:hover {
|
||||
color: var(--color-primary-hover);
|
||||
}
|
||||
}
|
||||
|
||||
/* Custom component styles */
|
||||
@layer components {
|
||||
/* Modern Button Styles */
|
||||
.btn {
|
||||
padding-left: 1.5rem;
|
||||
padding-right: 1.5rem;
|
||||
padding-top: 0.75rem;
|
||||
padding-bottom: 0.75rem;
|
||||
border-radius: 0.75rem;
|
||||
font-weight: 600;
|
||||
font-size: 0.875rem;
|
||||
transition: all 0.2s;
|
||||
outline: none;
|
||||
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
.btn:focus {
|
||||
ring: 2px;
|
||||
ring-offset: 2px;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
|
||||
transform: translateY(-0.125rem);
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: linear-gradient(to right, #3b82f6, #2563eb);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-primary:hover {
|
||||
background: linear-gradient(to right, #2563eb, #1d4ed8);
|
||||
}
|
||||
|
||||
.btn-primary:focus {
|
||||
ring-color: #3b82f6;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background: linear-gradient(to right, #64748b, #475569);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-secondary:hover {
|
||||
background: linear-gradient(to right, #475569, #334155);
|
||||
}
|
||||
|
||||
.btn-secondary:focus {
|
||||
ring-color: #64748b;
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background: linear-gradient(to right, #ef4444, #dc2626);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-danger:hover {
|
||||
background: linear-gradient(to right, #dc2626, #b91c1c);
|
||||
}
|
||||
|
||||
.btn-danger:focus {
|
||||
ring-color: #ef4444;
|
||||
}
|
||||
|
||||
.btn-success {
|
||||
background: linear-gradient(to right, #22c55e, #16a34a);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-success:hover {
|
||||
background: linear-gradient(to right, #16a34a, #15803d);
|
||||
}
|
||||
|
||||
.btn-success:focus {
|
||||
ring-color: #22c55e;
|
||||
}
|
||||
|
||||
/* Modern Card Styles */
|
||||
.card {
|
||||
background-color: white;
|
||||
border-radius: 1rem;
|
||||
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
||||
border: 1px solid rgba(226, 232, 240, 0.6);
|
||||
overflow: hidden;
|
||||
backdrop-filter: blur(4px);
|
||||
}
|
||||
|
||||
/* Modern Form Styles */
|
||||
.form-group {
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.form-label {
|
||||
display: block;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
color: #334155;
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.form-input {
|
||||
width: 100%;
|
||||
padding: 0.75rem 1rem;
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: 0.75rem;
|
||||
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
|
||||
background-color: white;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.form-input:focus {
|
||||
outline: none;
|
||||
ring: 2px;
|
||||
ring-color: #3b82f6;
|
||||
border-color: #3b82f6;
|
||||
}
|
||||
|
||||
.form-select {
|
||||
width: 100%;
|
||||
padding: 0.75rem 1rem;
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: 0.75rem;
|
||||
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
|
||||
background-color: white;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.form-select:focus {
|
||||
outline: none;
|
||||
ring: 2px;
|
||||
ring-color: #3b82f6;
|
||||
border-color: #3b82f6;
|
||||
}
|
||||
|
||||
.form-textarea {
|
||||
width: 100%;
|
||||
padding: 0.75rem 1rem;
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: 0.75rem;
|
||||
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
|
||||
background-color: white;
|
||||
transition: all 0.2s;
|
||||
resize: none;
|
||||
}
|
||||
|
||||
.form-textarea:focus {
|
||||
outline: none;
|
||||
ring: 2px;
|
||||
ring-color: #3b82f6;
|
||||
border-color: #3b82f6;
|
||||
}
|
||||
|
||||
.form-checkbox {
|
||||
width: 1.25rem;
|
||||
height: 1.25rem;
|
||||
color: #2563eb;
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: 0.25rem;
|
||||
}
|
||||
|
||||
.form-checkbox:focus {
|
||||
ring: 2px;
|
||||
ring-color: #3b82f6;
|
||||
}
|
||||
|
||||
.form-radio {
|
||||
width: 1rem;
|
||||
height: 1rem;
|
||||
color: #2563eb;
|
||||
border: 1px solid #cbd5e1;
|
||||
}
|
||||
|
||||
.form-radio:focus {
|
||||
ring: 2px;
|
||||
ring-color: #3b82f6;
|
||||
}
|
||||
|
||||
/* Modal Styles */
|
||||
.modal-overlay {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
backdrop-filter: blur(4px);
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
z-index: 50;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.modal-content {
|
||||
background-color: white;
|
||||
border-radius: 1rem;
|
||||
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
|
||||
max-width: 56rem;
|
||||
width: 100%;
|
||||
max-height: 90vh;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.modal-header {
|
||||
background: linear-gradient(to right, #eff6ff, #eef2ff);
|
||||
padding: 1.5rem 2rem;
|
||||
border-bottom: 1px solid rgba(226, 232, 240, 0.6);
|
||||
}
|
||||
|
||||
.modal-body {
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.modal-footer {
|
||||
background-color: #f8fafc;
|
||||
padding: 1.5rem 2rem;
|
||||
border-top: 1px solid rgba(226, 232, 240, 0.6);
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
/* Form Actions */
|
||||
.form-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 1rem;
|
||||
padding-top: 1.5rem;
|
||||
border-top: 1px solid rgba(226, 232, 240, 0.6);
|
||||
margin-top: 2rem;
|
||||
}
|
||||
|
||||
/* Form Sections */
|
||||
.form-section {
|
||||
background-color: #f8fafc;
|
||||
border-radius: 0.75rem;
|
||||
padding: 1.5rem;
|
||||
margin-bottom: 1.5rem;
|
||||
border: 1px solid rgba(226, 232, 240, 0.6);
|
||||
}
|
||||
|
||||
.form-section-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.form-section-title {
|
||||
font-size: 1.125rem;
|
||||
font-weight: 700;
|
||||
color: #1e293b;
|
||||
}
|
||||
|
||||
/* Radio Group */
|
||||
.radio-group {
|
||||
display: flex;
|
||||
gap: 1.5rem;
|
||||
margin-top: 0.75rem;
|
||||
}
|
||||
|
||||
.radio-option {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
cursor: pointer;
|
||||
background-color: white;
|
||||
border-radius: 0.5rem;
|
||||
padding: 0.75rem 1rem;
|
||||
border: 1px solid #e2e8f0;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.radio-option:hover {
|
||||
border-color: #93c5fd;
|
||||
background-color: #eff6ff;
|
||||
}
|
||||
|
||||
.radio-option.selected {
|
||||
border-color: #3b82f6;
|
||||
background-color: #eff6ff;
|
||||
ring: 2px;
|
||||
ring-color: #bfdbfe;
|
||||
}
|
||||
|
||||
/* Checkbox Group */
|
||||
.checkbox-option {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
cursor: pointer;
|
||||
background-color: white;
|
||||
border-radius: 0.5rem;
|
||||
padding: 0.75rem 1rem;
|
||||
border: 1px solid #e2e8f0;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.checkbox-option:hover {
|
||||
border-color: #93c5fd;
|
||||
background-color: #eff6ff;
|
||||
}
|
||||
|
||||
.checkbox-option.checked {
|
||||
border-color: #3b82f6;
|
||||
background-color: #eff6ff;
|
||||
}
|
||||
/* Smooth scrolling */
|
||||
html {
|
||||
scroll-behavior: smooth;
|
||||
}
|
||||
|
||||
/* Custom scrollbar */
|
||||
::-webkit-scrollbar {
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-track {
|
||||
background: var(--color-bg-secondary);
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb {
|
||||
background: var(--color-text-secondary);
|
||||
border-radius: 5px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb:hover {
|
||||
background: var(--color-text);
|
||||
}
|
||||
@@ -2,9 +2,15 @@ import React from 'react'
|
||||
import ReactDOM from 'react-dom/client'
|
||||
import App from './App.tsx'
|
||||
import './index.css'
|
||||
import { ErrorBoundary } from './components/ErrorBoundary'
|
||||
import { ToastProvider } from './contexts/ToastContext'
|
||||
|
||||
ReactDOM.createRoot(document.getElementById('root')!).render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
<ErrorBoundary>
|
||||
<ToastProvider>
|
||||
<App />
|
||||
</ToastProvider>
|
||||
</ErrorBoundary>
|
||||
</React.StrictMode>,
|
||||
)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { apiCall } from '../config/api';
|
||||
import { apiCall } from '../utils/api';
|
||||
|
||||
interface ScheduleEvent {
|
||||
id: string;
|
||||
@@ -83,28 +83,27 @@ const Dashboard: React.FC = () => {
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
|
||||
const [vipsResponse, driversResponse] = await Promise.all([
|
||||
const [vipsResult, driversResult] = await Promise.all([
|
||||
apiCall('/api/vips', { headers: authHeaders }),
|
||||
apiCall('/api/drivers', { headers: authHeaders })
|
||||
]);
|
||||
|
||||
if (!vipsResponse.ok || !driversResponse.ok) {
|
||||
const vipsData = vipsResult.data;
|
||||
const driversData = driversResult.data;
|
||||
|
||||
if (!vipsData || !driversData) {
|
||||
throw new Error('Failed to fetch data');
|
||||
}
|
||||
|
||||
const vipsData = await vipsResponse.json();
|
||||
const driversData = await driversResponse.json();
|
||||
|
||||
// Fetch schedule for each VIP and determine current/next events
|
||||
const vipsWithSchedules = await Promise.all(
|
||||
vipsData.map(async (vip: Vip) => {
|
||||
try {
|
||||
const scheduleResponse = await apiCall(`/api/vips/${vip.id}/schedule`, {
|
||||
const { data: scheduleData } = await apiCall(`/api/vips/${vip.id}/schedule`, {
|
||||
headers: authHeaders
|
||||
});
|
||||
|
||||
if (scheduleResponse.ok) {
|
||||
const scheduleData = await scheduleResponse.json();
|
||||
if (scheduleData) {
|
||||
|
||||
const currentEvent = getCurrentEvent(scheduleData);
|
||||
const nextEvent = getNextEvent(scheduleData);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useParams, Link } from 'react-router-dom';
|
||||
import { apiCall } from '../config/api';
|
||||
import { apiCall } from '../utils/api';
|
||||
import GanttChart from '../components/GanttChart';
|
||||
|
||||
interface DriverScheduleEvent {
|
||||
@@ -42,15 +42,14 @@ const DriverDashboard: React.FC = () => {
|
||||
const fetchDriverSchedule = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall(`/api/drivers/${driverId}/schedule`, {
|
||||
const { data } = await apiCall(`/api/drivers/${driverId}/schedule`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
if (data) {
|
||||
setScheduleData(data);
|
||||
} else {
|
||||
setError('Driver not found');
|
||||
|
||||
@@ -1,23 +1,19 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { apiCall } from '../config/api';
|
||||
import { apiCall } from '../utils/api';
|
||||
import DriverForm from '../components/DriverForm';
|
||||
import EditDriverForm from '../components/EditDriverForm';
|
||||
|
||||
interface Driver {
|
||||
id: string;
|
||||
name: string;
|
||||
phone: string;
|
||||
currentLocation: { lat: number; lng: number };
|
||||
assignedVipIds: string[];
|
||||
vehicleCapacity?: number;
|
||||
}
|
||||
import { Driver, DriverFormData } from '../types';
|
||||
import { useToast } from '../contexts/ToastContext';
|
||||
import { LoadingSpinner } from '../components/LoadingSpinner';
|
||||
|
||||
const DriverList: React.FC = () => {
|
||||
const { showToast } = useToast();
|
||||
const [drivers, setDrivers] = useState<Driver[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [showForm, setShowForm] = useState(false);
|
||||
const [editingDriver, setEditingDriver] = useState<Driver | null>(null);
|
||||
const [searchTerm, setSearchTerm] = useState('');
|
||||
|
||||
// Function to extract last name for sorting
|
||||
const getLastName = (fullName: string) => {
|
||||
@@ -38,19 +34,18 @@ const DriverList: React.FC = () => {
|
||||
const fetchDrivers = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall('/api/drivers', {
|
||||
const { data } = await apiCall('/api/drivers', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
if (data) {
|
||||
const sortedDrivers = sortDriversByLastName(data);
|
||||
setDrivers(sortedDrivers);
|
||||
} else {
|
||||
console.error('Failed to fetch drivers:', response.status);
|
||||
console.error('Failed to fetch drivers');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching drivers:', error);
|
||||
@@ -62,7 +57,7 @@ const DriverList: React.FC = () => {
|
||||
fetchDrivers();
|
||||
}, []);
|
||||
|
||||
const handleAddDriver = async (driverData: any) => {
|
||||
const handleAddDriver = async (driverData: DriverFormData) => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall('/api/drivers', {
|
||||
@@ -78,15 +73,18 @@ const DriverList: React.FC = () => {
|
||||
const newDriver = await response.json();
|
||||
setDrivers(prev => sortDriversByLastName([...prev, newDriver]));
|
||||
setShowForm(false);
|
||||
showToast('Driver added successfully!', 'success');
|
||||
} else {
|
||||
console.error('Failed to add driver:', response.status);
|
||||
showToast('Failed to add driver. Please try again.', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error adding driver:', error);
|
||||
showToast('An error occurred while adding the driver.', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
const handleEditDriver = async (driverData: any) => {
|
||||
const handleEditDriver = async (driverData: DriverFormData) => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall(`/api/drivers/${driverData.id}`, {
|
||||
@@ -104,11 +102,14 @@ const DriverList: React.FC = () => {
|
||||
driver.id === updatedDriver.id ? updatedDriver : driver
|
||||
)));
|
||||
setEditingDriver(null);
|
||||
showToast('Driver updated successfully!', 'success');
|
||||
} else {
|
||||
console.error('Failed to update driver:', response.status);
|
||||
showToast('Failed to update driver. Please try again.', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error updating driver:', error);
|
||||
showToast('An error occurred while updating the driver.', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -129,30 +130,39 @@ const DriverList: React.FC = () => {
|
||||
|
||||
if (response.ok) {
|
||||
setDrivers(prev => prev.filter(driver => driver.id !== driverId));
|
||||
showToast('Driver deleted successfully!', 'success');
|
||||
} else {
|
||||
console.error('Failed to delete driver:', response.status);
|
||||
showToast('Failed to delete driver. Please try again.', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error deleting driver:', error);
|
||||
showToast('An error occurred while deleting the driver.', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex justify-center items-center min-h-64">
|
||||
<div className="bg-white rounded-2xl shadow-lg p-8 flex items-center space-x-4">
|
||||
<div className="w-8 h-8 border-4 border-blue-600 border-t-transparent rounded-full animate-spin"></div>
|
||||
<span className="text-lg font-medium text-slate-700">Loading drivers...</span>
|
||||
</div>
|
||||
<div className="flex justify-center items-center min-h-screen bg-gradient-to-br from-slate-50 to-slate-100">
|
||||
<LoadingSpinner size="lg" message="Loading drivers..." />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Filter drivers based on search term
|
||||
const filteredDrivers = drivers.filter(driver => {
|
||||
const searchLower = searchTerm.toLowerCase();
|
||||
return (
|
||||
driver.name.toLowerCase().includes(searchLower) ||
|
||||
driver.phone.toLowerCase().includes(searchLower)
|
||||
);
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
{/* Header */}
|
||||
<div className="bg-white rounded-2xl shadow-lg p-8 border border-slate-200/60">
|
||||
<div className="flex justify-between items-center">
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold bg-gradient-to-r from-slate-800 to-slate-600 bg-clip-text text-transparent">
|
||||
Driver Management
|
||||
@@ -171,16 +181,53 @@ const DriverList: React.FC = () => {
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Search Bar */}
|
||||
<div className="relative">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Search by name or phone number..."
|
||||
value={searchTerm}
|
||||
onChange={(e) => setSearchTerm(e.target.value)}
|
||||
className="w-full px-4 py-3 pl-12 border border-slate-200 rounded-lg focus:ring-2 focus:ring-amber-500 focus:border-transparent transition-all"
|
||||
/>
|
||||
<svg className="absolute left-4 top-3.5 h-5 w-5 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
|
||||
</svg>
|
||||
{searchTerm && (
|
||||
<button
|
||||
onClick={() => setSearchTerm('')}
|
||||
className="absolute right-4 top-3.5 text-slate-400 hover:text-slate-600"
|
||||
>
|
||||
<svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Search Results */}
|
||||
{searchTerm && (
|
||||
<div className="bg-amber-50 border border-amber-200 rounded-lg px-4 py-2 mb-4">
|
||||
<p className="text-sm text-amber-800">
|
||||
Found {filteredDrivers.length} result{filteredDrivers.length !== 1 ? 's' : ''} for "{searchTerm}"
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Driver Grid */}
|
||||
{drivers.length === 0 ? (
|
||||
{filteredDrivers.length === 0 ? (
|
||||
<div className="bg-white rounded-2xl shadow-lg p-12 border border-slate-200/60 text-center">
|
||||
<div className="w-16 h-16 bg-slate-100 rounded-full flex items-center justify-center mx-auto mb-4">
|
||||
<div className="w-8 h-8 bg-slate-300 rounded-full"></div>
|
||||
</div>
|
||||
<h3 className="text-lg font-semibold text-slate-800 mb-2">No Drivers Found</h3>
|
||||
<p className="text-slate-600 mb-6">Get started by adding your first driver</p>
|
||||
<h3 className="text-lg font-semibold text-slate-800 mb-2">
|
||||
{searchTerm ? 'No Drivers Found' : 'No Drivers Added Yet'}
|
||||
</h3>
|
||||
<p className="text-slate-600 mb-6">
|
||||
{searchTerm ? `No drivers match your search for "${searchTerm}"` : 'Get started by adding your first driver'}
|
||||
</p>
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
onClick={() => setShowForm(true)}
|
||||
@@ -190,7 +237,7 @@ const DriverList: React.FC = () => {
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{drivers.map((driver) => (
|
||||
{filteredDrivers.map((driver) => (
|
||||
<div key={driver.id} className="bg-white rounded-2xl shadow-lg border border-slate-200/60 overflow-hidden hover:shadow-xl transition-shadow duration-200">
|
||||
<div className="p-6">
|
||||
{/* Driver Header */}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { useParams, Link } from 'react-router-dom';
|
||||
import { apiCall } from '../config/api';
|
||||
import { apiCall } from '../utils/api';
|
||||
import FlightStatus from '../components/FlightStatus';
|
||||
import ScheduleManager from '../components/ScheduleManager';
|
||||
|
||||
@@ -37,15 +37,14 @@ const VipDetails: React.FC = () => {
|
||||
const fetchVip = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall('/api/vips', {
|
||||
const { data: vips } = await apiCall('/api/vips', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const vips = await response.json();
|
||||
if (vips) {
|
||||
const foundVip = vips.find((v: Vip) => v.id === id);
|
||||
|
||||
if (foundVip) {
|
||||
@@ -74,15 +73,14 @@ const VipDetails: React.FC = () => {
|
||||
if (vip) {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall(`/api/vips/${vip.id}/schedule`, {
|
||||
const { data: scheduleData } = await apiCall(`/api/vips/${vip.id}/schedule`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const scheduleData = await response.json();
|
||||
if (scheduleData) {
|
||||
setSchedule(scheduleData);
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { apiCall } from '../config/api';
|
||||
import { apiCall } from '../utils/api';
|
||||
import VipForm from '../components/VipForm';
|
||||
import EditVipForm from '../components/EditVipForm';
|
||||
import FlightStatus from '../components/FlightStatus';
|
||||
import { useToast } from '../contexts/ToastContext';
|
||||
import { LoadingSpinner } from '../components/LoadingSpinner';
|
||||
|
||||
interface Vip {
|
||||
id: string;
|
||||
@@ -26,10 +28,13 @@ interface Vip {
|
||||
}
|
||||
|
||||
const VipList: React.FC = () => {
|
||||
const { showToast } = useToast();
|
||||
const [vips, setVips] = useState<Vip[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [showForm, setShowForm] = useState(false);
|
||||
const [editingVip, setEditingVip] = useState<Vip | null>(null);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [searchTerm, setSearchTerm] = useState('');
|
||||
|
||||
// Function to extract last name for sorting
|
||||
const getLastName = (fullName: string) => {
|
||||
@@ -50,19 +55,18 @@ const VipList: React.FC = () => {
|
||||
const fetchVips = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall('/api/vips', {
|
||||
const { data } = await apiCall('/api/vips', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
if (data) {
|
||||
const sortedVips = sortVipsByLastName(data);
|
||||
setVips(sortedVips);
|
||||
} else {
|
||||
console.error('Failed to fetch VIPs:', response.status);
|
||||
console.error('Failed to fetch VIPs');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching VIPs:', error);
|
||||
@@ -75,6 +79,7 @@ const VipList: React.FC = () => {
|
||||
}, []);
|
||||
|
||||
const handleAddVip = async (vipData: any) => {
|
||||
setSubmitting(true);
|
||||
try {
|
||||
const token = localStorage.getItem('authToken');
|
||||
const response = await apiCall('/api/vips', {
|
||||
@@ -90,11 +95,15 @@ const VipList: React.FC = () => {
|
||||
const newVip = await response.json();
|
||||
setVips(prev => sortVipsByLastName([...prev, newVip]));
|
||||
setShowForm(false);
|
||||
showToast('VIP added successfully!', 'success');
|
||||
} else {
|
||||
console.error('Failed to add VIP:', response.status);
|
||||
showToast('Failed to add VIP. Please try again.', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error adding VIP:', error);
|
||||
showToast('An error occurred while adding the VIP.', 'error');
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -114,11 +123,13 @@ const VipList: React.FC = () => {
|
||||
const updatedVip = await response.json();
|
||||
setVips(prev => sortVipsByLastName(prev.map(vip => vip.id === updatedVip.id ? updatedVip : vip)));
|
||||
setEditingVip(null);
|
||||
showToast('VIP updated successfully!', 'success');
|
||||
} else {
|
||||
console.error('Failed to update VIP:', response.status);
|
||||
showToast('Failed to update VIP. Please try again.', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error updating VIP:', error);
|
||||
showToast('An error occurred while updating the VIP.', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -139,30 +150,42 @@ const VipList: React.FC = () => {
|
||||
|
||||
if (response.ok) {
|
||||
setVips(prev => prev.filter(vip => vip.id !== vipId));
|
||||
showToast('VIP deleted successfully!', 'success');
|
||||
} else {
|
||||
console.error('Failed to delete VIP:', response.status);
|
||||
showToast('Failed to delete VIP. Please try again.', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error deleting VIP:', error);
|
||||
showToast('An error occurred while deleting the VIP.', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex justify-center items-center min-h-64">
|
||||
<div className="bg-white rounded-2xl shadow-lg p-8 flex items-center space-x-4">
|
||||
<div className="w-8 h-8 border-4 border-blue-600 border-t-transparent rounded-full animate-spin"></div>
|
||||
<span className="text-lg font-medium text-slate-700">Loading VIPs...</span>
|
||||
</div>
|
||||
<div className="flex justify-center items-center min-h-screen bg-gradient-to-br from-slate-50 to-slate-100">
|
||||
<LoadingSpinner size="lg" message="Loading VIPs..." />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Filter VIPs based on search term
|
||||
const filteredVips = vips.filter(vip => {
|
||||
const searchLower = searchTerm.toLowerCase();
|
||||
return (
|
||||
vip.name.toLowerCase().includes(searchLower) ||
|
||||
vip.organization.toLowerCase().includes(searchLower) ||
|
||||
vip.department.toLowerCase().includes(searchLower) ||
|
||||
(vip.transportMode === 'flight' && vip.flights?.some(flight =>
|
||||
flight.flightNumber.toLowerCase().includes(searchLower)
|
||||
))
|
||||
);
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
{/* Header */}
|
||||
<div className="bg-white rounded-2xl shadow-lg p-8 border border-slate-200/60">
|
||||
<div className="flex justify-between items-center">
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold bg-gradient-to-r from-slate-800 to-slate-600 bg-clip-text text-transparent">
|
||||
VIP Management
|
||||
@@ -176,16 +199,52 @@ const VipList: React.FC = () => {
|
||||
Add New VIP
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Search Bar */}
|
||||
<div className="relative">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Search by name, organization, department, or flight number..."
|
||||
value={searchTerm}
|
||||
onChange={(e) => setSearchTerm(e.target.value)}
|
||||
className="w-full px-4 py-3 pl-12 border border-slate-200 rounded-lg focus:ring-2 focus:ring-amber-500 focus:border-transparent transition-all"
|
||||
/>
|
||||
<svg className="absolute left-4 top-3.5 h-5 w-5 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
|
||||
</svg>
|
||||
{searchTerm && (
|
||||
<button
|
||||
onClick={() => setSearchTerm('')}
|
||||
className="absolute right-4 top-3.5 text-slate-400 hover:text-slate-600"
|
||||
>
|
||||
<svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* VIP List */}
|
||||
{vips.length === 0 ? (
|
||||
{searchTerm && (
|
||||
<div className="bg-amber-50 border border-amber-200 rounded-lg px-4 py-2 mb-4">
|
||||
<p className="text-sm text-amber-800">
|
||||
Found {filteredVips.length} result{filteredVips.length !== 1 ? 's' : ''} for "{searchTerm}"
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{filteredVips.length === 0 ? (
|
||||
<div className="bg-white rounded-2xl shadow-lg p-12 border border-slate-200/60 text-center">
|
||||
<div className="w-16 h-16 bg-slate-100 rounded-full flex items-center justify-center mx-auto mb-4">
|
||||
<div className="w-8 h-8 bg-slate-300 rounded-full"></div>
|
||||
</div>
|
||||
<h3 className="text-lg font-semibold text-slate-800 mb-2">No VIPs Found</h3>
|
||||
<p className="text-slate-600 mb-6">Get started by adding your first VIP</p>
|
||||
<h3 className="text-lg font-semibold text-slate-800 mb-2">
|
||||
{searchTerm ? 'No VIPs Found' : 'No VIPs Added Yet'}
|
||||
</h3>
|
||||
<p className="text-slate-600 mb-6">
|
||||
{searchTerm ? `No VIPs match your search for "${searchTerm}"` : 'Get started by adding your first VIP'}
|
||||
</p>
|
||||
<button
|
||||
className="btn btn-primary"
|
||||
onClick={() => setShowForm(true)}
|
||||
@@ -195,7 +254,7 @@ const VipList: React.FC = () => {
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-4">
|
||||
{vips.map((vip) => (
|
||||
{filteredVips.map((vip) => (
|
||||
<div key={vip.id} className="bg-white rounded-2xl shadow-lg border border-slate-200/60 overflow-hidden hover:shadow-xl transition-shadow duration-200">
|
||||
<div className="p-6">
|
||||
<div className="flex justify-between items-start">
|
||||
|
||||
Reference in New Issue
Block a user