Skip to content

Commit

Permalink
[refactor] boostcampwm-2022#66 boostcampwm-2022#85 닉네임 바꾼 친구 존재 시, 전역…
Browse files Browse the repository at this point in the history
… 변수에 반영해주기

- 전역 key를 nickname에서 id로 변경
- 해당되는 id를 찾아 nickname 변경해주기
  • Loading branch information
ktmihs committed Dec 8, 2022
1 parent 3fd8c67 commit c684751
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion frontend/src/component/Sidebar/Friends/friendItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const FriendItem = ({ friend }: { friend: friendType }) => {

const newFriend = { ...friends };

delete newFriend[nickname];
delete newFriend[id];
setFriends(newFriend);

alert(`${nickname}님을 언팔로우 하였습니다.`);
Expand Down
20 changes: 18 additions & 2 deletions frontend/src/component/Sidebar/Friends/friendList.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import { MouseEvent } from 'react';
import { useRecoilValue } from 'recoil';
import { useRecoilState, useRecoilValue } from 'recoil';
import Content from '../Content';
import FriendItem from './friendItem';
import { friendType } from './friends';
import { friendListWrapper } from './friends.styled';
import { friendsState } from '../../../store/atom/friends';
import Search from './search';
import { socketState } from '../../../store/atom/socket';

const FriendList = () => {
const friends = useRecoilValue(friendsState);
const [friends, setFriends] = useRecoilState(friendsState);
const friendList = Object.values(friends).filter(value => !value.isCalling);
const socket = useRecoilValue(socketState);

socket.on('userDataChanged', data => {
const { id, nickname } = data;

if (!friends[id]) return;

const newFriends = { ...friends };
newFriends[id] = {
...newFriends[id],
nickname: nickname,
};

setFriends(newFriends);
});

const handleDrag = (e: MouseEvent) => {
const target = e.target as HTMLElement;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/component/Sidebar/Friends/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const Search = () => {

setFriends({
...friends,
id: {
[data.userId]: {
id: data.userId,
status: 'offline',
nickname: data.nickname,
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/guard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const routerGuard = () => {

friendList.forEach(
({ userId, nickname }: { userId: string; nickname: string }) => {
initialList[nickname] = {
initialList[userId] = {
id: userId,
status: 'offline',
nickname: nickname,
Expand All @@ -37,6 +37,7 @@ export const routerGuard = () => {
);

setFriends(initialList);
console.log(initialList);
} catch (e) {
console.log(e);
}
Expand Down

0 comments on commit c684751

Please sign in to comment.