Created
July 13, 2022 01:43
-
-
Save waldothedeveloper/c4716444871e8b3b064e1262752d3003 to your computer and use it in GitHub Desktop.
useConfigureChat.js
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
import { useMemo, useState } from "react"; | |
import { chatFetchers } from "../../utils/chatFetchers"; | |
import { selectedConversation } from "../../components/shared/selectedConversation"; | |
import { useAddSMSParticipant } from "./useAddSMSParticipant"; | |
import { useCreateAccessToken } from "./useCreateAccessToken"; | |
import { useCreateConversation } from "./useCreateConversation"; | |
import { useDeliveryReceipt } from "./useDeliveryReceipt"; | |
import { useInitializeConversationClient } from "./useInitializeConversationClient"; | |
import { useJoinAndLeaveConversations } from "./useJoinAndLeaveConversations"; | |
import { useSendAndReceiveMessages } from "./useSendAndReceiveMessages"; | |
import { useUpdateUser } from "../../utils/fireStoreMethods"; | |
export const useConfigureChat = (userInfo) => { | |
const { updateUser } = useUpdateUser(); | |
const [newOrGetConversationData, setCreateOrGetConversation] = useState({ | |
token: null, | |
selectedConversationSid: null, | |
conversationCreated: false, | |
}); | |
const { conversationCreated, token } = newOrGetConversationData; | |
const { refDocumentId } = userInfo || null; | |
const { createNewConversation, addSMSParticipant, createTokenForChat } = | |
chatFetchers(userInfo); | |
/* | |
DOWN HERE WE WILL IMPLEMENT NEW CONVERSATIONS LOGIC | |
*/ | |
const { createConversation, createConversationError } = useCreateConversation( | |
userInfo, | |
createNewConversation, | |
conversationCreated | |
); | |
const { addSMSParticipantData, smsParticipantError } = useAddSMSParticipant( | |
createConversation, | |
conversationCreated, | |
addSMSParticipant | |
); | |
/* | |
we are getting the token from state instead of the tokenData hook, but this is on purpose to be able to refresh the token on the useCreateAccessToken hook when is about to expire or it already expired | |
*/ | |
const { conversationData } = useInitializeConversationClient(token); | |
const { client } = conversationData; | |
const { tokenData, tokenError } = useCreateAccessToken( | |
createConversation, | |
conversationCreated, | |
createTokenForChat, | |
userInfo | |
); | |
/* | |
SOME ADDITIONAL HOOKS NEEDED TO IMPLEMENT THE CHAT | |
*/ | |
const { conversations } = useJoinAndLeaveConversations(client); | |
const { conversationMessage, handleChange, handleSubmit } = | |
useSendAndReceiveMessages(selectedConversation(conversations, userInfo)); | |
const { messages } = conversationMessage; | |
// console.log(`messages DO THEY CHANGE?: `, messages); | |
const { deliveryReceipt, deliveryReceiptError } = useDeliveryReceipt( | |
messages, | |
userInfo | |
); | |
// console.log(`deliveryReceipt: `, deliveryReceipt); | |
// console.log(`deliveryReceiptError: `, deliveryReceiptError); | |
/* | |
update the firebase user with the sid & chatServiceSid after creating | |
a new conversation only if a successful new conversation has been created | |
*/ | |
useMemo(() => { | |
let active = true; | |
if ( | |
active && | |
refDocumentId && | |
createConversation && | |
createConversation.status === 200 && | |
tokenData && | |
tokenData.status === 200 | |
) { | |
updateUser({ | |
refDocumentId: refDocumentId, | |
sid: createConversation.sid, | |
chat_service_sid: createConversation.chatServiceSid, | |
}) | |
.then(() => { | |
// console.log(`User updated successfully!`); | |
setCreateOrGetConversation((oldData) => { | |
return { | |
...oldData, | |
conversationCreated: true, | |
token: tokenData.token, | |
}; | |
}); | |
}) | |
.catch((err) => | |
// if the user is NOT updated we have a problem | |
// make sure you handle this error | |
console.log(`Error trying to update the firebase user`, err) | |
); | |
} | |
return () => (active = false); | |
}, [createConversation, refDocumentId, tokenData, updateUser]); | |
useMemo(() => { | |
let active = true; | |
if (userInfo && active) { | |
setCreateOrGetConversation((oldData) => { | |
return { | |
...oldData, | |
conversationCreated: false, | |
}; | |
}); | |
} | |
return () => (active = false); | |
}, [userInfo]); | |
useMemo(() => { | |
if (tokenData && tokenData.status === 200) { | |
setCreateOrGetConversation((oldData) => { | |
return { | |
...oldData, | |
token: tokenData.token, | |
}; | |
}); | |
} | |
}, [tokenData]); | |
return { | |
conversationData, | |
conversations, | |
conversationMessage, | |
handleChange, | |
handleSubmit, | |
deliveryReceipt, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment