Skip to content

Commit 85227b6

Browse files
authored
add mutations and actions for replies (codesandbox#3633)
1 parent 8e455c0 commit 85227b6

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

packages/app/src/app/overmind/effects/fakeGql/comments/mutations.ts

+44
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import {
77
DeleteCommentResponse,
88
UpdateCommentVariables,
99
UpdateCommentResponse,
10+
DeleteReplyVariables,
11+
DeleteReplyResponse,
12+
UpdateReplyResponse,
13+
UpdateReplyVariables,
1014
} from './types';
1115

1216
export const addComment: Query<AddCommentResponse, AddCommentVariables> = gql`
@@ -104,3 +108,43 @@ export const reply: Query<any, any> = gql`
104108
}
105109
}
106110
`;
111+
112+
export const deleteReply: Query<
113+
DeleteReplyResponse,
114+
DeleteReplyVariables
115+
> = gql`
116+
mutation deleteReply($replyId: String!, $commentId: String!) {
117+
deleteReply(replyId: $replyId, commentId: $commentId) {
118+
id
119+
replies {
120+
id
121+
content
122+
author {
123+
id
124+
avatarUrl
125+
username
126+
}
127+
}
128+
}
129+
}
130+
`;
131+
132+
export const updateReply: Query<
133+
UpdateReplyResponse,
134+
UpdateReplyVariables
135+
> = gql`
136+
mutation updateReply($replyId: String!, $commentId: String!) {
137+
updateReply(replyId: $replyId, commentId: $commentId, comment: $comment) {
138+
id
139+
replies {
140+
id
141+
content
142+
author {
143+
id
144+
avatarUrl
145+
username
146+
}
147+
}
148+
}
149+
}
150+
`;

packages/app/src/app/overmind/effects/fakeGql/comments/types.ts

+22
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,25 @@ export type CommentResponse = {
104104
metadata: string;
105105
};
106106
};
107+
108+
export type DeleteReplyVariables = { replyId: string; commentId: string };
109+
110+
export type DeleteReplyResponse = {
111+
deleteReply: {
112+
id: string;
113+
replies: Message[];
114+
};
115+
};
116+
117+
export type UpdateReplyVariables = {
118+
replyId: string;
119+
commentId: string;
120+
comment: string;
121+
};
122+
123+
export type UpdateReplyResponse = {
124+
updateReply: {
125+
id: string;
126+
replies: Message[];
127+
};
128+
};

packages/app/src/app/overmind/namespaces/editor/actions.ts

+67
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,73 @@ export const updateComment: AsyncAction<{
15111511
}
15121512
};
15131513

1514+
export const deleteReply: AsyncAction<{
1515+
replyId: string;
1516+
commentId: string;
1517+
}> = async ({ state, effects }, { replyId, commentId }) => {
1518+
if (!state.editor.currentSandbox) {
1519+
return;
1520+
}
1521+
const sandboxId = state.editor.currentSandbox.id;
1522+
const oldReplies = state.editor.comments[sandboxId][commentId];
1523+
1524+
state.editor.comments[sandboxId][commentId] = {
1525+
...oldReplies,
1526+
replies: oldReplies.replies.filter(reply => reply.id !== replyId),
1527+
};
1528+
1529+
try {
1530+
await effects.fakeGql.mutations.deleteReply({ replyId, commentId });
1531+
} catch (error) {
1532+
effects.notificationToast.error(
1533+
'Unable to delete your reply, please try again'
1534+
);
1535+
state.editor.comments[sandboxId][commentId] = oldReplies;
1536+
}
1537+
};
1538+
1539+
export const updateReply: AsyncAction<{
1540+
replyId: string;
1541+
commentId: string;
1542+
comment: string;
1543+
}> = async (
1544+
{ state, effects },
1545+
{ replyId, commentId, comment: newComment }
1546+
) => {
1547+
if (!state.editor.currentSandbox) {
1548+
return;
1549+
}
1550+
const sandboxId = state.editor.currentSandbox.id;
1551+
const old = state.editor.comments[sandboxId][commentId];
1552+
1553+
state.editor.comments[sandboxId][commentId] = {
1554+
...old,
1555+
replies: old.replies.map(reply => {
1556+
if (reply.id === replyId) {
1557+
return {
1558+
...reply,
1559+
content: newComment,
1560+
};
1561+
}
1562+
1563+
return reply;
1564+
}),
1565+
};
1566+
1567+
try {
1568+
await effects.fakeGql.mutations.updateReply({
1569+
replyId,
1570+
commentId,
1571+
comment: newComment,
1572+
});
1573+
} catch (error) {
1574+
effects.notificationToast.error(
1575+
'Unable to update your comment, please try again'
1576+
);
1577+
state.editor.comments[sandboxId][commentId] = old;
1578+
}
1579+
};
1580+
15141581
export const selectCommentsFilter: Action<CommentsFilterOption> = (
15151582
{ state },
15161583
option

0 commit comments

Comments
 (0)