Skip to content

Commit

Permalink
the id of the chats and messages is generated by the client, optimis…
Browse files Browse the repository at this point in the history
…tic response removechat
  • Loading branch information
lorenzo.digiacomo committed Aug 9, 2019
1 parent bdcf9d0 commit b6127de
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@types/react-dom": "16.8.5",
"@types/react-router-dom": "4.3.4",
"@types/styled-components": "4.1.18",
"@types/uuid": "3.4.5",
"apollo-cache-inmemory": "1.6.2",
"apollo-client": "2.6.3",
"apollo-link": "1.2.12",
Expand All @@ -43,7 +44,7 @@
"styled-components": "4.3.2",
"subscriptions-transport-ws": "0.9.16",
"typescript": "3.5.3",
"@wora/apollo-offline": "0.1.1"
"@wora/apollo-offline": "0.1.2"
},
"scripts": {
"prebuild": "yarn codegen",
Expand Down
17 changes: 10 additions & 7 deletions src/components/ChatCreationScreen/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ChatCreationNavbar from './ChatCreationNavbar';
import { History } from 'history';
import { useAddChatMutation } from '../../graphql/types';
import { writeChat } from '../../services/cache.service';
import { v4 as uuid } from "uuid";

// eslint-disable-next-line
const Container = styled.div`
Expand All @@ -21,8 +22,8 @@ const StyledUsersList = styled(UsersList)`
`;

gql`
mutation AddChat($recipientId: ID!) {
addChat(recipientId: $recipientId) {
mutation AddChat($id: ID!, $recipientId: ID!) {
addChat(id: $id, recipientId: $recipientId) {
...Chat
}
}
Expand All @@ -37,20 +38,21 @@ const ChatCreationScreen: React.FC<ChildComponentProps> = ({ history }) => {
const [addChat] = useAddChatMutation();

const onUserPick = useCallback(
user => addChat({
user => {
const id: string = uuid();
addChat({
optimisticResponse: {
__typename: 'Mutation',
addChat: {
__typename: 'Chat',
id: Math.random()
.toString(36)
.substr(2, 9),
id,
name: user.name,
picture: user.picture,
lastMessage: null,
},
},
variables: {
id,
recipientId: user.id,
},
update: (client, { data: { addChat } }) => {
Expand All @@ -60,7 +62,8 @@ const ChatCreationScreen: React.FC<ChildComponentProps> = ({ history }) => {
if (result && result.data !== null) {
history.push(`/chats/${result.data!.addChat!.id}`);
}
}),
})
},
[addChat, history]
);

Expand Down
4 changes: 4 additions & 0 deletions src/components/ChatRoomScreen/ChatNavbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const ChatNavbar: React.FC<ChatNavbarProps> = ({ chat, history }) => {
variables: {
chatId: chat.id,
},
optimisticResponse: {
__typename: 'Mutation',
removeChat: chat.id,
},
update: (client, { data: { removeChat } }) => {
eraseChat(client, removeChat);
},
Expand Down
13 changes: 7 additions & 6 deletions src/components/ChatRoomScreen/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
} from '../../graphql/types';
import * as fragments from '../../graphql/fragments';
import { writeMessage } from '../../services/cache.service';
import { v4 as uuid } from "uuid";


const Container = styled.div`
background: url(/assets/chat-background.jpg);
Expand All @@ -37,8 +39,8 @@ const getChatQuery = gql`

// eslint-disable-next-line
const addMessageMutation = gql`
mutation AddMessage($chatId: ID!, $content: String!) {
addMessage(chatId: $chatId, content: $content) {
mutation AddMessage($id: ID!, $chatId: ID!, $content: String!) {
addMessage(id: $id, chatId: $chatId, content: $content) {
...Message
}
}
Expand Down Expand Up @@ -122,17 +124,16 @@ const [addMessage] = useAddMessageMutation();
return null;
}
const chat = data.chat;
const id:string = uuid();
if (chat === null) return null;

addMessage({
variables: { chatId, content },
variables: { id, chatId, content },
optimisticResponse: {
__typename: 'Mutation',
addMessage: {
__typename: 'Message',
id: Math.random()
.toString(36)
.substr(2, 9),
id,
createdAt: new Date(),
isMine: true,
chat: {
Expand Down
15 changes: 11 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,13 @@
"@types/react-native" "*"
csstype "^2.2.0"

"@types/[email protected]":
version "3.4.5"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-3.4.5.tgz#d4dc10785b497a1474eae0ba7f0cb09c0ddfd6eb"
integrity sha512-MNL15wC3EKyw1VLF+RoVO4hJJdk9t/Hlv3rt1OL65Qvuadm4BYo6g9ZJQqoq7X8NBFSsQXgAujWciovh2lpVjA==
dependencies:
"@types/node" "*"

"@types/[email protected]":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@types/valid-url/-/valid-url-1.0.2.tgz#60fa435ce24bfd5ba107b8d2a80796aeaf3a8f45"
Expand Down Expand Up @@ -1954,10 +1961,10 @@
dependencies:
"@wora/cache-persist" "^1.0.2"

"@wora/[email protected].1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@wora/apollo-offline/-/apollo-offline-0.1.1.tgz#b83335b705317857b23b5ac1cefd6e2925c86222"
integrity sha512-rwkodZME7MLUXhDSbfDsyQAYSssKUhC07a7tZ05FB88Xq0XVVJ3untuFM2DwgxTsf7eDKwQaAM1XNOw+919eDw==
"@wora/[email protected].2":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@wora/apollo-offline/-/apollo-offline-0.1.2.tgz#d80a23d3e8985e4db4b69e682a95ddd69d63fb7b"
integrity sha512-3Ydc2uPNodtiZA7Rzhu1fPKQu3UQgAz05irjnF6eT/tp0CxZBytm/kwfyP7tSeAz+u/Dw1+LiBnTBSOzle2pOg==
dependencies:
"@wora/apollo-cache" "^0.0.3"
"@wora/cache-persist" "^1.1.0"
Expand Down

0 comments on commit b6127de

Please sign in to comment.