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

Fix: channel switching issue and loading state fix #206

Merged
merged 5 commits into from
Dec 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions frontend/zc_messaging/src/pages/message-board/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ const MessagingBoard = () => {
const [pageIndex, setPageIndex] = useState(1)
const [roomChats, setRoomChats] = useState([])
const [refresh, setRefresh] = useState(false)
const [isFetching, setIsFetching] = useState(false)
const [fileData, setFileData] = useState(null)
const [showEmoji, setShowEmoji] = useState(false)
const [down, setDown] = useState(false)
const [isProcessing, setIsProcessing] = useState([])
const chatSize = 15
// Get messages endpoint
// Get Rooms endpoint
const { data: roomsAvailable, isLoading: IsLoadingRoomsAvailable } =
useGetRoomsAvailableToUserQuery(
{
Expand All @@ -48,25 +49,30 @@ const MessagingBoard = () => {
}
)
// Get messages in a room
const { data: data, isLoading: isLoadingRoomMessages } =
useGetMessagesInRoomQuery(
{
orgId: currentWorkspaceId,
roomId,
pageIndex
},
{
skip: Boolean(!roomId),
refetchOnMountOrArgChange: true
}
)
const {
data: data,
isLoading: isLoadingRoomMessages,
isFetching: isPaginating
} = useGetMessagesInRoomQuery(
{
orgId: currentWorkspaceId,
roomId,
pageIndex
},
{
skip: Boolean(!roomId),
refetchOnMountOrArgChange: true
}
)

// send message endpoint query
const [sendNewMessageWithFile, { isLoading: isPending }] =
useSendMessageWithFileMutation()

const [updateMessage] = useUpdateMessageInRoomMutation()

useEffect(() => {
setIsFetching(true)
if (!roomId) {
fetchDefaultRoom(currentWorkspaceId, authUser?.user_id).then(result => {
navigateTo(`/${result.room_id}`, { replace: true })
Expand Down Expand Up @@ -337,20 +343,24 @@ const MessagingBoard = () => {

const handleScroll = event => {
const numPage = Math.ceil(data.total / chatSize)
if (event.currentTarget.scrollTop === 0 && pageIndex < numPage) {
if (
event.currentTarget.scrollTop === 0 &&
pageIndex < numPage &&
!isPaginating
) {
setPageIndex(prev => prev + 1)
setDown(false)
event.currentTarget.scrollTop =
(event.currentTarget.scrollHeight - event.currentTarget.clientHeight) /
2
}
}
useEffect(() => {
if (data?.roomMessages) {
setIsFetching(false)
if (pageIndex === 1) {
setRoomChats(data?.roomMessages)
} else {
} else if (pageIndex > 1 && !isFetching) {
setRoomChats(data?.roomMessages.concat(roomChats))
} else {
setRoomChats(data?.roomMessages)
}
}
}, [data?.roomMessages])
Expand All @@ -365,8 +375,8 @@ const MessagingBoard = () => {
<MessageWrapper>
<MessageRoomViewHeader name={`#${roomName}`} />
<MessageBoard
isLoadingMessages={isLoadingRoomMessages}
messages={roomChats || []}
isLoadingMessages={isFetching}
messages={isFetching ? [] : roomChats || []}
onSendMessage={sendMessageHandler}
onReact={reactHandler}
onSendAttachedFile={SendAttachedFileHandler}
Expand Down