-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #636 from glific/conversation-restructuring
Conversation restructuring
- Loading branch information
Showing
6 changed files
with
76 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// This service includes all the actions related to conversations storing | ||
|
||
import { SEARCH_QUERY } from '../graphql/queries/Search'; | ||
|
||
// write conversation to cache | ||
export const updateConversations = (conversation: any, client: any, queryVariables: any) => { | ||
// get the current conversations from the cache | ||
const conversations = getCachedConverations(client, queryVariables); | ||
|
||
// make a copy of current conversation | ||
const conversationCopy = JSON.parse(JSON.stringify(conversation)); | ||
|
||
// make a copy of current conversations | ||
const conversationsCopy = JSON.parse(JSON.stringify(conversations)); | ||
|
||
// add new conversation to conversations | ||
conversationsCopy.search = [...conversationsCopy.search, ...conversationCopy.search]; | ||
|
||
// update conversations | ||
updateConversationsCache(conversationsCopy, client, queryVariables); | ||
}; | ||
|
||
export const saveConversation = (conversation: any, client: any, queryVariables: any) => { | ||
// parse the conversation | ||
const conversationCopy = JSON.parse(JSON.stringify(conversation)); | ||
|
||
// TODOS: need to check why we need this. | ||
conversationCopy.search[0].messages | ||
.sort((currentMessage: any, nextMessage: any) => { | ||
return currentMessage.id - nextMessage.id; | ||
}) | ||
.reverse(); | ||
|
||
updateConversations(conversation, client, queryVariables); | ||
}; | ||
|
||
// read the conversation from cache | ||
export const getCachedConverations = (client: any, queryVariables: any) => { | ||
// fetch conversations from the cache | ||
const conversations = client.readQuery({ | ||
query: SEARCH_QUERY, | ||
variables: queryVariables, | ||
}); | ||
|
||
return conversations; | ||
}; | ||
|
||
// update the conversations cache | ||
export const updateConversationsCache = (conversations: any, client: any, queryVariables: any) => { | ||
// write the updated conversations to cached | ||
client.writeQuery({ | ||
query: SEARCH_QUERY, | ||
variables: queryVariables, | ||
data: conversations, | ||
}); | ||
}; |