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

allow edit of user name #328

Merged
merged 4 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions client/src/components/Sidebar/UserList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import React, { useContext } from 'react'
import { UserInfoContext } from "@/contexts";
import React, { useContext, useRef, useState } from 'react'
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 userInfosRef = useRef(userInfos);
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 (
<>
{(Object.values(userInfos) as UserInfo[]).map((userInfo: UserInfo, index: number) => (
{(Object.values(userInfosRef.current) as UserInfo[]).map((userInfo: UserInfo, index: number) => (
siberowl marked this conversation as resolved.
Show resolved Hide resolved
userInfo.name &&
<div className="sidebar__userlist-item" key={index}>
<AvatarDOM
Expand All @@ -24,6 +35,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
6 changes: 5 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,10 @@ function RoomPage({ name }: { name: string }): JSX.Element {
}
});

socket.on(SIOChannel.EDIT_USER_NAME, ({ id, name }) => {
editUserName(id, name);
});

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