class ChatsContainer extends PureComponent {
constructor(props) {
super(props)
this.state = {
refreshing: false,
vets: [],
user: this.props.auth,
presenceRef:
firebasefirebase.database().ref('presence/vets')
}
}
componentDidMount() {
this.fetchData()
}
componentDidUpdate(prevState, prevProps) {
if (this.state.vets !== prevState.vets
&& this.props.vets.vets !== prevProps.vets.vets
) {
this.updateStatus()
}
}
componentWillUnmount() {
this.removeListeners()
}
removeListeners = () => {
this.state.presenceRef.off()
}
fetchData = () => {
const idP =
this.props.navigation.state.params.data.id const userID = this.props.auth.currentUserUid
const { getLastMessageForPet, toggleAddStatus } = this.props
const vetsKey = this.props.vets.vetsId
getLastMessageForPet(vetsKey, idP, () => this.addStatusToUser(vetsKey))
this.setState({vets: this.props.vets.vets})
toggleAddStatus(idP, false, userID)
this.updateStatus()
}
updateStatus = () => {
this.state.presenceRef.on('child_added', snap => {
consoleconsole.log(snap.key, 'snap.key')
this.addStatusToUser(snap.key)
})
this.state.presenceRef.on('child_removed', snap => {
this.addStatusToUser(snap.key, false)
})
}
addStatusToUser = (userId, connected = true) => {
const upDateUsers = this.state.vets.reduce((acc, user) => {
if (
user.id === userId) {
user['status'] =
${connected ? 'online' : 'offline'}
}
return acc.concat(user)
}, [])
this.setState({vets: upDateUsers})
}
refreshingStarted = () => {
this.setState({refreshing: true})
this.fetchData()
this.setState({refreshing: false})
}...