Skip to content

Instantly share code, notes, and snippets.

@waldothedeveloper
Created June 21, 2022 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waldothedeveloper/a90968d76cbcb48d079146e36c11069c to your computer and use it in GitHub Desktop.
Save waldothedeveloper/a90968d76cbcb48d079146e36c11069c to your computer and use it in GitHub Desktop.
useEffect(() => {
if (userInfo.requestConversation) {
if (userInfo.sid) {
setCreateOrGetConversation((oldData) => {
return {
...oldData,
selectedConversationSid: userInfo.sid,
};
});
const readConversation = fetch(`/api/read_conversation`, {
method: `POST`,
body: JSON.stringify(userInfo),
headers: {
"Content-Type": `application/json`,
},
});
readConversation
.then((res) => res.json())
.then((chatSID) => {
// ! get a list of the chat participants in this conversation to give them a client token
fetch(`/api/get_chat_participants`, {
method: `POST`,
body: JSON.stringify({ sid: userInfo.sid }),
})
.then((res) => res.json())
.then((participants) => {
console.log(`participants LOOK AT THIS HERE: `, participants);
// ! get a client token to initialize the conversation
if (participants) {
fetch(`/api/create_access_token`, {
method: `POST`,
body: JSON.stringify({
serviceSid: chatSID,
// remember this token is for the chat participant
identity: participants,
}),
})
.then((res) => res.json())
.then((data) => {
const { token } = data;
// console.log(`token: `, token);
setCreateOrGetConversation((oldData) => {
return { ...oldData, token: token };
});
})
.catch((err) =>
console.log(`err fetching access token: `, err)
);
}
});
})
.catch((err) => console.log(`err fetching conversation: `, err));
} else {
const newConversation = fetch(`/api/create_conversation`, {
method: `POST`,
body: JSON.stringify(userInfo),
headers: {
"Content-Type": `application/json`,
},
});
newConversation
.then((res) => res.json())
.then((data) => {
console.log(`data from new Conversation`, data);
// save the conversationSid in the user's firebase doc
if (data.sid && data.chatServiceSid) {
updateUser({
refDocumentId: userInfo.refDocumentId,
sid: data.sid,
chat_service_sid: data.chatServiceSid,
}).then(() => {
// add the SMS participant to the conversation
const smsParticipant = fetch(`/api/add_sms_participant`, {
method: `POST`,
body: JSON.stringify({
phone: userInfo.phone,
sid: data.sid,
}),
});
smsParticipant
.then((res) => res.json())
.then((data) => {
console.log(`data from sms participant: `, data);
// add the chat participant to the conversation
const chatParticipant = fetch(`/api/add_chat_participant`, {
method: `POST`,
body: JSON.stringify({ sid: userInfo.sid }),
});
chatParticipant
.then((res) => res.json())
.then((data) => {
console.log(`data from chat participant: `, data);
// ! get a client token to initialize the chat participant session
fetch(`/api/create_access_token`, {
method: `POST`,
body: JSON.stringify({
serviceSid: userInfo.chat_service_sid,
identity: data.identity,
}),
})
.then((res) => res.json())
.then((data) => {
const { token } = data;
setCreateOrGetConversation((oldData) => {
return { ...oldData, token: token };
});
})
.catch((err) =>
console.log(`err fetching access token: `, err)
);
});
});
});
}
})
.catch((err) => console.log(`err creating conversation: `, err));
}
}
}, [userInfo]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment