From 044f3abb2f0165f6f663b2f39380f19b29e26679 Mon Sep 17 00:00:00 2001 From: marshennikovaolga Date: Sat, 7 Sep 2024 15:31:44 +0300 Subject: [PATCH] edit profile --- .../(pages)/profile/_components/wrapper.tsx | 88 ++++++++++++------- 1 file changed, 58 insertions(+), 30 deletions(-) diff --git a/web/app/(pages)/profile/_components/wrapper.tsx b/web/app/(pages)/profile/_components/wrapper.tsx index a236a42e..725583d0 100644 --- a/web/app/(pages)/profile/_components/wrapper.tsx +++ b/web/app/(pages)/profile/_components/wrapper.tsx @@ -1,12 +1,12 @@ "use client" import { useAccount } from "@/lib/providers/jazz-provider" +import { useState, useRef, useCallback } from "react" 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 Link from "next/link" import { Button } from "@/components/ui/button" +import Link from "next/link" +import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar" interface ProfileStatsProps { number: number @@ -30,31 +30,46 @@ export const ProfileWrapper = () => { const [isEditing, setIsEditing] = useState(false) const [newName, setNewName] = useState(account.me?.profile?.name || "") - const [originalName, setOriginalName] = useState(account.me?.profile?.name || "") + const [error, setError] = useState("") const editProfileClicked = () => { - setIsEditing(!isEditing) + setIsEditing(true) + setError("") } const changeName = (e: React.ChangeEvent) => { 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 = () => { - if (newName.trim() !== "") { - if (account.me && account.me.profile) { - account.me.profile.name = newName.trim() - } - setOriginalName(newName.trim()) - setIsEditing(false) + const validationError = validateName(newName) + if (validationError) { + setError(validationError) + return } + + if (account.me && account.me.profile) { + account.me.profile.name = newName.trim() + console.log("Updating name to:", newName.trim()) + } + setIsEditing(false) } - const clickOutside = () => { - if (isEditing) { - setNewName(originalName) - setIsEditing(false) - } + const cancelEditing = () => { + setNewName(account.me?.profile?.name || "") + setIsEditing(false) + setError("") } const editAvatar = (event: React.ChangeEvent) => { @@ -75,7 +90,7 @@ export const ProfileWrapper = () => {

The link you followed may be broken, or the page may have been removed. Go back to - homepage + homepage .

@@ -87,7 +102,7 @@ export const ProfileWrapper = () => { return (
-

Profile

+

Profile

{username}

@@ -102,22 +117,35 @@ export const ProfileWrapper = () => {
{isEditing ? ( - + <> + + {error &&

{error}

} + ) : (

{account.me?.profile?.name}

)}
- + {isEditing ? ( +
+ + +
+ ) : ( + + )}