Spaces:
Paused
Paused
File size: 9,064 Bytes
5eb4857 fc874c6 5eb4857 fc874c6 5eb4857 c83f886 5eb4857 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | import React, { useState } from 'react';
import { api } from '@nexus/shared';
const COLORS = {
brand: "#1E40AF",
error: "#DC2626",
success: "#10B981",
gray50: "#F9FAFB",
gray100: "#F3F4F6",
gray200: "#E5E7EB",
gray500: "#6B7280",
gray700: "#374151",
gray800: "#1F2937",
gray900: "#111827",
};
interface UserSettingsModalProps {
user: any;
onClose: () => void;
onUpdate: (updatedUser: any) => void;
}
export const UserSettingsModal: React.FC<UserSettingsModalProps> = ({ user, onClose, onUpdate }) => {
const [tab, setTab] = useState<'profile' | 'security'>('profile');
const [loading, setLoading] = useState(false);
const [message, setMessage] = useState<{ type: 'success' | 'error', text: string } | null>(null);
// Profile Form
const [profileForm, setProfileForm] = useState({
full_name: user?.full_name || '',
avatar_url: user?.avatar_url || ''
});
// Security Form
const [securityForm, setSecurityForm] = useState({
old_password: '',
new_password: '',
confirm_password: ''
});
const handleProfileUpdate = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setMessage(null);
try {
const resp = await api.patch('users/me/', profileForm);
onUpdate(resp.data);
setMessage({ type: 'success', text: 'Profile updated successfully!' });
} catch (err) {
setMessage({ type: 'error', text: 'Failed to update profile.' });
} finally {
setLoading(false);
}
};
const handlePasswordUpdate = async (e: React.FormEvent) => {
e.preventDefault();
if (securityForm.new_password !== securityForm.confirm_password) {
setMessage({ type: 'error', text: 'Passwords do not match.' });
return;
}
setLoading(true);
setMessage(null);
try {
await api.post('auth/change-password/', {
old_password: securityForm.old_password,
new_password: securityForm.new_password
});
setMessage({ type: 'success', text: 'Password changed successfully!' });
setSecurityForm({ old_password: '', new_password: '', confirm_password: '' });
} catch (err) {
setMessage({ type: 'error', text: 'Incorrect current password or update failed.' });
} finally {
setLoading(false);
}
};
return (
<div style={{
position: 'fixed',
top: 0,
left: 0,
right: 0,
bottom: 0,
background: 'rgba(0,0,0,0.4)',
backdropFilter: 'blur(4px)',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
zIndex: 2000,
animation: 'nexus-fade-in 0.2s ease-out'
}}>
<style>
{` @keyframes nexus-fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes nexus-pop-in { from { transform: scale(0.95); opacity: 0; } to { transform: scale(1); opacity: 1; } }
`}
</style>
<div style={{
width: 480,
background: '#fff',
borderRadius: 20,
boxShadow: '0 20px 50px rgba(0,0,0,0.2)',
overflow: 'hidden',
animation: 'nexus-pop-in 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275)'
}}>
<div style={{ padding: '24px', borderBottom: `1px solid ${COLORS.gray100}`, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<h2 style={{ fontSize: 18, fontWeight: 800, color: COLORS.gray900, margin: 0 }}>Personal Settings</h2>
<button onClick={onClose} style={{ background: 'none', border: 'none', fontSize: 24, cursor: 'pointer', color: COLORS.gray500 }}>×</button>
</div>
<div style={{ display: 'flex', borderBottom: `1px solid ${COLORS.gray100}`, background: COLORS.gray50 }}>
<button
onClick={() => { setTab('profile'); setMessage(null); }}
style={{
flex: 1, padding: '14px', border: 'none', background: 'none', cursor: 'pointer',
fontSize: 13, fontWeight: 700, color: tab === 'profile' ? COLORS.brand : COLORS.gray500,
borderBottom: tab === 'profile' ? `2px solid ${COLORS.brand}` : 'none'
}}
>
👤 Profile Details
</button>
<button
onClick={() => { setTab('security'); setMessage(null); }}
style={{
flex: 1, padding: '14px', border: 'none', background: 'none', cursor: 'pointer',
fontSize: 13, fontWeight: 700, color: tab === 'security' ? COLORS.brand : COLORS.gray500,
borderBottom: tab === 'security' ? `2px solid ${COLORS.brand}` : 'none'
}}
>
🔒 Security & Password
</button>
</div>
<div style={{ padding: '32px' }}>
{message && (
<div style={{
padding: '12px 16px', borderRadius: 8, marginBottom: 20, fontSize: 13, fontWeight: 600,
background: message.type === 'success' ? '#ECFDF5' : '#FEF2F2',
color: message.type === 'success' ? COLORS.success : COLORS.error,
border: `1px solid ${message.type === 'success' ? '#A7F3D0' : '#FECACA'}`
}}>
{message.text}
</div>
)}
{tab === 'profile' ? (
<form onSubmit={handleProfileUpdate}>
<div style={{ marginBottom: 16 }}>
<label style={{ display: 'block', fontSize: 12, fontWeight: 700, color: COLORS.gray500, marginBottom: 8, textTransform: 'uppercase' }}>Full Name</label>
<input
value={profileForm.full_name}
onChange={e => setProfileForm({...profileForm, full_name: e.target.value})}
style={{ width: '100%', padding: '12px', borderRadius: 8, border: `1px solid ${COLORS.gray200}`, fontSize: 14 }}
/>
</div>
<div style={{ marginBottom: 24 }}>
<label style={{ display: 'block', fontSize: 12, fontWeight: 700, color: COLORS.gray500, marginBottom: 8, textTransform: 'uppercase' }}>Avatar URL</label>
<input
value={profileForm.avatar_url}
onChange={e => setProfileForm({...profileForm, avatar_url: e.target.value})}
placeholder="https://example.com/photo.jpg"
style={{ width: '100%', padding: '12px', borderRadius: 8, border: `1px solid ${COLORS.gray200}`, fontSize: 14 }}
/>
</div>
<button disabled={loading} style={{ width: '100%', padding: '14px', background: COLORS.brand, color: '#fff', border: 'none', borderRadius: 8, fontWeight: 700, cursor: 'pointer', opacity: loading ? 0.7 : 1 }}>
{loading ? 'Saving Changes...' : 'Update Profile'}
</button>
</form>
) : (
<form onSubmit={handlePasswordUpdate}>
<div style={{ marginBottom: 16 }}>
<label style={{ display: 'block', fontSize: 12, fontWeight: 700, color: COLORS.gray500, marginBottom: 8, textTransform: 'uppercase' }}>Current Password</label>
<input
type="password"
value={securityForm.old_password}
onChange={e => setSecurityForm({...securityForm, old_password: e.target.value})}
style={{ width: '100%', padding: '12px', borderRadius: 8, border: `1px solid ${COLORS.gray200}`, fontSize: 14 }}
/>
</div>
<div style={{ marginBottom: 16 }}>
<label style={{ display: 'block', fontSize: 12, fontWeight: 700, color: COLORS.gray500, marginBottom: 8, textTransform: 'uppercase' }}>New Password</label>
<input
type="password"
value={securityForm.new_password}
onChange={e => setSecurityForm({...securityForm, new_password: e.target.value})}
style={{ width: '100%', padding: '12px', borderRadius: 8, border: `1px solid ${COLORS.gray200}`, fontSize: 14 }}
/>
</div>
<div style={{ marginBottom: 24 }}>
<label style={{ display: 'block', fontSize: 12, fontWeight: 700, color: COLORS.gray500, marginBottom: 8, textTransform: 'uppercase' }}>Confirm New Password</label>
<input
type="password"
value={securityForm.confirm_password}
onChange={e => setSecurityForm({...securityForm, confirm_password: e.target.value})}
style={{ width: '100%', padding: '12px', borderRadius: 8, border: `1px solid ${COLORS.gray200}`, fontSize: 14 }}
/>
</div>
<button disabled={loading} style={{ width: '100%', padding: '14px', background: COLORS.brand, color: '#fff', border: 'none', borderRadius: 8, fontWeight: 700, cursor: 'pointer', opacity: loading ? 0.7 : 1 }}>
{loading ? 'Updating Password...' : 'Change Password'}
</button>
</form>
)}
</div>
</div>
</div>
);
};
|