2022-11-19 14:21:26 +00:00
|
|
|
import React from "react";
|
|
|
|
// swr
|
|
|
|
import { mutate } from "swr";
|
|
|
|
// react hook form
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
// services
|
2022-12-12 04:49:52 +00:00
|
|
|
import issuesServices from "lib/services/issues.service";
|
2022-11-19 14:21:26 +00:00
|
|
|
// fetch keys
|
|
|
|
import { PROJECT_ISSUES_COMMENTS } from "constants/fetch-keys";
|
|
|
|
// components
|
2022-11-24 13:48:18 +00:00
|
|
|
import CommentCard from "components/project/issues/issue-detail/comment/IssueCommentCard";
|
2022-11-19 14:21:26 +00:00
|
|
|
// ui
|
|
|
|
import { TextArea, Button, Spinner } from "ui";
|
|
|
|
// types
|
|
|
|
import type { IIssueComment } from "types";
|
2022-11-24 13:48:18 +00:00
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
type Props = {
|
|
|
|
comments?: IIssueComment[];
|
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
issueId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: Partial<IIssueComment> = {
|
|
|
|
comment: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
const IssueCommentSection: React.FC<Props> = ({ comments, issueId, projectId, workspaceSlug }) => {
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
setValue,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
reset,
|
|
|
|
} = useForm<IIssueComment>({ defaultValues });
|
|
|
|
|
|
|
|
const onSubmit = async (formData: IIssueComment) => {
|
|
|
|
await issuesServices
|
|
|
|
.createIssueComment(workspaceSlug, projectId, issueId, formData)
|
|
|
|
.then((response) => {
|
|
|
|
console.log(response);
|
2022-12-13 08:48:49 +00:00
|
|
|
mutate<IIssueComment[]>(PROJECT_ISSUES_COMMENTS(issueId), (prevData) => [
|
|
|
|
response,
|
|
|
|
...(prevData ?? []),
|
|
|
|
]);
|
2022-11-19 14:21:26 +00:00
|
|
|
reset(defaultValues);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onCommentUpdate = async (comment: IIssueComment) => {
|
|
|
|
await issuesServices
|
|
|
|
.patchIssueComment(workspaceSlug, projectId, issueId, comment.id, comment)
|
|
|
|
.then((response) => {
|
|
|
|
console.log(response);
|
2022-12-13 08:48:49 +00:00
|
|
|
mutate<IIssueComment[]>(PROJECT_ISSUES_COMMENTS(issueId), (prevData) => {
|
|
|
|
const newData = prevData ?? [];
|
|
|
|
const index = newData.findIndex((comment) => comment.id === response.id);
|
|
|
|
newData[index] = response;
|
|
|
|
return [...newData];
|
|
|
|
});
|
2022-11-19 14:21:26 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onCommentDelete = async (commentId: string) => {
|
|
|
|
await issuesServices
|
|
|
|
.deleteIssueComment(workspaceSlug, projectId, issueId, commentId)
|
|
|
|
.then((response) => {
|
2022-12-13 08:48:49 +00:00
|
|
|
mutate<IIssueComment[]>(PROJECT_ISSUES_COMMENTS(issueId), (prevData) =>
|
|
|
|
(prevData ?? []).filter((c) => c.id !== commentId)
|
|
|
|
);
|
2022-11-19 14:21:26 +00:00
|
|
|
console.log(response);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-11-24 13:48:18 +00:00
|
|
|
<div className="space-y-5">
|
2022-11-19 14:21:26 +00:00
|
|
|
{comments ? (
|
|
|
|
comments.length > 0 ? (
|
|
|
|
<div className="space-y-5">
|
|
|
|
{comments.map((comment) => (
|
|
|
|
<CommentCard
|
|
|
|
key={comment.id}
|
|
|
|
comment={comment}
|
|
|
|
onSubmit={onCommentUpdate}
|
|
|
|
handleCommentDeletion={onCommentDelete}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<p className="text-sm">No comments yet. Be the first to comment.</p>
|
|
|
|
)
|
|
|
|
) : (
|
|
|
|
<div className="w-full flex justify-center">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
)}
|
2022-12-13 15:52:44 +00:00
|
|
|
<form onSubmit={handleSubmit(onSubmit)}>
|
|
|
|
<div className="flex items-start gap-2 border rounded-md p-2">
|
|
|
|
<TextArea
|
|
|
|
id="comment"
|
|
|
|
name="comment"
|
|
|
|
register={register}
|
|
|
|
validations={{
|
|
|
|
required: true,
|
|
|
|
}}
|
|
|
|
mode="transparent"
|
|
|
|
error={errors.comment}
|
|
|
|
placeholder="Enter your comment"
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
if (e.key === "Enter" && e.shiftKey) {
|
|
|
|
e.preventDefault();
|
|
|
|
const value = e.currentTarget.value;
|
|
|
|
const start = e.currentTarget.selectionStart;
|
|
|
|
const end = e.currentTarget.selectionEnd;
|
|
|
|
setValue("comment", `${value.substring(0, start)}\r ${value.substring(end)}`);
|
|
|
|
} else if (e.key === "Enter" && !e.shiftKey) {
|
|
|
|
e.preventDefault();
|
|
|
|
isSubmitting || handleSubmit(onSubmit)();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Button type="submit" className="whitespace-nowrap" disabled={isSubmitting}>
|
|
|
|
{isSubmitting ? "Adding comment..." : "Add comment"}
|
|
|
|
{/* <UploadingIcon /> */}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</form>
|
2022-11-19 14:21:26 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default IssueCommentSection;
|