source: chapterx-frontend/src/pages/profile/ProfilePage.tsx@ a8f4a2d

main
Last change on this file since a8f4a2d was a8f4a2d, checked in by kikisrbinoska <srbinoskakristina07@…>, 11 days ago

Fixed views count

  • Property mode set to 100644
File size: 9.2 KB
Line 
1import React, { useState, useEffect } from 'react'
2import { useParams, useNavigate } from 'react-router-dom'
3import { BookOpen, Heart, Calendar, MessageCircle, Eye } from 'lucide-react'
4import { useAuthStore } from '../../store/authStore'
5import { useStoryStore } from '../../store/storyStore'
6import { useUIStore } from '../../store/uiStore'
7import { Avatar } from '../../components/ui/Avatar'
8import { RoleBadge, StatusBadge } from '../../components/ui/Badge'
9import { StoryCard } from '../../components/ui/StoryCard'
10import { GenreBadge } from '../../components/ui/Badge'
11import { Modal } from '../../components/ui/Modal'
12import { Button } from '../../components/ui/Button'
13
14type Tab = 'stories' | 'about'
15
16export const ProfilePage: React.FC = () => {
17 const { username } = useParams<{ username: string }>()
18 const navigate = useNavigate()
19 const { allUsers, currentUser, fetchAllUsers, updateUser } = useAuthStore()
20 const { stories, fetchStories } = useStoryStore()
21 const { addToast } = useUIStore()
22 const [tab, setTab] = useState<Tab>('stories')
23 const [loading, setLoading] = useState(false)
24 const [editOpen, setEditOpen] = useState(false)
25 const [editForm, setEditForm] = useState({ username: '', email: '', name: '', surname: '' })
26 const [saving, setSaving] = useState(false)
27
28 useEffect(() => {
29 fetchStories()
30 if (allUsers.length === 0) {
31 setLoading(true)
32 fetchAllUsers().finally(() => setLoading(false))
33 }
34 }, [])
35
36 const user = allUsers.find(u => u.username === username)
37
38 const openEdit = () => {
39 if (!user) return
40 setEditForm({ username: user.username, email: user.email, name: user.name, surname: user.surname })
41 setEditOpen(true)
42 }
43
44 const handleEditSave = async () => {
45 if (!user) return
46 setSaving(true)
47 try {
48 await updateUser(user.user_id, editForm)
49 addToast('Profile updated successfully!')
50 setEditOpen(false)
51 navigate(`/profile/${editForm.username}`, { replace: true })
52 } catch (err: any) {
53 addToast(err.response?.data?.message ?? 'Failed to update profile.', 'error')
54 } finally {
55 setSaving(false)
56 }
57 }
58
59 if (loading) {
60 return (
61 <div className="max-w-4xl mx-auto px-4 py-20 text-center">
62 <p className="text-slate-400">Loading profile...</p>
63 </div>
64 )
65 }
66
67 if (!user) {
68 return (
69 <div className="max-w-4xl mx-auto px-4 py-20 text-center">
70 <h2 className="text-2xl text-white mb-4">User not found</h2>
71 </div>
72 )
73 }
74
75 const userStories = stories.filter(s => s.user_id === user.user_id && s.status === 'published')
76 const totalLikes = userStories.reduce((acc, s) => acc + s.total_likes, 0)
77 const totalComments = userStories.reduce((acc, s) => acc + s.total_comments, 0)
78 const totalViews = userStories.reduce((acc, s) => acc + s.total_views, 0)
79 const allGenres = [...new Set(userStories.flatMap(s => s.genres))]
80
81 return (
82 <div className="max-w-5xl mx-auto px-4 py-8">
83 {/* Profile header */}
84 <div className="relative mb-8">
85 {/* Cover */}
86 <div className="h-32 sm:h-48 rounded-2xl bg-gradient-to-br from-indigo-900 via-violet-900 to-purple-900 mb-16 sm:mb-20 overflow-hidden">
87 <div className="absolute inset-0 rounded-2xl opacity-40">
88 <div className="absolute top-4 left-16 w-32 h-32 rounded-full bg-white/10 blur-2xl" />
89 <div className="absolute bottom-2 right-8 w-24 h-24 rounded-full bg-white/10 blur-xl" />
90 </div>
91 </div>
92
93 {/* Avatar + info */}
94 <div className="absolute bottom-0 left-0 right-0 px-6 flex items-end justify-between">
95 <Avatar name={`${user.name} ${user.surname}`} size="xl" className="ring-4 ring-slate-950" />
96 {currentUser?.user_id === user.user_id && (
97 <button onClick={openEdit} className="mb-2 text-sm text-indigo-400 hover:text-indigo-300 transition-colors">
98 Edit Profile
99 </button>
100 )}
101 </div>
102 </div>
103
104 {/* User info */}
105 <div className="mb-8">
106 <div className="flex items-center gap-3 mb-1">
107 <h1 className="font-serif text-2xl font-bold text-white">
108 {user.name} {user.surname}
109 </h1>
110 <RoleBadge role={user.role} />
111 </div>
112 <p className="text-slate-400">@{user.username}</p>
113 {user.bio && <p className="text-slate-300 mt-3 max-w-xl">{user.bio}</p>}
114 <div className="flex items-center gap-4 mt-4 flex-wrap">
115 <span className="flex items-center gap-1 text-slate-400 text-sm">
116 <Calendar size={14} />
117 Joined {new Date(user.created_at).toLocaleDateString('en-US', { month: 'long', year: 'numeric' })}
118 </span>
119 </div>
120 </div>
121
122 {/* Stats */}
123 <div className="grid grid-cols-2 sm:grid-cols-4 gap-4 mb-8">
124 {[
125 { icon: <BookOpen size={16} className="text-indigo-400" />, value: userStories.length, label: 'Stories' },
126 { icon: <Heart size={16} className="text-rose-400" />, value: totalLikes.toLocaleString(), label: 'Likes' },
127 { icon: <MessageCircle size={16} className="text-amber-400" />, value: totalComments, label: 'Comments' },
128 { icon: <Eye size={16} className="text-cyan-400" />, value: totalViews.toLocaleString(), label: 'Views' },
129 ].map(s => (
130 <div key={s.label} className="bg-slate-800 border border-slate-700 rounded-xl p-4 text-center">
131 <div className="flex justify-center mb-1">{s.icon}</div>
132 <p className="text-xl font-bold text-white">{s.value}</p>
133 <p className="text-slate-500 text-xs">{s.label}</p>
134 </div>
135 ))}
136 </div>
137
138 {/* Tabs */}
139 <div className="flex gap-1 p-1 bg-slate-800 rounded-xl mb-6 w-fit">
140 {(['stories', 'about'] as Tab[]).map(t => (
141 <button
142 key={t}
143 onClick={() => setTab(t)}
144 className={`px-4 py-2 rounded-lg text-sm font-medium capitalize transition-colors ${
145 tab === t ? 'bg-indigo-600 text-white' : 'text-slate-400 hover:text-white'
146 }`}
147 >
148 {t}
149 </button>
150 ))}
151 </div>
152
153 {/* Tab content */}
154 {tab === 'stories' ? (
155 <div>
156 {userStories.length === 0 ? (
157 <div className="text-center py-16 text-slate-500">
158 <BookOpen size={40} className="mx-auto mb-3 opacity-40" />
159 <p>No published stories yet.</p>
160 </div>
161 ) : (
162 <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
163 {userStories.map(story => (
164 <StoryCard key={story.story_id} story={story} />
165 ))}
166 </div>
167 )}
168 </div>
169 ) : (
170 <div className="space-y-6">
171 {allGenres.length > 0 && (
172 <div className="bg-slate-800 border border-slate-700 rounded-2xl p-6">
173 <h3 className="text-white font-semibold mb-3">Writes In</h3>
174 <div className="flex flex-wrap gap-2">
175 {allGenres.map(g => <GenreBadge key={g} genre={g} />)}
176 </div>
177 </div>
178 )}
179 <div className="bg-slate-800 border border-slate-700 rounded-2xl p-6">
180 <h3 className="text-white font-semibold mb-3">Stats</h3>
181 <div className="space-y-3">
182 <div className="flex justify-between text-sm">
183 <span className="text-slate-400">Role</span>
184 <RoleBadge role={user.role} />
185 </div>
186 <div className="flex justify-between text-sm">
187 <span className="text-slate-400">Member since</span>
188 <span className="text-white">{new Date(user.created_at).toLocaleDateString()}</span>
189 </div>
190 <div className="flex justify-between text-sm">
191 <span className="text-slate-400">Published stories</span>
192 <span className="text-white">{userStories.length}</span>
193 </div>
194 </div>
195 </div>
196 </div>
197 )}
198
199 <Modal isOpen={editOpen} onClose={() => setEditOpen(false)} title="Edit Profile">
200 <div className="space-y-4">
201 {[
202 { label: 'First Name', key: 'name' },
203 { label: 'Last Name', key: 'surname' },
204 { label: 'Username', key: 'username' },
205 { label: 'Email', key: 'email' },
206 ].map(({ label, key }) => (
207 <div key={key}>
208 <label className="block text-sm text-slate-400 mb-1.5">{label}</label>
209 <input
210 type={key === 'email' ? 'email' : 'text'}
211 value={editForm[key as keyof typeof editForm]}
212 onChange={e => setEditForm(p => ({ ...p, [key]: e.target.value }))}
213 className="w-full px-4 py-2.5 bg-slate-800 border border-slate-700 rounded-xl text-white placeholder-slate-500 focus:outline-none focus:border-indigo-500"
214 />
215 </div>
216 ))}
217 <div className="flex gap-3 pt-2">
218 <Button variant="ghost" className="flex-1" onClick={() => setEditOpen(false)}>Cancel</Button>
219 <Button className="flex-1" loading={saving} onClick={handleEditSave}>Save Changes</Button>
220 </div>
221 </div>
222 </Modal>
223 </div>
224 )
225}
Note: See TracBrowser for help on using the repository browser.