Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4주차] 이은비 미션 제출합니다 #22

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1b4d440
[chore] 공통 스타일 지정
silverain02 Sep 29, 2023
eb950ab
[chore] 초기세팅 typescript 맞춰 변경
silverain02 Sep 29, 2023
e0575dd
[feat] 채팅박스 컴포넌트
silverain02 Sep 29, 2023
20f7b02
[chore] icon 컴포넌트화
silverain02 Sep 29, 2023
e386d5f
[feat] input컴포넌트
silverain02 Sep 29, 2023
ae09e79
[feat] 입력값 처리 기능
silverain02 Sep 29, 2023
62802b9
[feat] localStorage에 chatData저장
silverain02 Sep 29, 2023
3864efe
[feat] input클릭 시 하단 스크롤 기능
silverain02 Sep 29, 2023
0e7ca9d
[chore] css일부 마진조정
silverain02 Sep 29, 2023
673e270
[chore] package 의존설정 변화
silverain02 Sep 29, 2023
472337b
[chore] 대화 시 사용자 전환 CSS 메시지 박스 변화
silverain02 Sep 29, 2023
134a720
[chore] 같은 시간 보여주지 않기
silverain02 Sep 29, 2023
50f6eae
[feat] 프로필 추가
silverain02 Sep 29, 2023
042a35b
[chore] redux 설치
silverain02 Nov 2, 2023
490b640
[chore]react router 설치
silverain02 Nov 2, 2023
4b8201d
[feat] 페이지 라우팅 적용
silverain02 Nov 2, 2023
bd359ad
[feat] footer구현
silverain02 Nov 3, 2023
ce23bd6
[chore] userData 세팅
silverain02 Nov 3, 2023
0f008bc
[chore] profile 스타일링
silverain02 Nov 3, 2023
add8959
[feat]친구리스트 배열 매핑
silverain02 Nov 3, 2023
bf9286c
[chore] 친구리스트 스타일링
silverain02 Nov 3, 2023
e03d921
[feat]myPage완성
silverain02 Nov 3, 2023
204a243
[feat]chatList mock data불러오기
silverain02 Nov 3, 2023
e1a59cb
[feat]chatList구현
silverain02 Nov 3, 2023
8201dc3
[feat]경로변경 후 userMock데이터 받아오기
silverain02 Nov 3, 2023
d821755
[feat]chatData연동
silverain02 Nov 3, 2023
f13b150
[feat]input기능 재건
silverain02 Nov 3, 2023
1a35cb6
[chore]mock data수정
silverain02 Nov 3, 2023
71f3f8b
[chore]말풍선 시간 조정
silverain02 Nov 3, 2023
567f19f
[chore] link색조정
silverain02 Nov 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions public/data/chatData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"userId": 4,
"chatData": [
{ "userId": 4, "isLike": false, "chat": "어디냐", "time": "15:00" },
{ "userId": 0, "isLike": false, "chat": "집", "time": "15:40" },
{ "userId": 4, "isLike": false, "chat": "중도가자", "time": "15:50" }
]
},
{
"userId": 5,
"chatData": [
{ "userId": 5, "isLike": false, "chat": "과제안함?", "time": "18:00" },
{ "userId": 0, "isLike": false, "chat": "할거야", "time": "18:50" },
{ "userId": 5, "isLike": false, "chat": "언제", "time": "18:55" },
{
"userId": 0,
"isLike": false,
"chat": "어쩌라고 화나게하지마",
"time": "19:00"
}
]
}
]
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function App() {
<Routes>
<Route path="/" element={<FriendList />} />
<Route path="/chatlist" element={<ChatList />} />
{/* <Route path="/chatroom/:cid" element={<ChatRoom />} /> */}
<Route path="/chatroom/:userId" element={<ChatRoom />} />
<Route path="/mypage" element={<MyPage />} />
</Routes>
);
Expand Down
42 changes: 26 additions & 16 deletions src/components/ChatListBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import styled from 'styled-components';
import theme from '../style/theme';
import ProfileSmallIcon from '../static/ProfileSmallIcon';
import ProfileIcon from '../static/PropfileIcon';
import { Route, Routes } from 'react-router-dom';
import { Link, useLocation } from 'react-router-dom';

