diff --git a/app/page.js b/app/page.js
index e5969b9..087ea2b 100644
--- a/app/page.js
+++ b/app/page.js
@@ -70,8 +70,11 @@ export default function Home() {
} catch (err) {
setError(err.message || 'An unexpected error occurred')
} finally {
- setTimeout(() => setLoading(false), 1000)
- setTimeout(() => setShortenedUrl(''), 1000) // Clear message after 1 seconds
+ setTimeout(() => {
+ setLoading(false)
+ setShortenedUrl('')
+ }, 1000)
+ // Clear message after 1 seconds
}
}
@@ -103,7 +106,6 @@ export default function Home() {
}),
})
- console.log(response.status)
if (response.status === 400) {
// Assuming 409 is returned by the backend when there's a duplicate URL
setError(
diff --git a/app/profile/page.js b/app/profile/page.js
new file mode 100644
index 0000000..538889f
--- /dev/null
+++ b/app/profile/page.js
@@ -0,0 +1,189 @@
+'use client'
+
+import Cookies from 'js-cookie'
+import { useRef, useState } from 'react'
+import { FcCamera } from 'react-icons/fc'
+import Image from 'next/image'
+
+export default function ProfilePage() {
+ const webURL = process.env.NEXT_PUBLIC_WEB_URL
+ const imgRef = useRef(null)
+ const [form, setForm] = useState({
+ image: '',
+ firstName: '',
+ lastName: '',
+ email: '',
+ })
+ const [previewFile, setPreviewFile] = useState('')
+ const [token, setToken] = useState(Cookies.get('jwt'))
+ const [userData, setUserData] = useState(null)
+ const [loading, setLoading] = useState(false)
+ const [message, setMessage] = useState('')
+
+ const getUserDetails = async () => {
+ try {
+ const response = await fetch(webURL + 'auth/me', {
+ method: 'GET',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization: `Bearer ${token}`,
+ },
+ })
+
+ if (!response.ok) {
+ throw new Error('Failed to fetch user data')
+ }
+
+ const data = await response.json()
+ console.log(data)
+ setUserData(data)
+ setForm({
+ image: '',
+ firstName: data.firstName || '',
+ lastName: data.lastName || '',
+ email: data.email || '',
+ })
+ } catch (error) {
+ console.error('Error fetching user data:', error)
+ }
+ }
+
+ if (token && !userData) {
+ getUserDetails()
+ }
+
+ const handleFileChange = (e) => {
+ const file = e.target.files[0]
+
+ if (file) {
+ const imageUrl = URL.createObjectURL(file)
+ setPreviewFile(imageUrl)
+ setForm((prevForm) => ({
+ ...prevForm,
+ image: imageUrl,
+ }))
+ }
+ }
+
+ const handleSubmit = async (e) => {
+ e.preventDefault()
+
+ setLoading(true)
+ try {
+ const response = await fetch(webURL + 'auth/me', {
+ method: 'PUT',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization: `Bearer ${token}`,
+ },
+ body: JSON.stringify(form),
+ })
+
+ if (!response.ok) {
+ throw new Error('Failed to update profile')
+ }
+
+ const updatedUser = await response.json()
+ setUserData(updatedUser)
+
+ setMessage(updatedUser.message)
+
+ // update form with the new user data
+ setForm((prevForm) => ({
+ ...prevForm,
+ firstName: updatedUser.firstName || prevForm.firstName,
+ lastName: updatedUser.lastName || prevForm.lastName,
+ email: updatedUser.email || prevForm.email,
+ }))
+
+ setLoading(false)
+ } catch (error) {
+ console.error('Error updating profile:', error)
+ }
+ }
+
+ return (
+
+ )
+}
diff --git a/public/default-avatar.jpg b/public/default-avatar.jpg
new file mode 100644
index 0000000..10cfb2f
Binary files /dev/null and b/public/default-avatar.jpg differ