import React, { useState, useEffect } from 'react'; import { subscribeToPush, unsubscribeFromPush } from './pushUtils'; const Settings = ({ user, showToast, onUpdateUser }) => { const [activeTab, setActiveTab] = useState('profile'); // Profile fields const [editName, setEditName] = useState(user?.name || ''); const [editPhone, setEditPhone] = useState(user?.phone || ''); const [profileLoading, setProfileLoading] = useState(false); // Preferences const [notifEmail, setNotifEmail] = useState(true); const [notifPush, setNotifPush] = useState(false); const [womenOnly, setWomenOnly] = useState(user?.women_only || false); useEffect(() => { if ('serviceWorker' in navigator && 'PushManager' in window) { navigator.serviceWorker.ready.then(reg => { reg.pushManager.getSubscription().then(sub => { if (sub) setNotifPush(true); }); }); } }, []); const handleTogglePush = async () => { const newVal = !notifPush; setNotifPush(newVal); const token = localStorage.getItem('token'); if (newVal) { const success = await subscribeToPush(token); if (success) { if (showToast) showToast('Push notifications enabled!', 'success'); } else { setNotifPush(false); if (showToast) showToast('Failed to enable push notifications.', 'danger'); } } else { const success = await unsubscribeFromPush(token); if (success) { if (showToast) showToast('Push notifications disabled.', 'success'); } else { setNotifPush(true); if (showToast) showToast('Failed to disable push notifications.', 'danger'); } } }; const [paymentMode, setPaymentMode] = useState('Corporate Wallet'); const [prefsLoading, setPrefsLoading] = useState(false); const handleSaveProfile = async (e) => { e.preventDefault(); if (!editName.trim()) return showToast('Name cannot be empty.', 'warning'); setProfileLoading(true); try { const res = await fetch(`/api/users/${user.id}/update`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: editName.trim(), phone: editPhone.trim() }) }); const data = await res.json(); if (res.ok) { showToast('Profile updated successfully!', 'success'); if (onUpdateUser) onUpdateUser(data.user); } else { showToast(data.error || 'Failed to update profile.', 'danger'); } } catch { showToast('Network error updating profile.', 'danger'); } finally { setProfileLoading(false); } }; const handleAvatarUpload = async (e) => { const file = e.target.files[0]; if (!file) return; const formData = new FormData(); formData.append('avatar', file); try { const res = await fetch(`/api/users/${user.id}/avatar`, { method: 'POST', body: formData }); const data = await res.json(); if (res.ok) { showToast('Profile picture updated successfully!', 'success'); if (onUpdateUser) onUpdateUser({ ...user, avatar_url: data.avatar_url }); } else { showToast(data.error || 'Failed to update avatar.', 'danger'); } } catch (err) { showToast('Network error uploading image.', 'danger'); } }; const handleSavePrefs = (e) => { e.preventDefault(); setPrefsLoading(true); setTimeout(() => { setPrefsLoading(false); showToast('Preferences saved successfully!', 'success'); }, 800); }; const tabs = [ { id: 'profile', label: 'Profile', icon: 'person' }, { id: 'preferences', label: 'Preferences', icon: 'tune' }, { id: 'security', label: 'Security & SSO', icon: 'security' }, ]; return (
settings

Account Settings

Manage your profile, preferences and security

{/* Tab Nav */}
{tabs.map(tab => ( ))}
{/* Profile Tab */} {activeTab === 'profile' && (
{/* Avatar */}
{user?.avatar_url ? ( Avatar ) : (
{(editName || 'U').charAt(0).toUpperCase()}
)}

{user?.name}

{user?.email}

{user?.role}
setEditName(e.target.value)} required className="glass-input rounded-lg px-4 py-3 text-white outline-none focus:border-primary transition-all" />

Email is managed by your corporate SSO directory

= 2 ? 'text-error-red' : 'text-primary'}`}> {user?.penalty_points || 0} / 3
{user?.penalty_points >= 2 && ( Warning: 1 point away from 15-day suspension. )} {user?.penalty_points > 0 && user?.penalty_points < 2 && ( Please be punctual to avoid penalties. )}

Points are added when reported as a No-Show. Reach 3 points to be suspended.

setEditPhone(e.target.value)} placeholder="+91 9XXXXXXXXX" className="glass-input rounded-lg px-4 py-3 text-white outline-none focus:border-primary transition-all placeholder:text-on-surface-variant/30" />
)} {/* Preferences Tab */} {activeTab === 'preferences' && (
{/* Notification Preferences */}

notifications Notification Preferences

{/* Ride Preferences */}

tune Ride Preferences

{(user?.role === 'Passenger' || user?.role === 'Pool Host') && user?.gender === 'Female' && ( )}
)} {/* Security Tab */} {activeTab === 'security' && (

corporate_fare Corporate SSO Status

verified_user

Reliance Active Directory

Connected & Authenticated

lock Session Management

Your session is secured via JWT token (expires in 24h). All ride data is encrypted in transit.

shield Last login: {new Date().toLocaleDateString('en-IN', { day: '2-digit', month: 'short', year: 'numeric' })} via SSO
)}
); }; export default Settings;