Skip to content

Commit

Permalink
πŸ› μƒˆλ‘œμš΄ μ±„νŒ…λ°© μƒμ„±μ‹œ μƒκΈ°λŠ” 버그 ν•΄κ²° boostcampwm-2021#116
Browse files Browse the repository at this point in the history
  • Loading branch information
HanCiHu committed Nov 23, 2021
1 parent 1d8d9a1 commit bcb10ed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions client/src/components/chat/chat-new-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styled, { css } from 'styled-components';
import { useRecoilState, useRecoilValue } from 'recoil';
import { useHistory } from 'react-router-dom';

import useChatSocket from '@utils/chat-socket';
import { ChatHeaderStyle } from '@components/chat/style';
import { postChatRoom } from '@api/index';
import selectedUserType from '@atoms/chat-selected-users';
Expand Down Expand Up @@ -39,6 +40,7 @@ const DoneBtn = () => {
const [selectedUserList, setSelectedUserList] = useRecoilState(selectedUserType);
const user = useRecoilValue(userType);
const history = useHistory();
const chatSocket = useChatSocket();

const makeChatRoom = () => {
if (selectedUserList.length === 0) return;
Expand All @@ -49,6 +51,10 @@ const DoneBtn = () => {
state: { participantsInfo: selectedUserList },
});
setSelectedUserList([]);
chatSocket.emit('chat:makeChat', {
chatDocumentId: res.chatRoomId,
participantsInfo: [...selectedUserList, { userDocumentId: user.userDocumentId, userName: user.userName, profileUrl: user.profileUrl }],
});
});
};

Expand Down
8 changes: 8 additions & 0 deletions client/src/views/chat-rooms-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ function ChatRoomsViews() {
});
};

const newChatRooms = (payload: any) => {
const { chatDocumentId, participantsInfo } = payload;
setChatRooms((oldRooms: any) => [{
chatDocumentId, participants: participantsInfo, lastMsg: '', recentActive: new Date(), unCheckedMsg: 0,
}, ...oldRooms]);
};

useEffect(() => {
getChatRooms(userDocumentId)
.then((res: any) => {
Expand All @@ -76,6 +83,7 @@ function ChatRoomsViews() {
if (!socket) return;
socket.emit('chat:viewJoin', userDocumentId);
socket.on('chat:alertMsg', setNewRooms);
socket.on('chat:makeChat', newChatRooms);
}, [socket]);

if (loading) return (<LoadingSpinner />);
Expand Down
9 changes: 9 additions & 0 deletions server/src/sockets/chat.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable array-callback-return */
/* eslint-disable prefer-destructuring */
/* eslint-disable max-len */
import { Socket } from 'socket.io';
Expand Down Expand Up @@ -32,9 +33,17 @@ export default function chatEventHandler(socket : Socket) {
});
};

const makeChatHandler = ({ participantsInfo, chatDocumentId }: any) => {
participantsInfo.map((user: any) => {
const newParticipants = participantsInfo.filter((participant: any) => participant.userDocumentId !== user.userDocumentId);
socket.to(user.userDocumentId).emit('chat:makeChat', { chatDocumentId, participantsInfo: newParticipants });
});
};

socket.on('chat:roomJoin', chatRoomJoinHandler);
socket.on('chat:viewJoin', chatViewJoinHandler);
socket.on('chat:sendMsg', sendMsgHandler);
socket.on('chat:leave', chatLeaveHandler);
socket.on('chat:alertMsg', alertMsgHandler);
socket.on('chat:makeChat', makeChatHandler);
}

0 comments on commit bcb10ed

Please sign in to comment.