File size: 2,962 Bytes
fd52f31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect } from 'react';
import './UserModal.css';

const UserModal = ({ isOpen, onClose, token }) => {
    const [user, setUser] = useState(null);
    const [newPassword, setNewPassword] = useState('');
    const [message, setMessage] = useState('');

    useEffect(() => {
        if (isOpen && token) {
            fetchUserDetails();
        }
    }, [isOpen, token]);

    const fetchUserDetails = async () => {
        try {
            const response = await fetch('http://localhost:8000/user/me', {
                headers: {
                    'Authorization': `Bearer ${token}`
                }
            });
            if (response.ok) {
                const data = await response.json();
                setUser(data);
            }
        } catch (error) {
            console.error('Error fetching user details:', error);
        }
    };

    const handlePasswordUpdate = async (e) => {
        e.preventDefault();
        try {
            const response = await fetch('http://localhost:8000/user/update-password', {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${token}`
                },
                body: JSON.stringify({ password: newPassword })
            });

            if (response.ok) {
                setMessage('Password updated successfully');
                setNewPassword('');
            } else {
                const error = await response.json();
                setMessage(error.detail);
            }
        } catch (error) {
            setMessage('Failed to update password');
        }
    };

    if (!isOpen) return null;

    return (
        <div className="modal-overlay">
            <div className="modal-content">
                <button className="modal-close" onClick={onClose}>&times;</button>
                <h2>User Profile</h2>
                {user && (
                    <div className="user-info">
                        <p><strong>Username:</strong> {user.username}</p>
                        <form onSubmit={handlePasswordUpdate}>
                            <div className="form-group">
                                <label>New Password:</label>
                                <input
                                    type="password"
                                    value={newPassword}
                                    onChange={(e) => setNewPassword(e.target.value)}
                                    placeholder="Enter new password"
                                />
                            </div>
                            <button type="submit" className="update-btn">Update Password</button>
                        </form>
                        {message && <p className="message">{message}</p>}
                    </div>
                )}
            </div>
        </div>
    );
};

export default UserModal;