Skip to content

Commit

Permalink
Merge pull request #943 from glific/bug/search-fix
Browse files Browse the repository at this point in the history
Search fix
  • Loading branch information
DigneshGujarathi authored Jan 21, 2021
2 parents 1b26fdb + c51d2d8 commit 60cb0d5
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions src/containers/Chat/ChatMessages/ChatMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,39 +83,39 @@ export const ChatMessages: React.SFC<ChatMessagesProps> = ({ contactId, simulato
const [getSearchQuery, { called, data, loading, error }] = useLazyQuery<any>(SEARCH_QUERY, {
onCompleted: (searchData) => {
if (searchData) {
// get the conversations from cache
const conversations = getCachedConverations(client, queryVariables);

const conversationCopy = JSON.parse(JSON.stringify(searchData));
conversationCopy.search[0].messages
.sort((currentMessage: any, nextMessage: any) => {
return currentMessage.id - nextMessage.id;
})
.reverse();
const conversationsCopy = JSON.parse(JSON.stringify(conversations));
let isContactCached = false;
conversationsCopy.search = conversationsCopy.search.map((conversation: any) => {
const conversationObj = conversation;
// If the contact is present in the cache
if (conversationObj.contact.id === contactId.toString()) {
isContactCached = true;
conversationObj.messages = [
...conversationObj.messages,
...conversationCopy.search[0].messages,
];
}
return conversationObj;
});
// If the contact is NOT present in the cache
if (!isContactCached) {
conversationsCopy.search = [...conversationsCopy.search, searchData.search[0]];
}
// update the conversation cache
updateConversationsCache(conversationsCopy, client, queryVariables);

if (searchData.search[0].messages.length === 0) {
setShowLoadMore(false);
} else {
// get the conversations from cache
const conversations = getCachedConverations(client, queryVariables);

const conversationCopy = JSON.parse(JSON.stringify(searchData));
conversationCopy.search[0].messages
.sort((currentMessage: any, nextMessage: any) => {
return currentMessage.id - nextMessage.id;
})
.reverse();
const conversationsCopy = JSON.parse(JSON.stringify(conversations));
let isContactCached = false;
conversationsCopy.search = conversationsCopy.search.map((conversation: any) => {
const conversationObj = conversation;
// If the contact is present in the cache
if (conversationObj.contact.id === contactId.toString()) {
isContactCached = true;
conversationObj.messages = [
...conversationObj.messages,
...conversationCopy.search[0].messages,
];
}
return conversationObj;
});
// If the contact is NOT present in the cache
if (!isContactCached) {
conversationsCopy.search = [...conversationsCopy.search, searchData.search[0]];
}
// update the conversation cache
updateConversationsCache(conversationsCopy, client, queryVariables);

setMessageOffset(messageOffset + 50);
}
}
Expand Down Expand Up @@ -314,7 +314,7 @@ export const ChatMessages: React.SFC<ChatMessagesProps> = ({ contactId, simulato
getSearchQuery({
variables: {
filter: { id: contactId.toString() },
messageOpts: { limit: 50, offset: messageOffset },
messageOpts: { limit: 200, offset: messageOffset },
contactOpts: { limit: 1 },
},
});
Expand Down

0 comments on commit 60cb0d5

Please sign in to comment.