Search

채팅룸에 헤더에 대화상대 이름 띄우기

날짜
2023/06/26
담당자 확인
확인 완료
개발 담당자
종미
선빈
기타
담당자 확인 결과
승인
버그 내용
클릭한 이벤트대상의 name을 경로로 띄우고 띄운 값을 디코딩해서 한글로 만들어서 값을 넘겨받아야하는 채팅룸에 전달
버그 유형
기능
버그 작성자
종미
이슈 등록 여부
중요도
진행 상황
반영 완료

오류 개요

일시: 06월26일(월) 23:24

작성자: 정종미

내용

채팅룸 헤더에 클릭한 아이템의 이름값을 넘겨 받아서 띄우기

해결

아이템을 만드는 컴포넌트의 li하나씩 onclick 이벤트로 네비게이트 띄우기
<li className={styles['chat-item']} onClick={() => navigate(`/chat/${chat.name}`)} >
JavaScript
복사
chatitem.jsx
라우터 /chat에 path 값 accountname으로 onClick에서 보내는 chat.name으로 연결
<Route path="/chat"> <Route index element={<ChatList />} /> {/* 변경 예정: chat/{id} */} <Route path=":accountname" element={<ChatRoom />} />
JavaScript
복사
App.js
채팅룸 헤더에서 받은 경로값을 디코딩해서 값으로 넘겨주기
const [currentPath, setCurrentPath] = useState(''); useEffect(() => { const decodedPath = decodeURI( document.location.pathname.replace('/chat/', ''), ); setCurrentPath(decodedPath); }, []); // 리턴 안에다가 헤더에 값을 줘야함으로 currentPath 넘김 return ( <> <Layout chatTitle={currentPath}>
JavaScript
복사
ChatRoom.jsx
헤더타입이 있는 쪽에 챗타이틀을 넣어주고
export default function Header({ type, chatTitle }) { const HeaderUI = { none: <></>, chatHeader: ( <header className={styles['header-wrap']}> <div className={styles['left']}> <button className={styles['btn-back']} onClick={goBack}> <img src={IconArrowLeft} alt="뒤로가기" /> </button> <span className={['chat-title']}>{chatTitle}</span> </div> <button className={styles['btn-more']}> <img src={IconMoreVertical} alt="메뉴보기" /> </button> </header>
JavaScript
복사
Header.jsx
전체 레이아웃을 하는 곳에 가서 헤더 채팅룸에서 chatTitle 넘겨 받은 값을 헤더에 챗타이틀에 넣어줌
// 받는 값 추가 export default function Layout({ children, btnHandler, chatTitle }) { //<생략> const path = document.location.pathname; if (path.includes('/profile/m')) { pathToCheck = '/profile/m'; } else if (path.includes('/profile/')) { pathToCheck = '/profile/:accountname'; } else if (path.includes('/followers/')) { pathToCheck = '/followers'; } else if (path.includes('/followings/')) { pathToCheck = '/followings'; } else if (path.includes('/post/upload')) { pathToCheck = '/post/upload'; } else if (path.includes('/post/')) { pathToCheck = '/post'; } else if (path.includes('/chat/')) { pathToCheck = '/chat/:accountname'; } else { pathToCheck = path; } // 케이스 추가 switch (pathToCheck) { case '/chat/:accountname': } //리턴 안에다가는 헤더 타입과 챗 타이틀을 넘겨서 return ( <> <Header type={headerType} btnHandler={btnHandler} chatTitle={chatTitle} /> <main>{children}</main> {footerType === 'input' || footerType === 'comment' || footerType === 'chatting' ? ( <footer> <Input type={footerType} /> </footer> ) : ( <TabMenu type={footerType} /> )} </> );
JavaScript
복사
Layout.jsx