Skip to content

Commit

Permalink
Step 8.2: Rewrite lastMessage to chats query
Browse files Browse the repository at this point in the history
  • Loading branch information
Urigo committed May 20, 2020
1 parent 205ea79 commit bd5da04
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
36 changes: 36 additions & 0 deletions src/components/ChatRoomScreen/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -60,6 +61,10 @@ export interface ChatQueryResult {

type OptionalChatQueryResult = ChatQueryResult | null;

interface ChatsResult {
chats: any[];
}

const ChatRoomScreen: React.FC<ChatRoomScreenParams> = ({
history,
chatId,
Expand Down Expand Up @@ -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.

Copy link
@lgandecki

lgandecki Jul 10, 2020

This === null check is redundant

return null;
}
if (!clientChatsData.chats || clientChatsData.chats === undefined) {

This comment has been minimized.

Copy link
@lgandecki

lgandecki Jul 10, 2020

=== undefined check is redundant

This comment has been minimized.

Copy link
@lgandecki

lgandecki Jul 10, 2020

actually, both of those checks could probably be replaced with just

            if (!clientChatsData?.chats) {
              return null;
            }
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 },
});
},
});
},
Expand Down
5 changes: 3 additions & 2 deletions src/components/ChatsListScreen/ChatsList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { createBrowserHistory } from 'history';
import { mockApolloClient } from '../../test-helpers';
import ChatsList, { getChatsQuery } from './ChatsList';
import * as queries from '../../graphql/queries';

describe('ChatsList', () => {
afterEach(() => {
Expand All @@ -29,7 +30,7 @@ describe('ChatsList', () => {
it('renders fetched chats data', async () => {
const client = mockApolloClient([
{
request: { query: getChatsQuery },
request: { query: queries.chats },
result: {
data: {
chats: [
Expand Down Expand Up @@ -75,7 +76,7 @@ describe('ChatsList', () => {
it('should navigate to the target chat room on chat item click', async () => {
const client = mockApolloClient([
{
request: { query: getChatsQuery },
request: { query: queries.chats },
result: {
data: {
chats: [
Expand Down
19 changes: 2 additions & 17 deletions src/components/ChatsListScreen/ChatsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { List, ListItem } from '@material-ui/core';
import styled from 'styled-components';
import { useCallback } from 'react';
import { History } from 'history';
import gql from 'graphql-tag';
import { useQuery } from '@apollo/react-hooks';
import * as queries from '../../graphql/queries';

const Container = styled.div`
height: calc(100% - 56px);
Expand Down Expand Up @@ -59,27 +59,12 @@ const MessageDate = styled.div`
font-size: 13px;
`;

export const getChatsQuery = gql`
query GetChats {
chats {
id
name
picture
lastMessage {
id
content
createdAt
}
}
}
`;

interface ChatsListProps {
history: History;
}

const ChatsList: React.FC<ChatsListProps> = ({ history }) => {
const { data } = useQuery<any>(getChatsQuery);
const { data } = useQuery<any>(queries.chats);

const navToChat = useCallback(
(chat) => {
Expand Down
16 changes: 16 additions & 0 deletions src/graphql/queries/chats.query.ts
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
}
}
}
`;
1 change: 1 addition & 0 deletions src/graphql/queries/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as chats } from './chats.query';

0 comments on commit bd5da04

Please sign in to comment.