Skip to content
This repository has been archived by the owner on Sep 16, 2020. It is now read-only.

Fix duplicate events and comments #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const client = new AWSAppSyncClient({
auth: {
type: appSyncConfig.authenticationType,
apiKey: appSyncConfig.apiKey,
}
},
disableOffline: true
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

const WithProvider = () => (
Expand Down
5 changes: 4 additions & 1 deletion src/Components/AllEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export default compose(
const query = QueryAllEvents;
const data = proxy.readQuery({ query });

data.listEvents.items = data.listEvents.items.filter(event => event.id !== deleteEvent.id);
const index = data.listEvents.items.findIndex(event => event.id === deleteEvent.id)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (index > -1) {
data.listEvents.items.splice(index, 1)
}

proxy.writeQuery({ query, data });
}
Expand Down
14 changes: 7 additions & 7 deletions src/Components/EventComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ const EventCommentsWithData = graphql(
eventId: props.ownProps.eventId,
},
updateQuery: (prev, { subscriptionData: { data: { subscribeToEventComments } } }) => {
//skip the update if the comment exists.
//This can happen when the `createComment` Mutation updates the query
if (prev.getEvent.comments.items.some(c => c.commentId === subscribeToEventComments.commentId)) {
return prev;
}

const res = {
...prev,
...{
Expand All @@ -75,13 +81,7 @@ const EventCommentsWithData = graphql(
comments: {
__typename: 'CommentConnections',
items: [
...prev.getEvent.comments.items.filter(c => {
return (
c.content !== subscribeToEventComments.content &&
c.createdAt !== subscribeToEventComments.createdAt &&
c.commentId !== subscribeToEventComments.commentId
);
}),
...prev.getEvent.comments.items,
subscribeToEventComments,
]
}
Expand Down
14 changes: 4 additions & 10 deletions src/Components/NewComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,10 @@ const NewCommentWithData = graphql(
const variables = { id: props.eventId };
const data = proxy.readQuery({ query, variables });

data.getEvent = {
...data.getEvent,
comments: {
...data.getEvent.comments,
items: [
...data.getEvent.comments.items.filter(c => c.commentId !== commentOnEvent.commentId),
commentOnEvent,
]
}
};
//don't add an existing comment (can happen when the comments subscription updates the query)
if (!data.getEvent.comments.items.some(c => c.commentId === commentOnEvent.commentId)) {
data.getEvent.comments.items.push(commentOnEvent);
}

proxy.writeQuery({ query, data });
},
Expand Down
2 changes: 1 addition & 1 deletion src/Components/NewEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default graphql(
const query = QueryAllEvents;
const data = proxy.readQuery({ query });

data.listEvents.items = [...data.listEvents.items.filter(e => e.id !== createEvent.id), createEvent];
data.listEvents.items.push(createEvent);

proxy.writeQuery({ query, data });
}
Expand Down