Toggle navigation
Toggle navigation
This project
Loading...
Sign in
강동현
/
nodejs-game
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Overnap
2021-06-05 22:37:20 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
46d1ac212526a647a326babfcae80a36addcf549
46d1ac21
1 parent
741996bd
Builds for 1 pipeline
failed
in 1 minute 19 seconds
RoomInfo 컴포넌트 추가
Changes
2
Builds
3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
0 deletions
web/src/components/room/RoomInfo.tsx
web/src/pages/Room.tsx
web/src/components/room/RoomInfo.tsx
0 → 100644
View file @
46d1ac2
import React, { useCallback, useContext, useEffect, useState } from 'react';
import { useLocation, useParams } from 'react-router';
import SocketContext from '../../contexts/SocketContext';
import { MessageType } from '../common/types';
import { RoomData, UpdateRoomUser } from './types';
interface RoomInfoLocation {
state: { roomData: RoomData }
}
export const RoomInfo: React.FC = () => {
const socket = useContext(SocketContext);
const location: RoomInfoLocation = useLocation();
const [ roomData, setRoomData ] = useState<RoomData>({
// 기본값
uuid: '0',
name: 'loading...',
maxUsers: 9,
users: []
});
const handleUpdateRoomUser = useCallback((data: UpdateRoomUser) => {
if (data.state == 'added') {
setRoomData({
...roomData,
users: [data.user, ...roomData.users] // TODO: immutable.js로 바꾸면 좋을 듯
});
} else if (data.state == 'removed') {
const newUsers = roomData.users;
const index = newUsers.indexOf(data.user);
if (index < 0) {
console.log('존재하지 않는 유저를 제거 시도');
} else {
newUsers.splice(index, 1);
}
setRoomData({
...roomData,
users: newUsers
});
}
}, [roomData]);
useEffect(() => {
setRoomData(location.state.roomData);
socket.on(MessageType.ROOM_USER_UPDATE, handleUpdateRoomUser);
return () => {
socket.off(MessageType.ROOM_USER_UPDATE, handleUpdateRoomUser);
}
}, []);
return (
<div className='m-3 flex items-center place-content-between'>
<div>{roomData.name}</div>
<div>{roomData.users.length}/{roomData.maxUsers}</div>
</div>
);
}
\ No newline at end of file
web/src/pages/Room.tsx
View file @
46d1ac2
...
...
@@ -7,6 +7,7 @@ import SocketContext from '../contexts/SocketContext';
export const Room: React.FC = () => {
return (
<Main>
<RoomInfo />
</Main>
);
}
\ No newline at end of file
...
...
Please
register
or
login
to post a comment