feat: Comment resolving (#7115)
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import invariant from "invariant";
|
||||
import orderBy from "lodash/orderBy";
|
||||
import { action, computed } from "mobx";
|
||||
import Comment from "~/models/Comment";
|
||||
import { client } from "~/utils/ApiClient";
|
||||
import RootStore from "./RootStore";
|
||||
import Store from "./base/Store";
|
||||
|
||||
@@ -29,12 +31,54 @@ export default class CommentsStore extends Store<Comment> {
|
||||
threadsInDocument(documentId: string): Comment[] {
|
||||
return this.filter(
|
||||
(comment: Comment) =>
|
||||
comment.documentId === documentId && !comment.parentCommentId
|
||||
comment.documentId === documentId &&
|
||||
!comment.parentCommentId &&
|
||||
(!comment.isNew ||
|
||||
comment.createdById === this.rootStore.auth.currentUserId)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of comments that are replies to the given comment.
|
||||
* Returns a list of resolved comments in a document that are not replies to other
|
||||
* comments.
|
||||
*
|
||||
* @param documentId ID of the document to get comments for
|
||||
* @returns Array of comments
|
||||
*/
|
||||
resolvedThreadsInDocument(documentId: string): Comment[] {
|
||||
return this.threadsInDocument(documentId).filter(
|
||||
(comment: Comment) => comment.isResolved === true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of comments in a document that are not replies to other
|
||||
* comments.
|
||||
*
|
||||
* @param documentId ID of the document to get comments for
|
||||
* @returns Array of comments
|
||||
*/
|
||||
unresolvedThreadsInDocument(documentId: string): Comment[] {
|
||||
return this.threadsInDocument(documentId).filter(
|
||||
(comment: Comment) => comment.isResolved === false
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the total number of unresolbed comments in the given document.
|
||||
*
|
||||
* @param documentId ID of the document to get comments for
|
||||
* @returns A number of comments
|
||||
*/
|
||||
unresolvedCommentsInDocumentCount(documentId: string): number {
|
||||
return this.unresolvedThreadsInDocument(documentId).reduce(
|
||||
(memo, thread) => memo + this.inThread(thread.id).length,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of comments that includes the given thread ID and any of it's replies.
|
||||
*
|
||||
* @param commentId ID of the comment to get replies for
|
||||
* @returns Array of comments
|
||||
@@ -46,6 +90,40 @@ export default class CommentsStore extends Store<Comment> {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a comment thread with the given ID.
|
||||
*
|
||||
* @param id ID of the comment to resolve
|
||||
* @returns Resolved comment
|
||||
*/
|
||||
@action
|
||||
resolve = async (id: string): Promise<Comment> => {
|
||||
const res = await client.post("/comments.resolve", {
|
||||
id,
|
||||
});
|
||||
invariant(res?.data, "Comment not available");
|
||||
this.addPolicies(res.policies);
|
||||
this.add(res.data);
|
||||
return this.data.get(res.data.id) as Comment;
|
||||
};
|
||||
|
||||
/**
|
||||
* Unresolve a comment thread with the given ID.
|
||||
*
|
||||
* @param id ID of the comment to unresolve
|
||||
* @returns Unresolved comment
|
||||
*/
|
||||
@action
|
||||
unresolve = async (id: string): Promise<Comment> => {
|
||||
const res = await client.post("/comments.unresolve", {
|
||||
id,
|
||||
});
|
||||
invariant(res?.data, "Comment not available");
|
||||
this.addPolicies(res.policies);
|
||||
this.add(res.data);
|
||||
return this.data.get(res.data.id) as Comment;
|
||||
};
|
||||
|
||||
@action
|
||||
setTyping({
|
||||
commentId,
|
||||
|
||||
Reference in New Issue
Block a user