Skip to content

Commit

Permalink
allow edit of user name (#328)
Browse files Browse the repository at this point in the history
* allow edit of user name

* fix bugs regarding socket

* re-use addUserInfo to update user info

* update edit user info logic
  • Loading branch information
vinmaster committed Oct 21, 2022
1 parent e813cde commit e1767f1
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
13 changes: 12 additions & 1 deletion client/src/components/Sidebar/UserList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import React, { useContext } from 'react'
import { UserInfoContext } from "@/contexts";
import { UserInfoContext, SocketContext } from "@/contexts";
import { UserInfo } from "@/contexts/UserInfoContext";
import AvatarDOM from "@/components/AvatarCanvas/AvatarDOM"
import { SIOChannel } from "@/contexts/SocketIOContext";

const UserList = () => {
// TODO: Need to have selfUserInfo here as well
const { userInfos } = useContext(UserInfoContext);
const { socket } = useContext(SocketContext);

const editName = () => {
const newName = prompt('Enter new name');
if (newName) {
socket.emit(SIOChannel.EDIT_USER_NAME, newName);
}
};

// FIXME: Styling of user list
return (
<>
Expand All @@ -24,6 +34,7 @@ const UserList = () => {
mute={userInfo.mute}
/>
<span>{userInfo.name}</span>
{userInfo.id === socket.id && <button className="edit-btn" onClick={() => editName()}>Edit</button>}
</div>
))}
</>
Expand Down
4 changes: 4 additions & 0 deletions client/src/components/Sidebar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ button[data-active=inactive] {
}
}

.edit-btn {
color: black;
margin-left: auto;
}
}

.sidebar[data-open=open] {
Expand Down
1 change: 1 addition & 0 deletions client/src/contexts/SocketIOContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export enum SIOChannel {
ANSWER = "ANSWER",
OFFER = "OFFER",
NUM_USERS = "NUM_USERS",
EDIT_USER_NAME = "EDIT_USER_NAME",
CONNECT_ERROR = "connect_error", // Predefined string by socketio.
}

Expand Down
13 changes: 12 additions & 1 deletion client/src/contexts/UserInfoContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ export const UserInfoProvider = ({ children }: { children: JSX.Element }): JSX.E
const { [action.id]: _toRemove, ...removed } = userInfos;
return removed;
}
case "editUserName": {
Object.keys(userInfos).forEach(key => {
if (userInfos[key].id === action.id && action.data.name) {
userInfos[key].name = action.data.name;
}
})
return { ...userInfos };
}
default:
return userInfos;
}
Expand All @@ -75,8 +83,11 @@ export const UserInfoProvider = ({ children }: { children: JSX.Element }): JSX.E
const removeUserInfo = useCallback((userId: string) => {
dispatch({ type: "remove", data: {}, id: userId });
}, []);
const editUserName = useCallback((userId: string, name: string) => {
dispatch({ type: "editUserName", data: { name }, id: userId });
}, []);
return (
<UserInfoContext.Provider value={{ userInfos, addUserInfo, removeUserInfo }}>
<UserInfoContext.Provider value={{ userInfos, addUserInfo, removeUserInfo, editUserName }}>
{children}
</UserInfoContext.Provider>
);
Expand Down
15 changes: 14 additions & 1 deletion client/src/pages/RoomPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function RoomPage({ name }: { name: string }): JSX.Element {
const visualizerRef = useRef(visualizer);
const [selfUserInfo, setSelfUserInfo] = useState<UserInfo>({ ...defaultUserInfo, name });
const selfUserInfoRef = useRef(selfUserInfo);
const { userInfos, addUserInfo, removeUserInfo } = useContext(UserInfoContext);
const { userInfos, addUserInfo, removeUserInfo, editUserName } = useContext(UserInfoContext);
const userInfosRef = useRef(userInfos)
const history = useHistory();
const [showModal, setShowModal] = useState(false);
Expand Down Expand Up @@ -156,6 +156,19 @@ function RoomPage({ name }: { name: string }): JSX.Element {
}
});

socket.on(SIOChannel.EDIT_USER_NAME, ({ id, name }) => {
let newInfo = null as unknown as UserInfo;
if (id === socket.id) {
newInfo = { ...selfUserInfoRef.current, name };
selfUserInfoRef.current = newInfo;
setSelfUserInfo(newInfo);
} else {
newInfo = { ...userInfosRef.current, name };
userInfosRef.current = newInfo;
}
addUserInfo(newInfo);
});

updateVisualizer(new AudioVisualizer(onAudioActivity));

window.onbeforeunload = () => {
Expand Down
4 changes: 4 additions & 0 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ io.of((nsp, query, next) => {
io.of(roomName).to(sdpSenderId).emit("SDP_RECEIVED");
});

socket.on("EDIT_USER_NAME", (name) => {
io.of(roomName).emit("EDIT_USER_NAME", { id: socket.id, name });
});

socket.on("LEAVE", () => {
io.of(roomName).emit("LEAVE", { id: socket.id });
});
Expand Down

0 comments on commit e1767f1

Please sign in to comment.