Last active
October 27, 2021 12:17
-
-
Save hieptl/02f955ebcd923928585871fca4b23650 to your computer and use it in GitHub Desktop.
Home.tsx - Search Users, Search Groups - Ionic Chat App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
useEffect(() => { | |
if (cometChat) { | |
listenForMessages(); | |
} | |
return () => { | |
if (cometChat) { | |
cometChat.removeMessageListener(listenerID); | |
} | |
setData(null); | |
} | |
}, [cometChat, selectedType]); | |
... | |
const updateUnreadData = (data: any) => { | |
const updatedData = { ...data }; | |
const updatedUnreadCount = data.unreadCount ? data.unreadCount + 1 : 1; | |
updatedData.unreadCount = updatedUnreadCount; | |
return updatedData; | |
}; | |
const updateUnreadCountMessage = (id: any) => { | |
if (id) { | |
setData((prevData: any) => { | |
return prevData.map((data: any) => { | |
if (selectedType === 0 && data.uid === id) { | |
return updateUnreadData(data); | |
} else if (selectedType === 1 && data.guid === id) { | |
return updateUnreadData(data); | |
} | |
return data; | |
}); | |
}); | |
} | |
} | |
const getUnreadUpdatedId = (message: any) => { | |
const receiverType = message.receiverType; | |
if (receiverType === 'group') { | |
return message.receiverId; | |
} else if (receiverType === 'user') { | |
return message.sender.uid; | |
} | |
return null; | |
}; | |
const markAsDelivered = (message: any) => { | |
if (message) { | |
cometChat.markAsDelivered(message); | |
} | |
}; | |
const listenForMessages = useCallback(() => { | |
cometChat.addMessageListener( | |
listenerID, | |
new cometChat.MessageListener({ | |
onTextMessageReceived: (textMessage: any) => { | |
if (textMessage) { | |
markAsDelivered(textMessage); | |
const updatedId = getUnreadUpdatedId(textMessage); | |
if (updatedId) { | |
updateUnreadCountMessage(updatedId); | |
} | |
} | |
}, | |
onMediaMessageReceived: (mediaMessage: any) => { | |
if (mediaMessage) { | |
markAsDelivered(mediaMessage); | |
const updatedId = getUnreadUpdatedId(mediaMessage); | |
if (updatedId) { | |
updateUnreadCountMessage(updatedId); | |
} | |
} | |
}, | |
}) | |
); | |
}, [cometChat, selectedType]); | |
... | |
const getUnreadMessageCountForAllUsers = (userList: any) => { | |
cometChat.getUnreadMessageCountForAllUsers().then((array: any) => { | |
const unread = Object.keys(array); | |
if (unread.length > 0) { | |
unread.map(uid => { | |
const index = userList.findIndex((user: any) => user.uid === uid); | |
if (index !== -1) { | |
userList[index].unreadCount = array[uid]; | |
} | |
}); | |
setData(() => userList); | |
} else { | |
setData(() => userList); | |
} | |
}); | |
}; | |
const searchUsers = () => { | |
if (cometChat) { | |
const limit = 30; | |
const usersRequestBuilder = new cometChat.UsersRequestBuilder().setLimit(limit); | |
const usersRequest = keyword ? usersRequestBuilder.setSearchKeyword(keyword).build() : usersRequestBuilder.build(); | |
usersRequest.fetchNext().then( | |
(userList: any) => { | |
getUnreadMessageCountForAllUsers(userList); | |
}, | |
(error: any) => { | |
} | |
); | |
} | |
}; | |
const getUnreadMessageCountForAllGroups = (groupList: any) => { | |
if (groupList && groupList.length !== 0) { | |
cometChat.getUnreadMessageCountForAllGroups().then((array: any) => { | |
const unread = Object.keys(array); | |
if (unread.length > 0) { | |
unread.map(guid => { | |
const index = groupList.findIndex((user: any) => user.guid === guid); | |
if (index !== -1) { | |
groupList[index].unreadCount = array[guid]; | |
} | |
}); | |
setData(() => groupList); | |
} else { | |
setData(() => groupList); | |
} | |
}); | |
} | |
}; | |
const searchGroups = () => { | |
const limit = 30; | |
const groupRequestBuilder = new cometChat.GroupsRequestBuilder().setLimit(limit); | |
const groupsRequest = keyword ? groupRequestBuilder.setSearchKeyword(keyword).build() : groupRequestBuilder.build(); | |
groupsRequest.fetchNext().then( | |
(groupList: any) => { | |
getUnreadMessageCountForAllGroups(groupList); | |
}, | |
(error: any) => { | |
} | |
); | |
}; | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment