edit profile

This commit is contained in:
marshennikovaolga
2024-09-07 15:31:44 +03:00
parent 833e5eff82
commit 044f3abb2f

View File

@@ -1,12 +1,12 @@
"use client" "use client"
import { useAccount } from "@/lib/providers/jazz-provider" import { useAccount } from "@/lib/providers/jazz-provider"
import { useState, useRef, useCallback } from "react"
import { useParams } from "next/navigation" import { useParams } from "next/navigation"
import { useState, useRef } from "react"
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar"
import { Input } from "@/components/ui/input" import { Input } from "@/components/ui/input"
import Link from "next/link"
import { Button } from "@/components/ui/button" import { Button } from "@/components/ui/button"
import Link from "next/link"
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar"
interface ProfileStatsProps { interface ProfileStatsProps {
number: number number: number
@@ -30,31 +30,46 @@ export const ProfileWrapper = () => {
const [isEditing, setIsEditing] = useState(false) const [isEditing, setIsEditing] = useState(false)
const [newName, setNewName] = useState(account.me?.profile?.name || "") const [newName, setNewName] = useState(account.me?.profile?.name || "")
const [originalName, setOriginalName] = useState(account.me?.profile?.name || "") const [error, setError] = useState("")
const editProfileClicked = () => { const editProfileClicked = () => {
setIsEditing(!isEditing) setIsEditing(true)
setError("")
} }
const changeName = (e: React.ChangeEvent<HTMLInputElement>) => { const changeName = (e: React.ChangeEvent<HTMLInputElement>) => {
setNewName(e.target.value) setNewName(e.target.value)
setError("")
} }
const validateName = useCallback((name: string) => {
if (name.trim().length < 2) {
return "Name must be at least 2 characters long"
}
if (name.trim().length > 40) {
return "Name must not exceed 40 characters"
}
return ""
}, [])
const saveProfile = () => { const saveProfile = () => {
if (newName.trim() !== "") { const validationError = validateName(newName)
if (account.me && account.me.profile) { if (validationError) {
account.me.profile.name = newName.trim() setError(validationError)
} return
setOriginalName(newName.trim())
setIsEditing(false)
}
} }
const clickOutside = () => { if (account.me && account.me.profile) {
if (isEditing) { account.me.profile.name = newName.trim()
setNewName(originalName) console.log("Updating name to:", newName.trim())
}
setIsEditing(false) setIsEditing(false)
} }
const cancelEditing = () => {
setNewName(account.me?.profile?.name || "")
setIsEditing(false)
setError("")
} }
const editAvatar = (event: React.ChangeEvent<HTMLInputElement>) => { const editAvatar = (event: React.ChangeEvent<HTMLInputElement>) => {
@@ -75,7 +90,7 @@ export const ProfileWrapper = () => {
<p className="mb-5 text-center text-lg font-semibold"> <p className="mb-5 text-center text-lg font-semibold">
The link you followed may be broken, or the page may have been removed. Go back to The link you followed may be broken, or the page may have been removed. Go back to
<Link href="/"> <Link href="/">
<span className="">homepage</span> <span className=""> homepage</span>
</Link> </Link>
. .
</p> </p>
@@ -87,7 +102,7 @@ export const ProfileWrapper = () => {
return ( return (
<div className="flex flex-1 flex-col text-black dark:text-white"> <div className="flex flex-1 flex-col text-black dark:text-white">
<div className="flex items-center justify-between p-[20px]"> <div className="flex items-center justify-between p-[20px]">
<p className="text-2xl font-semibold">Profile</p> <p className="text-2xl font-semibold opacity-70">Profile</p>
</div> </div>
<p className="text-2xl font-semibold">{username}</p> <p className="text-2xl font-semibold">{username}</p>
<div className="flex flex-col items-center border-b border-neutral-900 bg-inherit pb-5"> <div className="flex flex-col items-center border-b border-neutral-900 bg-inherit pb-5">
@@ -102,22 +117,35 @@ export const ProfileWrapper = () => {
<div className="ml-6 flex-1"> <div className="ml-6 flex-1">
{isEditing ? ( {isEditing ? (
<>
<Input <Input
value={newName} value={newName}
onChange={changeName} onChange={changeName}
className="border-result mb-3 mr-3 text-[25px] font-semibold" className="border-result mb-3 mr-3 text-[25px] font-semibold"
onBlur={clickOutside}
/> />
{error && <p className="text-red-500 text-opacity-70">{error}</p>}
</>
) : ( ) : (
<p className="mb-3 text-[25px] font-semibold">{account.me?.profile?.name}</p> <p className="mb-3 text-[25px] font-semibold">{account.me?.profile?.name}</p>
)} )}
</div> </div>
{isEditing ? (
<div>
<Button onClick={saveProfile} className="mr-2">
Save
</Button>
<Button onClick={cancelEditing} variant="outline">
Cancel
</Button>
</div>
) : (
<Button <Button
onClick={isEditing ? saveProfile : editProfileClicked} onClick={editProfileClicked}
className="shadow-outer ml-auto flex h-[34px] cursor-pointer flex-row items-center justify-center space-x-2 rounded-lg bg-white px-3 text-center font-medium text-black shadow-[1px_1px_1px_1px_rgba(0,0,0,0.3)] hover:bg-black/10 dark:bg-[#222222] dark:text-white dark:hover:opacity-60" className="shadow-outer ml-auto flex h-[34px] cursor-pointer flex-row items-center justify-center space-x-2 rounded-lg bg-white px-3 text-center font-medium text-black shadow-[1px_1px_1px_1px_rgba(0,0,0,0.3)] hover:bg-black/10 dark:bg-[#222222] dark:text-white dark:hover:opacity-60"
> >
{isEditing ? "Save" : "Edit profile"} Edit profile
</Button> </Button>
)}
</div> </div>
</div> </div>
<div className="mt-10 flex justify-center"> <div className="mt-10 flex justify-center">