{ const { usersRef } = this.state; let usersArray = []; usersRef.on("child_added", DataSnapshot => { if (currentUserId !== DataSnapshot.key) { let user = DataSnapshot.val() user["uid"] = DataSnapshot.key user["status"] = "offline"; usersArray.push(user) this.setState({ users: usersArray }) } }) } //Direct Message getChatRoomId = (userId) => { const currentUserId = this.props.user.uid return userId > currentUserId ? `${userId}/${currentUserId}` : `${currentUserId}/${userId}` } //화면에 보이게 하기 renderDire"> { const { usersRef } = this.state; let usersArray = []; usersRef.on("child_added", DataSnapshot => { if (currentUserId !== DataSnapshot.key) { let user = DataSnapshot.val() user["uid"] = DataSnapshot.key user["status"] = "offline"; usersArray.push(user) this.setState({ users: usersArray }) } }) } //Direct Message getChatRoomId = (userId) => { const currentUserId = this.props.user.uid return userId > currentUserId ? `${userId}/${currentUserId}` : `${currentUserId}/${userId}` } //화면에 보이게 하기 renderDire"> { const { usersRef } = this.state; let usersArray = []; usersRef.on("child_added", DataSnapshot => { if (currentUserId !== DataSnapshot.key) { let user = DataSnapshot.val() user["uid"] = DataSnapshot.key user["status"] = "offline"; usersArray.push(user) this.setState({ users: usersArray }) } }) } //Direct Message getChatRoomId = (userId) => { const currentUserId = this.props.user.uid return userId > currentUserId ? `${userId}/${currentUserId}` : `${currentUserId}/${userId}` } //화면에 보이게 하기 renderDire">
//fireabse database import
import 'firebase/compat/auth';
import 'firebase/compat/database';
import 'firebase/compat/storage';
//state
state = {
usersRef: firebase.database().ref("users"),
users: [],
activeChatRoom:""
}
//user
componentDidMount() {
if (this.props.user) {
this.addUsersListeners(this.props.user.uid)
}
}
//User 추가
addUsersListeners = (currentUserId) => {
const { usersRef } = this.state;
let usersArray = [];
usersRef.on("child_added", DataSnapshot => {
if (currentUserId !== DataSnapshot.key) {
let user = DataSnapshot.val()
user["uid"] = DataSnapshot.key
user["status"] = "offline";
usersArray.push(user)
this.setState({ users: usersArray })
}
})
}
//Direct Message
getChatRoomId = (userId) => {
const currentUserId = this.props.user.uid
return userId > currentUserId
? `${userId}/${currentUserId}`
: `${currentUserId}/${userId}`
}
//화면에 보이게 하기
renderDirectMessages = () => {
const { users } = this.state;
return (
users.length > 0 &&
users.map(user => (
<li key={user.uid}
style={{
backgroundColor:user.uid===this.state.activeChatRoom
&&"#ffffff45"
}}
onClick={() => this.changeChatRoom(user)}>
# {user.name}
</li>
))
);
}
//현재 User
const mapStateToProps = state => {
return {
user: state.user.currentUser
}
}