-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 8.2: Rewrite lastMessage to chats query
- Loading branch information
Showing
5 changed files
with
58 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ChatNavbar from './ChatNavbar'; | |
import MessageInput from './MessageInput'; | ||
import MessagesList from './MessagesList'; | ||
import { History } from 'history'; | ||
import * as queries from '../../graphql/queries'; | ||
|
||
const Container = styled.div` | ||
background: url(/assets/chat-background.jpg); | ||
|
@@ -60,6 +61,10 @@ export interface ChatQueryResult { | |
|
||
type OptionalChatQueryResult = ChatQueryResult | null; | ||
|
||
interface ChatsResult { | ||
chats: any[]; | ||
} | ||
|
||
const ChatRoomScreen: React.FC<ChatRoomScreenParams> = ({ | ||
history, | ||
chatId, | ||
|
@@ -96,6 +101,37 @@ const ChatRoomScreen: React.FC<ChatRoomScreenParams> = ({ | |
}, | ||
}); | ||
} | ||
|
||
let clientChatsData; | ||
try { | ||
clientChatsData = client.readQuery<ChatsResult>({ | ||
query: queries.chats, | ||
}); | ||
} catch (e) { | ||
return; | ||
} | ||
|
||
if (!clientChatsData || clientChatsData === null) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return null; | ||
} | ||
if (!clientChatsData.chats || clientChatsData.chats === undefined) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lgandecki
|
||
return null; | ||
} | ||
const chats = clientChatsData.chats; | ||
|
||
const chatIndex = chats.findIndex((currentChat: any) => currentChat.id === chatId); | ||
if (chatIndex === -1) return; | ||
const chatWhereAdded = chats[chatIndex]; | ||
|
||
chatWhereAdded.lastMessage = data.addMessage; | ||
// The chat will appear at the top of the ChatsList component | ||
chats.splice(chatIndex, 1); | ||
chats.unshift(chatWhereAdded); | ||
|
||
client.writeQuery({ | ||
query: queries.chats, | ||
data: { chats: chats }, | ||
}); | ||
}, | ||
}); | ||
}, | ||
|
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,16 @@ | ||
import gql from 'graphql-tag'; | ||
|
||
export default gql` | ||
query Chats { | ||
chats { | ||
id | ||
name | ||
picture | ||
lastMessage { | ||
id | ||
content | ||
createdAt | ||
} | ||
} | ||
} | ||
`; |
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 @@ | ||
export { default as chats } from './chats.query'; |
This === null check is redundant