interface ChatListBoxProps {
userId: number;
userName: string;
comment: string;
newNum: number;
Expand All @@ -16,24 +19,30 @@ const ChatListBox: React.FC<ChatListBoxProps> = ({
comment,
newNum,
latestTime,
userId,
}) => {
return (
<Wrapper>
<ProfileSmallIcon width={56} height={56} />
<RightWrapper>
<RightWrapperTop>
<SubText>{userName}</SubText>
<Caption>{latestTime}</Caption>
</RightWrapperTop>
<RightWrapperBottom>
<Caption>{comment}</Caption>
<NewNum>{newNum}</NewNum>
</RightWrapperBottom>
</RightWrapper>
</Wrapper>
<StyledLink to={`/chatroom/${userId}`}>
<Wrapper>
<ProfileSmallIcon width={56} height={56} />
<RightWrapper>
<RightWrapperTop>
<SubText>{userName}</SubText>
<Caption>{latestTime}</Caption>
</RightWrapperTop>
<RightWrapperBottom>
<Caption>{comment}</Caption>
<NewNum>{newNum}</NewNum>
</RightWrapperBottom>
</RightWrapper>
</Wrapper>
</StyledLink>
);
};

const StyledLink = styled(Link)`
text-decoration: none;
`;
const SubText = styled.div`
font-style: ${theme.fonts.subText};
`;
Expand All @@ -52,8 +61,8 @@ const NewNum = styled.div`
font-style: ${theme.fonts.caption1};
background-color: ${theme.colors.purple};
border-radius: 99px;
width: 20px;
height: 20px;
width: 25px;
height: 25px;
`;
const RightWrapperBottom = styled.div`
display: flex;
Expand All @@ -77,7 +86,7 @@ const RightWrapper = styled.div`
justify-content: space-between;
align-items: center;
width: 80%;
gap: 5px;
gap: 7px;
`;

const Wrapper = styled.div`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

개인적인 생각인데, styled-component 이름을 명명함에 있어서 Wrapper 보다는 좀 더 구체적인 명명이 좋지 않을까 생각합니다! 만약 page별로 동일한 Wrapper를 적용한다면, 최상위 컴포넌트에 동일한 스타일을 지정해줄 수도 있을 거 같아용

Expand All @@ -86,6 +95,7 @@ const Wrapper = styled.div`
justify-content: space-between;
align-items: center;
padding-left: 10px;
padding-right: 20px;
padding-top: 1rem;
padding-bottom: 1rem;

Expand Down
1 change: 1 addition & 0 deletions src/pages/ChatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const ChatList = () => {
<Body>
{chatData.map((it) => (
<ChatListBox
userId={it.userId}
userName={it.userName}
latestTime={it.latestTime}
comment={it.latestMent}
Expand Down
114 changes: 21 additions & 93 deletions src/pages/ChatRoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,108 +6,36 @@ import Header from '../components/Header';
import ProfileSmallIcon from '../static/ProfileSmallIcon';
import ChatInputBar from '../components/ChatInputBar';
import { useEffect, useState, useRef } from 'react';
import { useParams } from 'react-router-dom';

interface UserInfo {
id: number;
interface UserData {
id: string;
userName: string;
comment: string;
}

const ChatRoom: React.FC<UserInfo> = ({ id }) => {
const userIdArr: string[] = ['이은비', '윤서윤'];
const [userId, setUserId] = useState(id);
const [inputText, setInputText] = useState('');
const [isInputFocused, setInputFocused] = useState(false);

const bodyRef = React.useRef<HTMLDivElement | null>(null);

const initialData = JSON.parse(
localStorage.getItem('chatData') || '[]'
) as any[];
const [chatData, setChatData] = useState<any[]>(initialData);

const handleHeaderClick = () => {
// id를 0과 1 사이에서 토글
setUserId((prevId) => (prevId === 0 ? 1 : 0));
};

// 날짜 형식을 지정하는 함수
const getCurrentDate = () => {
const now = new Date();
// const year = now.getFullYear();
// const month = String(now.getMonth() + 1).padStart(2, '0');
// const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
return `${hours}:${minutes}`;
};
interface ChatData {
userId: string;
isLike: boolean;
chat: string;
time: string;
}

//inputText변동시 chatData추가
const ChatRoom = () => {
const { userId } = useParams();
const [userData, setUserData] = useState<UserData[]>([]);
useEffect(() => {
const currentDate = getCurrentDate();
if (inputText.trim() !== '') {
setChatData((prevChatData) => {
if (prevChatData) {
return [
...prevChatData,
{
userId: userId,
isLike: false,
chat: inputText,
time: currentDate,
},
];
} else {
return [
{
userId: userId,
isLike: false,
chat: inputText,
time: currentDate,
},
];
}
fetch('/data/userData.json')
.then((res) => res.json())
.then((result) => {
setUserData(result);
console.log(userData);
});
}
}, [inputText]);

useEffect(() => {
if (isInputFocused && bodyRef.current) {
bodyRef.current.scrollTop = bodyRef.current.scrollHeight;
}
}, [isInputFocused]);

// chatData가 업데이트될 때마다 로컬 스토리지에 저장
useEffect(() => {
localStorage.setItem('chatData', JSON.stringify(chatData));
}, [chatData]);
}, []);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fetch를 사용하셔서 나중에 백 쪽과 연결할 때 편리할 것 같습니다:)


return (
<Wrapper>
<Header text={userIdArr[userId]} onClick={handleHeaderClick} />
<Body ref={bodyRef}>
{chatData.map((chatItem, index) => (
<ChatBox
text={chatItem.chat}
hasTail={false}
isMe={chatItem.userId === userId}
time={
index === 0 || chatData[index - 1].time !== chatItem.time
? chatItem.time
: ''
}
isFirst={
index === 0 || chatData[index - 1].userId !== chatItem.userId
}
user={userIdArr[chatItem.userId]}
/>
))}
</Body>

<ChatInputBar
inputText={inputText}
setInputText={setInputText}
isInputFocused={isInputFocused}
setInputFocused={setInputFocused}
/>
{userData.map((it) => it.id === userId && <div>{it.userName}</div>)}
</Wrapper>
);
};
Expand Down