-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from game-node-app/dev
Lots of work on the MVP of review comments
- Loading branch information
Showing
45 changed files
with
1,243 additions
and
348 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
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
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,34 @@ | ||
import React, { useState } from "react"; | ||
import { BubbleMenu, EditorOptions, useEditor } from "@tiptap/react"; | ||
import { StarterKit } from "@tiptap/starter-kit"; | ||
import { RichTextEditor } from "@mantine/tiptap"; | ||
|
||
interface Props extends Partial<EditorOptions> {} | ||
|
||
export const COMMENT_EDITOR_EXTENSIONS = [StarterKit]; | ||
|
||
const CommentEditor = ({ ...editorOptions }: Props) => { | ||
const editor = useEditor( | ||
{ | ||
...editorOptions, | ||
extensions: COMMENT_EDITOR_EXTENSIONS, | ||
}, | ||
[editorOptions.content], | ||
); | ||
|
||
return ( | ||
<RichTextEditor editor={editor} className={"w-full h-full"}> | ||
{editor && ( | ||
<BubbleMenu editor={editor}> | ||
<RichTextEditor.ControlsGroup> | ||
<RichTextEditor.Bold /> | ||
<RichTextEditor.Italic /> | ||
</RichTextEditor.ControlsGroup> | ||
</BubbleMenu> | ||
)} | ||
<RichTextEditor.Content /> | ||
</RichTextEditor> | ||
); | ||
}; | ||
|
||
export default CommentEditor; |
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,154 @@ | ||
import React, { | ||
MutableRefObject, | ||
RefObject, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
import { | ||
ActionIcon, | ||
Box, | ||
Button, | ||
Group, | ||
LoadingOverlay, | ||
Stack, | ||
Text, | ||
} from "@mantine/core"; | ||
import CommentEditor from "@/components/comment/editor/CommentEditor"; | ||
import { IconX } from "@tabler/icons-react"; | ||
import { Editor } from "@tiptap/core"; | ||
import { | ||
useMutation, | ||
UseMutationOptions, | ||
useQueryClient, | ||
} from "@tanstack/react-query"; | ||
import { CommentService } from "@/wrapper/server"; | ||
import { CreateCommentDto } from "@/wrapper/server"; | ||
import { notifications } from "@mantine/notifications"; | ||
import { useComment } from "@/components/comment/hooks/useComment"; | ||
|
||
interface Props { | ||
/** | ||
* If available, user will be able to modify this comment. <br> | ||
* Ideally, should be cleared when 'onCancel' is called. | ||
*/ | ||
commentId?: string; | ||
onCancel: () => void; | ||
sourceType: CreateCommentDto.sourceType; | ||
sourceId: string; | ||
editorContainerRef?: RefObject<HTMLDivElement>; | ||
} | ||
|
||
const CommentEditorView = ({ | ||
commentId, | ||
sourceType, | ||
sourceId, | ||
onCancel, | ||
editorContainerRef, | ||
}: Props) => { | ||
const queryClient = useQueryClient(); | ||
const editorRef = useRef<Editor>(); | ||
const commentQuery = useComment(commentId, sourceType); | ||
const [previousContent, setPreviousContent] = useState<string | undefined>( | ||
undefined, | ||
); | ||
const [shouldShowActionButtons, setShouldShowActionButtons] = | ||
useState<boolean>(false); | ||
|
||
const clearEditor = () => { | ||
editorRef.current?.commands.clearContent(); | ||
setShouldShowActionButtons(false); | ||
onCancel(); | ||
}; | ||
|
||
const commentMutation = useMutation({ | ||
mutationFn: async () => { | ||
if (editorRef.current == undefined) return; | ||
|
||
const content = editorRef.current?.getHTML(); | ||
if (commentId) { | ||
return CommentService.commentControllerUpdate(commentId, { | ||
sourceType, | ||
content: content, | ||
}); | ||
} | ||
|
||
return CommentService.commentControllerCreate({ | ||
sourceId, | ||
sourceType, | ||
content: content, | ||
}); | ||
}, | ||
onSettled: () => { | ||
queryClient.invalidateQueries({ | ||
queryKey: ["comments", sourceType, sourceId], | ||
}); | ||
}, | ||
onSuccess: () => { | ||
notifications.show({ | ||
color: "green", | ||
message: "Successfully submitted your comment!", | ||
}); | ||
clearEditor(); | ||
}, | ||
}); | ||
|
||
const isUpdateAction = | ||
commentId != undefined && commentQuery.data != undefined; | ||
|
||
useEffect(() => { | ||
if (commentId == undefined && previousContent != undefined) { | ||
setPreviousContent(undefined); | ||
} | ||
|
||
if (commentId != undefined && commentQuery.data != undefined) { | ||
setPreviousContent(commentQuery.data.content); | ||
setShouldShowActionButtons(true); | ||
} | ||
}, [commentId, commentQuery.data, previousContent]); | ||
|
||
return ( | ||
<Stack className={"w-full h-full relative"} ref={editorContainerRef}> | ||
<LoadingOverlay visible={commentQuery.isLoading} /> | ||
{isUpdateAction && ( | ||
<Text c={"dimmed"}> | ||
You are currently editing one of your previous comments. | ||
</Text> | ||
)} | ||
<CommentEditor | ||
content={previousContent} | ||
onUpdate={(props) => { | ||
setShouldShowActionButtons(true); | ||
}} | ||
onCreate={(props) => { | ||
editorRef.current = props.editor; | ||
}} | ||
/> | ||
{shouldShowActionButtons && ( | ||
<Group className={"w-full justify-end"}> | ||
<ActionIcon | ||
size={"lg"} | ||
variant={"default"} | ||
onClick={() => { | ||
clearEditor(); | ||
}} | ||
> | ||
<IconX /> | ||
</ActionIcon> | ||
<Button | ||
type={"button"} | ||
loading={commentMutation.isPending} | ||
onClick={() => { | ||
commentMutation.mutate(); | ||
}} | ||
> | ||
{isUpdateAction ? "Update" : "Submit"} | ||
</Button> | ||
</Group> | ||
)} | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default CommentEditorView; |
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,29 @@ | ||
import { CommentService, FindAllCommentsDto } from "@/wrapper/server"; | ||
import sourceType = FindAllCommentsDto.sourceType; | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
/** | ||
* Retrieves a single comment based on criteria. | ||
* Will only be enabled if commentId is not null. | ||
*/ | ||
export function useComment( | ||
commentId: string | undefined, | ||
sourceType: sourceType, | ||
) { | ||
return useQuery({ | ||
queryKey: ["comment", "findOne", sourceType, commentId], | ||
queryFn: async () => { | ||
if (!commentId) { | ||
return null; | ||
} | ||
|
||
return CommentService.commentControllerFindOneById( | ||
sourceType.valueOf(), | ||
commentId, | ||
); | ||
}, | ||
enabled: commentId != undefined, | ||
retry: 1, | ||
staleTime: Infinity, | ||
}); | ||
} |
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,53 @@ | ||
import { useQuery, useQueryClient } from "@tanstack/react-query"; | ||
import { ExtendedUseQueryResult } from "@/util/types/ExtendedUseQueryResult"; | ||
import { | ||
CommentService, | ||
FindAllCommentsDto, | ||
FindCommentsPaginatedResponseDto, | ||
} from "@/wrapper/server"; | ||
|
||
const DEFAULT_USE_COMMENTS_ORDER_BY = { | ||
createdAt: "DESC", | ||
}; | ||
|
||
export interface UseCommentsProps extends FindAllCommentsDto { | ||
enabled?: boolean; | ||
offset?: number; | ||
limit?: number; | ||
} | ||
|
||
export function useComments({ | ||
enabled = true, | ||
sourceId, | ||
sourceType, | ||
offset = 0, | ||
limit = 10, | ||
orderBy = DEFAULT_USE_COMMENTS_ORDER_BY, | ||
}: UseCommentsProps): ExtendedUseQueryResult<FindCommentsPaginatedResponseDto> { | ||
const queryClient = useQueryClient(); | ||
const queryKey = ["comments", sourceType, sourceId, offset, limit, orderBy]; | ||
|
||
const invalidate = () => { | ||
queryClient.invalidateQueries({ | ||
queryKey: queryKey.slice(0, 2), | ||
}); | ||
}; | ||
return { | ||
...useQuery({ | ||
queryKey, | ||
queryFn: async () => { | ||
return CommentService.commentControllerFindAll({ | ||
sourceId, | ||
sourceType, | ||
orderBy, | ||
limit, | ||
offset, | ||
}); | ||
}, | ||
enabled, | ||
retry: 1, | ||
}), | ||
queryKey, | ||
invalidate, | ||
}; | ||
} |
Oops, something went wrong.