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 (
Manage your profile, preferences and security
Reliance Active Directory
Connected & Authenticated
Your session is secured via JWT token (expires in 24h). All ride data is encrypted in transit.