From 4345e98ff8128c6a7ecb50e3f3b909f968df19eb Mon Sep 17 00:00:00 2001 From: oikkoikk Date: Fri, 6 Oct 2023 14:03:04 +0900 Subject: [PATCH] [SWM-339] Fix : ProfileDropdown input --- src/components/gnb/NavBar.tsx | 19 ++++----- src/components/gnb/ProfileDropdown.tsx | 57 ++++++++++++++++++-------- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/components/gnb/NavBar.tsx b/src/components/gnb/NavBar.tsx index 4dbb1f4..cf4bf61 100644 --- a/src/components/gnb/NavBar.tsx +++ b/src/components/gnb/NavBar.tsx @@ -24,22 +24,21 @@ export default function NavBar({ logo, profileDropdown }: Props) { const [isEditMode, setIsEditMode] = useState(false); const [name, setName] = useState(session?.name ?? ''); - const profile = session?.profile; + const profileImage = session?.profile; const navBarHidden = session ? '' : 'hidden'; - const profileButtonClickHandler = async () => { - if (isEditMode) { - mutate(); - await update({ ...session, name }); - } + const profileButtonClickHandler = async (name: string) => { setIsEditMode((prev) => !prev); + + mutate(); + await update({ ...session, name }); }; const { mutate } = useMutation(() => updateUserProfile(name), { onSuccess: (data) => { setIsEditMode(false); - setName(data.name); + setName(() => data.name); } }); @@ -84,13 +83,13 @@ export default function NavBar({ logo, profileDropdown }: Props) { type='button' className={`${navBarHidden} dropdown dropdown-end md:dropdown-hover text-sroom-black-400`} > - {profile && ( + {profileImage && ( )} diff --git a/src/components/gnb/ProfileDropdown.tsx b/src/components/gnb/ProfileDropdown.tsx index 2199ed3..54f7a15 100644 --- a/src/components/gnb/ProfileDropdown.tsx +++ b/src/components/gnb/ProfileDropdown.tsx @@ -7,45 +7,67 @@ import Link from 'next/link'; import { useCallback, useEffect, useRef } from 'react'; type Props = { - profile: string; - name: string; + profileImage: string; isEditMode: boolean; + setIsEditMode: React.Dispatch>; profileDropdown?: ProfileDropdown[]; - setName: React.Dispatch>; - profileButtonClickHandler: () => Promise; + name: string; + profileButtonClickHandler: (name: string) => Promise; }; export default function ProfileDropdown({ - profile, - name, + profileImage, isEditMode, + setIsEditMode, profileDropdown, - setName, + name, profileButtonClickHandler }: Props) { const inputRef = useRef(null); const enterKeyDownHandler = useCallback( - async (e: string) => { - if (e === 'Enter') { - await profileButtonClickHandler(); + (e: string) => { + if (inputRef.current && e === 'Enter') { + inputRef.current.value = + inputRef.current.value.trim().slice(0, 10) ?? ''; + + const name = inputRef.current.value; + + if (name === '') return; + profileButtonClickHandler(inputRef.current.value); } }, [profileButtonClickHandler] ); - useEffect(() => { - if (inputRef.current === null) return; + const saveProfileButtonClickHandler = useCallback(async () => { + if (inputRef.current && isEditMode) { + inputRef.current.value = inputRef.current.value.trim().slice(0, 10) ?? ''; + const name = inputRef.current.value; - if (isEditMode) { + if (name === '') return; + + await profileButtonClickHandler(name); + } else { + setIsEditMode((prev) => !prev); + } + }, [isEditMode, setIsEditMode, profileButtonClickHandler]); + + useEffect(() => { + if (inputRef.current && isEditMode) { inputRef.current.focus(); inputRef.current.setSelectionRange( inputRef.current.value.length, inputRef.current.value.length ); + } + }, [isEditMode]); + + useEffect(() => { + if (inputRef.current) { inputRef.current.value = name; } - }, [name, isEditMode]); + }, [name]); return ( <> @@ -53,10 +75,10 @@ export default function ProfileDropdown({ tabIndex={0} className='z-20 flex items-center justify-between h-full gap-3 rounded-none btn btn-ghost hover:bg-sroom-gray-300' > - {profile && ( + {profileImage && (
프로필 setName(e.target.value.trim())} onKeyDown={(e) => enterKeyDownHandler(e.key)} maxLength={10} className='hidden w-32 p-1 text-xs font-semibold leading-9 text-left resize-none sm:block md:text-sm lg:text-base disabled:bg-sroom-white' @@ -84,7 +105,7 @@ export default function ProfileDropdown({
  • {isEditMode ? '저장하기' : '닉네임 수정'}