Skip to content

Commit

Permalink
feat: access selector for comment (#2012)
Browse files Browse the repository at this point in the history
* dev: access specifier for comment

* chore: change access order
  • Loading branch information
aaryan610 committed Aug 29, 2023
1 parent fd0efb0 commit d8bbdc1
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 44 deletions.
110 changes: 76 additions & 34 deletions apps/app/components/issues/comment/add-comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,55 @@ import { useRouter } from "next/router";
// react-hook-form
import { useForm, Controller } from "react-hook-form";
// components
import { SecondaryButton } from "components/ui";
import { TipTapEditor } from "components/tiptap";
// ui
import { Icon, SecondaryButton, Tooltip } from "components/ui";
// types
import type { IIssueComment } from "types";

const defaultValues: Partial<IIssueComment> = {
comment_json: "",
access: "INTERNAL",
comment_html: "",
};

type Props = {
disabled?: boolean;
onSubmit: (data: IIssueComment) => Promise<void>;
showAccessSpecifier?: boolean;
};

export const AddComment: React.FC<Props> = ({ disabled = false, onSubmit }) => {
const commentAccess = [
{
icon: "lock",
key: "INTERNAL",
label: "Private",
},
{
icon: "public",
key: "EXTERNAL",
label: "Public",
},
];

export const AddComment: React.FC<Props> = ({
disabled = false,
onSubmit,
showAccessSpecifier = false,
}) => {
const editorRef = React.useRef<any>(null);

const router = useRouter();
const { workspaceSlug } = router.query;

const {
control,
formState: { isSubmitting },
handleSubmit,
reset,
setValue,
watch,
} = useForm<IIssueComment>({ defaultValues });

const editorRef = React.useRef<any>(null);

const router = useRouter();
const { workspaceSlug } = router.query;

const handleAddComment = async (formData: IIssueComment) => {
if (!formData.comment_html || !formData.comment_json || isSubmitting) return;
if (!formData.comment_html || isSubmitting) return;

await onSubmit(formData).then(() => {
reset(defaultValues);
Expand All @@ -45,30 +62,55 @@ export const AddComment: React.FC<Props> = ({ disabled = false, onSubmit }) => {
return (
<div>
<form onSubmit={handleSubmit(handleAddComment)}>
<div className="issue-comments-section">
<Controller
name="comment_html"
control={control}
render={({ field: { value, onChange } }) => (
<TipTapEditor
workspaceSlug={workspaceSlug as string}
ref={editorRef}
value={
!value ||
value === "" ||
(typeof value === "object" && Object.keys(value).length === 0)
? watch("comment_html")
: value
}
customClassName="p-3 min-h-[50px] shadow-sm"
debouncedUpdatesEnabled={false}
onChange={(comment_json: Object, comment_html: string) => {
onChange(comment_html);
setValue("comment_json", comment_json);
}}
/>
<div>
<div className="relative">
{showAccessSpecifier && (
<div className="absolute bottom-2 left-3 z-[1]">
<Controller
control={control}
name="access"
render={({ field: { onChange, value } }) => (
<div className="flex border border-custom-border-300 divide-x divide-custom-border-300 rounded overflow-hidden">
{commentAccess.map((access) => (
<Tooltip key={access.key} tooltipContent={access.label}>
<button
type="button"
onClick={() => onChange(access.key)}
className={`grid place-items-center p-1 hover:bg-custom-background-80 ${
value === access.key ? "bg-custom-background-80" : ""
}`}
>
<Icon
iconName={access.icon}
className={`w-4 h-4 -mt-1 ${
value === access.key
? "!text-custom-text-100"
: "!text-custom-text-400"
}`}
/>
</button>
</Tooltip>
))}
</div>
)}
/>
</div>
)}
/>
<Controller
name="comment_html"
control={control}
render={({ field: { value, onChange } }) => (
<TipTapEditor
workspaceSlug={workspaceSlug as string}
ref={editorRef}
value={!value || value === "" ? "<p></p>" : value}
customClassName="p-3 min-h-[100px] shadow-sm"
debouncedUpdatesEnabled={false}
onChange={(comment_json: Object, comment_html: string) => onChange(comment_html)}
/>
)}
/>
</div>

<SecondaryButton type="submit" disabled={isSubmitting || disabled} className="mt-2">
{isSubmitting ? "Adding..." : "Comment"}
Expand Down
9 changes: 8 additions & 1 deletion apps/app/components/issues/main-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import issuesService from "services/issues.service";
// hooks
import useUserAuth from "hooks/use-user-auth";
import useToast from "hooks/use-toast";
import useProjectDetails from "hooks/use-project-details";
// contexts
import { useProjectMyMembership } from "contexts/project-member.context";
// components
Expand Down Expand Up @@ -49,6 +50,8 @@ export const IssueMainContent: React.FC<Props> = ({
const { user } = useUserAuth();
const { memberRole } = useProjectMyMembership();

const { projectDetails } = useProjectDetails();

const { data: siblingIssues } = useSWR(
workspaceSlug && projectId && issueDetails?.parent ? SUB_ISSUES(issueDetails.parent) : null,
workspaceSlug && projectId && issueDetails?.parent
Expand Down Expand Up @@ -220,7 +223,11 @@ export const IssueMainContent: React.FC<Props> = ({
handleCommentUpdate={handleCommentUpdate}
handleCommentDelete={handleCommentDelete}
/>
<AddComment onSubmit={handleAddComment} disabled={uneditable} />
<AddComment
onSubmit={handleAddComment}
disabled={uneditable}
showAccessSpecifier={projectDetails && projectDetails.is_deployed}
/>
</div>
</>
);
Expand Down
14 changes: 6 additions & 8 deletions apps/app/services/issues.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class ProjectIssuesServices extends APIService {
workspaceSlug: string,
projectId: string,
issueId: string,
data: any,
data: Partial<IIssueComment>,
user: ICurrentUserResponse | undefined
): Promise<any> {
return this.post(
Expand Down Expand Up @@ -468,20 +468,18 @@ class ProjectIssuesServices extends APIService {
metadata: any;
title: string;
url: string;
},

}
): Promise<any> {
return this.patch(
`/api/workspaces/${workspaceSlug}/projects/${projectId}/issues/${issueId}/issue-links/${linkId}/`,
data
)
.then((response) => response?.data)
.catch((error) => {
throw error?.response;
});
.then((response) => response?.data)
.catch((error) => {
throw error?.response;
});
}


async deleteIssueLink(
workspaceSlug: string,
projectId: string,
Expand Down
1 change: 1 addition & 0 deletions apps/app/types/issues.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export interface IIssueActivity {
}

export interface IIssueComment extends IIssueActivity {
access: "EXTERNAL" | "INTERNAL";
comment_html: string;
comment_json: any;
comment_stripped: string;
Expand Down
2 changes: 1 addition & 1 deletion apps/app/types/projects.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface IProject {
} | null;
id: string;
identifier: string;
is_deployed: boolean;
is_favorite: boolean;
is_member: boolean;
member_role: 5 | 10 | 15 | 20 | null;
Expand All @@ -57,7 +58,6 @@ export interface IProject {
updated_by: string;
workspace: IWorkspace | string;
workspace_detail: IWorkspaceLite;
is_deployed: boolean;
}

export interface IProjectLite {
Expand Down

0 comments on commit d8bbdc1

Please sign in to comment.