File size: 7,199 Bytes
aa58194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
210
211
212
213
214
import React, { useEffect, useState } from 'react';
import { useAuth } from '../context/AuthContext';
import { getJson } from '../utils/api';
import '../styles/UserProfile.css';

const readFileAsDataUrl = (file) =>
  new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = () => resolve(reader.result);
    reader.onerror = () => reject(new Error('Could not load the selected image'));
    reader.readAsDataURL(file);
  });

const UserProfile = ({ user }) => {
  const [isEditing, setIsEditing] = useState(false);
  const [username, setUsername] = useState(user?.username || '');
  const [avatarUrl, setAvatarUrl] = useState(user?.avatarUrl || '');
  const [loading, setLoading] = useState(false);
  const [message, setMessage] = useState('');
  const { token, setUser, logout } = useAuth();
  const inviteLink = user?.referralCode
    ? `${window.location.origin}/signup?ref=${encodeURIComponent(user.referralCode)}`
    : '';

  useEffect(() => {
    setUsername(user?.username || '');
    setAvatarUrl(user?.avatarUrl || '');
  }, [user]);

  const handleAvatarChange = async (e) => {
    const file = e.target.files?.[0];
    if (!file) return;

    try {
      const dataUrl = await readFileAsDataUrl(file);
      setAvatarUrl(dataUrl);
    } catch (err) {
      setMessage(err.message || 'Could not update profile image');
    }
  };

  const handleUpdateProfile = async (e) => {
    e.preventDefault();
    setLoading(true);
    setMessage('');

    try {
      const data = await getJson('/api/user/profile', {
        method: 'PUT',
        headers: {
          'Content-Type': 'application/json',
          Authorization: `Bearer ${token}`,
        },
        body: JSON.stringify({ username, avatarUrl }),
      });

      setUser(data.user);
      setMessage('Profile updated successfully!');
      setIsEditing(false);
      setTimeout(() => setMessage(''), 3000);
    } catch (err) {
      setMessage(err.message || 'Failed to update profile');
    } finally {
      setLoading(false);
    }
  };

  const handleCopyInvite = async () => {
    if (!inviteLink) return;

    try {
      await navigator.clipboard.writeText(inviteLink);
      setMessage('Invite link copied successfully!');
    } catch (err) {
      setMessage('Invite link copy nahi ho paya');
    }
  };

  return (
    <div className="user-profile">

      <h2>Your Profile</h2>



      {message && <div className="success-message">{message}</div>}



      <div className="profile-card">

        <div className="profile-avatar">

          {avatarUrl ? (

            <img src={avatarUrl} alt={user?.username || 'Profile'} className="avatar-image" />

          ) : (

            <div className="avatar-placeholder">

              {user?.username?.charAt(0).toUpperCase()}

            </div>

          )}

        </div>



        <div className="profile-info">

          <div className="profile-stats" aria-label="Profile stats">

            <div className="profile-stat-card">

              <strong>{user?.tokenBalance ?? 0}</strong>

              <span>Tokens</span>

            </div>

            <div className="profile-stat-card">

              <strong>{user?.followersCount ?? 0}</strong>

              <span>Followers</span>

            </div>

            <div className="profile-stat-card">

              <strong>{user?.followingCount ?? 0}</strong>

              <span>Following</span>

            </div>

          </div>



          {!isEditing ? (

            <>

              <div className="info-row">

                <label>Username</label>

                <p>{user?.username}</p>

              </div>

              <div className="info-row">

                <label>Email</label>

                <p>{user?.email || 'Not added'}</p>

              </div>

              <div className="info-row">

                <label>Mobile</label>

                <p>{user?.phone || 'Not added'}</p>

              </div>

              <div className="info-row">

                <label>Referral Code</label>

                <p>{user?.referralCode || 'Generating...'}</p>

              </div>

              <div className="invite-panel">

                <div>

                  <strong>Invite & Earn</strong>

                  <p>Har successful invite par 70 tokens milenge.</p>

                </div>

                <button type="button" className="edit-btn" onClick={handleCopyInvite} disabled={!inviteLink}>

                  Copy Invite Link

                </button>

              </div>

              <button className="edit-btn" onClick={() => setIsEditing(true)}>

                Edit Profile

              </button>

            </>

          ) : (

            <form onSubmit={handleUpdateProfile}>

              <div className="form-group">

                <label>Profile Picture</label>

                <div className="avatar-editor">

                  <label className="avatar-upload-btn">

                    Choose Image

                    <input

                      type="file"

                      accept="image/*"

                      onChange={handleAvatarChange}

                    />

                  </label>

                  <button

                    type="button"

                    className="avatar-remove-btn"

                    onClick={() => setAvatarUrl('')}

                  >

                    Remove

                  </button>

                </div>

              </div>



              <div className="form-group">

                <label>Username</label>

                <input

                  type="text"

                  value={username}

                  onChange={(e) => setUsername(e.target.value)}

                  required

                />

              </div>

              <div className="form-group">

                <label>Email (read-only)</label>

                <input type="email" value={user?.email || ''} disabled />

              </div>

              <div className="form-group">

                <label>Mobile (read-only)</label>

                <input type="text" value={user?.phone || ''} disabled />

              </div>

              <div className="button-group">

                <button type="submit" className="save-btn" disabled={loading}>

                  {loading ? 'Saving...' : 'Save Changes'}

                </button>

                <button

                  type="button"

                  className="cancel-btn"

                  onClick={() => {

                    setIsEditing(false);

                    setUsername(user?.username || '');

                    setAvatarUrl(user?.avatarUrl || '');

                  }}

                >

                  Cancel

                </button>

              </div>

            </form>

          )}

        </div>



        <div className="profile-actions">

          <button className="logout-btn" onClick={logout} title="Logout">

            Logout

          </button>

        </div>

      </div>
    </div>
  );
};

export default UserProfile;