-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(comment): allow user write comments on task (#125)
- Loading branch information
Showing
25 changed files
with
1,206 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { Request, Response } from 'express' | ||
import { CommentRepository } from '@shared/models' | ||
import { Comment } from '@prisma/client' | ||
import { | ||
BaseController, | ||
Controller, | ||
Res, | ||
Req, | ||
Body, | ||
Next, | ||
ExpressResponse, | ||
Get, | ||
Post, | ||
Put, | ||
Delete | ||
} from '../../core' | ||
import { pusherServer } from '../../lib/pusher-server' | ||
import { AuthRequest } from '../../types' | ||
|
||
@Controller('/comment') | ||
export default class TaskComment extends BaseController { | ||
name: string | ||
commentRepo: CommentRepository | ||
constructor() { | ||
super() | ||
this.name = 'comment' | ||
this.commentRepo = new CommentRepository() | ||
} | ||
|
||
@Get('') | ||
async getCommentByObjectId(@Res() res: Response, @Req() req: Request) { | ||
const { taskId } = req.query as { taskId: string } | ||
|
||
try { | ||
const results = await this.commentRepo.mdCommentGetAllByTask(taskId) | ||
// results.sort((a, b) => (a.createdAt < b.createdAt ? 1 : 0)) | ||
res.json({ status: 200, data: results }) | ||
} catch (error) { | ||
res.json({ | ||
status: 500, | ||
err: error, | ||
data: [] | ||
}) | ||
} | ||
} | ||
|
||
@Post('') | ||
createComment( | ||
@Body() body: Omit<Comment, 'id'>, | ||
@Res() res: ExpressResponse, | ||
@Req() req: AuthRequest | ||
) { | ||
this.commentRepo | ||
.mdCommentAdd(body) | ||
.then(result => { | ||
const { taskId } = body as Comment | ||
const eventName = `event-send-task-comment-${taskId}` | ||
|
||
console.log(`trigger event ${eventName} `, body) | ||
|
||
pusherServer.trigger('team-collab', eventName, { | ||
...result | ||
}) | ||
|
||
res.json({ status: 200, data: result }) | ||
}) | ||
.catch(error => { | ||
console.log({ error }) | ||
res.json({ | ||
status: 500, | ||
err: error | ||
}) | ||
}) | ||
} | ||
|
||
@Put('') | ||
updateComment(@Res() res: Response, @Req() req: AuthRequest, @Next() next) { | ||
const body = req.body as Comment | ||
const { id, ...rest } = body | ||
this.commentRepo | ||
.mdCommentUpdate(id, rest) | ||
.then(result => { | ||
const { taskId } = result as Comment | ||
const eventName = `event-update-task-comment-${taskId}` | ||
|
||
console.log(`trigger event ${eventName} `, body) | ||
|
||
pusherServer.trigger('team-collab', eventName, { | ||
...result | ||
}) | ||
|
||
res.json({ status: 200, data: result }) | ||
}) | ||
.catch(error => { | ||
console.log({ error }) | ||
res.json({ | ||
status: 500, | ||
err: error | ||
}) | ||
}) | ||
} | ||
|
||
@Delete('') | ||
async commentDelete(@Req() req: Request, @Res() res: Response) { | ||
try { | ||
const { id, taskId, updatedBy } = req.query as { | ||
id: string | ||
taskId: string | ||
updatedBy: string | ||
} | ||
const result = await this.commentRepo.mdCommentDel(id) | ||
const eventName = `event-delete-task-comment-${taskId}` | ||
|
||
console.log(`trigger event ${eventName} `, id) | ||
|
||
pusherServer.trigger('team-collab', eventName, { | ||
id, | ||
triggerBy: updatedBy | ||
}) | ||
|
||
res.json({ status: 200, data: result }) | ||
} catch (error) { | ||
res.json({ | ||
status: 500, | ||
err: error | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Comment } from '@prisma/client' | ||
import { pmClient } from './_prisma' | ||
|
||
const mdComment = pmClient.comment | ||
export class CommentRepository { | ||
async mdCommentAdd(data: Omit<Comment, 'id'>) { | ||
return mdComment.create({ | ||
data | ||
}) | ||
} | ||
|
||
async mdCommentAddMany(data: Omit<Comment, 'id'>[]) { | ||
return mdComment.createMany({ | ||
data | ||
}) | ||
} | ||
|
||
async mdCommentDel(id: string) { | ||
return mdComment.delete({ | ||
where: { | ||
id | ||
} | ||
}) | ||
} | ||
|
||
async mdCommentUpdate(id: string, data: Omit<Comment, 'id'>) { | ||
return mdComment.update({ | ||
where: { | ||
id | ||
}, | ||
data: data | ||
}) | ||
} | ||
|
||
async mdCommentGetAllByTask(taskId: string) { | ||
return mdComment.findMany({ | ||
where: { | ||
taskId: taskId | ||
} | ||
}) | ||
} | ||
|
||
async mdCommentGetAllByProject(projectId: string) { | ||
return mdComment.findMany({ | ||
where: { | ||
projectId: projectId | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
102 changes: 102 additions & 0 deletions
102
packages/shared-ui/src/components/Controls/RichTextEditorControl/MentionList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { SuggestionProps } from '@tiptap/suggestion' | ||
import { | ||
ReactElement, | ||
Ref, | ||
forwardRef, | ||
useEffect, | ||
useImperativeHandle, | ||
useState | ||
} from 'react' | ||
|
||
import MemberAvatar from '@/components/MemberAvatar' | ||
|
||
export type TItemBase = { | ||
id: string | ||
label: string | ||
} | ||
type TMemberMentionProps<I> = SuggestionProps<I> | ||
type TMemberMentionRef = Ref<{ onKeyDown: ({ event }: { event: any }) => void }> | ||
|
||
const Mention = <I,>( | ||
props: TMemberMentionProps<I & TItemBase>, | ||
ref: TMemberMentionRef | ||
) => { | ||
const [selectedIndex, setSelectedIndex] = useState(0) | ||
|
||
const selectItem = (index: number) => { | ||
const item = props.items[index] | ||
|
||
if (item) { | ||
const { id, label } = item | ||
props.command({ id, label }) | ||
} | ||
} | ||
|
||
const upHandler = () => { | ||
setSelectedIndex( | ||
(selectedIndex + props.items.length - 1) % props.items.length | ||
) | ||
} | ||
|
||
const downHandler = () => { | ||
setSelectedIndex((selectedIndex + 1) % props.items.length) | ||
} | ||
|
||
const enterHandler = () => { | ||
selectItem(selectedIndex) | ||
} | ||
|
||
useEffect(() => setSelectedIndex(0), [props.items]) | ||
|
||
useImperativeHandle(ref, () => ({ | ||
onKeyDown: ({ event }) => { | ||
if (event.key === 'ArrowUp') { | ||
upHandler() | ||
return true | ||
} | ||
|
||
if (event.key === 'ArrowDown') { | ||
downHandler() | ||
return true | ||
} | ||
|
||
if (event.key === 'Enter') { | ||
enterHandler() | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
})) | ||
|
||
return ( | ||
<div className="items border-gray-200"> | ||
{props.items?.length ? ( | ||
props.items?.map((item, index) => ( | ||
<button | ||
className={`item ${index === selectedIndex ? 'is-selected' : ''}`} | ||
key={index} | ||
onClick={() => { | ||
selectItem(index) | ||
}}> | ||
<div className="flex gap-3 items-start"> | ||
<MemberAvatar uid={item.id || ''} noName={true} /> | ||
<div className="flex flex-col"> | ||
{item.label} | ||
<span className="italic text-gray-600 text-xs"> | ||
{item?.email} | ||
</span> | ||
</div> | ||
</div> | ||
</button> | ||
)) | ||
) : ( | ||
<div className="item">No result</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default forwardRef(Mention) as <I>( | ||
p: TMemberMentionProps<I & TItemBase> & { r: TMemberMentionRef } | ||
) => ReactElement |
Oops, something went wrong.