Skip to content

Commit

Permalink
Step 7.3: Replace fetch() calls with Apollo useQuery()
Browse files Browse the repository at this point in the history
  • Loading branch information
Urigo committed May 20, 2020
1 parent aa22ee7 commit c911119
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 42 deletions.
44 changes: 20 additions & 24 deletions src/components/ChatRoomScreen/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import gql from 'graphql-tag';
import React from 'react';
import { useCallback, useMemo, useState } from 'react';
import { useCallback } from 'react';
import { useApolloClient, useQuery } from '@apollo/react-hooks';
import styled from 'styled-components';
import ChatNavbar from './ChatNavbar';
import MessageInput from './MessageInput';
Expand All @@ -13,7 +15,7 @@ const Container = styled.div`
height: 100vh;
`;

const getChatQuery = `
const getChatQuery = gql`
query GetChat($chatId: ID!) {
chat(chatId: $chatId) {
id
Expand Down Expand Up @@ -52,24 +54,11 @@ const ChatRoomScreen: React.FC<ChatRoomScreenParams> = ({
history,
chatId,
}) => {
const [chat, setChat] = useState<OptionalChatQueryResult>(null);

useMemo(async () => {
const body = await fetch(`${process.env.REACT_APP_SERVER_URL}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: getChatQuery,
variables: { chatId },
}),
});
const {
data: { chat },
} = await body.json();
setChat(chat);
}, [chatId]);
const client = useApolloClient();
const { data } = useQuery<any>(getChatQuery, {
variables: { chatId },
});
const chat = data?.chat;

const onSendMessage = useCallback(
(content: string) => {
Expand All @@ -79,14 +68,21 @@ const ChatRoomScreen: React.FC<ChatRoomScreenParams> = ({
id: (chat.messages.length + 10).toString(),
createdAt: new Date(),
content,
__typename: 'Chat',
};

setChat({
...chat,
messages: chat.messages.concat(message),
client.writeQuery({
query: getChatQuery,
variables: { chatId },
data: {
chat: {
...chat,
messages: chat.messages.concat(message),
},
},
});
},
[chat]
[chat, chatId, client]
);

if (!chat) return null;
Expand Down
29 changes: 11 additions & 18 deletions src/components/ChatsListScreen/ChatsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import React from 'react';
import moment from 'moment';
import { List, ListItem } from '@material-ui/core';
import styled from 'styled-components';
import { useCallback, useState, useMemo } from 'react';
import { useCallback } from 'react';
import { History } from 'history';
import gql from 'graphql-tag';
import { useQuery } from '@apollo/react-hooks';

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

const getChatsQuery = `
const getChatsQuery = gql`
query GetChats {
chats {
id
Expand All @@ -77,21 +79,7 @@ interface ChatsListProps {
}

const ChatsList: React.FC<ChatsListProps> = ({ history }) => {
const [chats, setChats] = useState<any[]>([]);

useMemo(async () => {
const body = await fetch(`${process.env.REACT_APP_SERVER_URL}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: getChatsQuery }),
});
const {
data: { chats },
} = await body.json();
setChats(chats);
}, []);
const { data } = useQuery<any>(getChatsQuery);

const navToChat = useCallback(
(chat) => {
Expand All @@ -100,10 +88,15 @@ const ChatsList: React.FC<ChatsListProps> = ({ history }) => {
[history]
);

if (data === undefined || data.chats === undefined) {
return null;
}
let chats = data.chats;

return (
<Container>
<StyledList>
{chats.map((chat) => (
{chats.map((chat: any) => (
<StyledListItem
key={chat.id}
data-testid="chat"
Expand Down

0 comments on commit c911119

Please sign in to comment.