-
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.
- Loading branch information
1 parent
e17ea14
commit 964f19e
Showing
7 changed files
with
172 additions
and
66 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,126 @@ | ||
import { useState } from "react"; | ||
import { useMutation } from "@apollo/client"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { List, Button, Popover, PopoverContent, PopoverHandler, Textarea, Typography } from "@material-tailwind/react"; | ||
import { format } from "date-fns"; | ||
import { MoreHorizontal, User } from "lucide-react"; | ||
import { SubmitHandler, useForm } from "react-hook-form"; | ||
import { toast } from "react-toastify"; | ||
import { z } from "zod"; | ||
import { CommentDeleteDialog } from "@components/CommentDeleteDialog/CommentDeleteDialog"; | ||
import { QuizCommentsQuery } from "@generated/graphql"; | ||
import { useUserStore } from "@state/userStore"; | ||
import { GET_QUIZ_COMMENTS } from "@utils/queries/QuizComments"; | ||
import { UPDATE_QUIZ_COMMENT } from "@utils/queries/UpdateComment"; | ||
import { quizCommentFormSchema } from "./utils"; | ||
|
||
interface CommentProps { | ||
comment: QuizCommentsQuery["quizComments"][0]; | ||
} | ||
|
||
export const Comment = ({ | ||
comment: { | ||
id: commentId, | ||
text, | ||
createdAt, | ||
user: { id: commentUserId, firstName, lastName }, | ||
}, | ||
}: CommentProps) => { | ||
const [isEditingId, setIsEditingId] = useState(""); | ||
const { | ||
user: { userId }, | ||
} = useUserStore(); | ||
|
||
const [updateComment, { loading: loadingUpdateComment }] = useMutation(UPDATE_QUIZ_COMMENT, { | ||
onCompleted: () => { | ||
setIsEditingId(""); | ||
toast.success("Comment updated successfully!"); | ||
}, | ||
refetchQueries: [GET_QUIZ_COMMENTS], | ||
}); | ||
|
||
const form = useForm<z.infer<typeof quizCommentFormSchema>>({ | ||
resolver: zodResolver(quizCommentFormSchema), | ||
defaultValues: { | ||
text: "", | ||
}, | ||
}); | ||
const { | ||
register, | ||
handleSubmit, | ||
formState: { errors }, | ||
} = form; | ||
|
||
const onSubmit: SubmitHandler<z.infer<typeof quizCommentFormSchema>> = async (values, event) => { | ||
event?.preventDefault(); | ||
try { | ||
updateComment({ variables: { commentId: isEditingId, text: values.text } }); | ||
form.reset(); | ||
} catch (e) { | ||
console.log("Something went wrong", e); | ||
} | ||
}; | ||
|
||
return ( | ||
<div key={commentId} className="flex flex-col gap-1"> | ||
<div className="flex items-center justify-between"> | ||
<div className="flex items-center"> | ||
<User className="mr-2 size-4" /> | ||
<Typography className="font-medium"> | ||
{firstName} | ||
{lastName} | ||
</Typography> | ||
<Typography variant="small" className="ml-2 opacity-70"> | ||
{format(new Date(createdAt), "dd MMM yyyy")} | ||
</Typography> | ||
</div> | ||
|
||
{commentUserId === userId && ( | ||
<Popover> | ||
<PopoverHandler> | ||
<MoreHorizontal className="size-5 opacity-80 hover:cursor-pointer" /> | ||
</PopoverHandler> | ||
<PopoverContent className="p-0"> | ||
<List> | ||
<Button | ||
variant="text" | ||
color="gray" | ||
className="outline-none" | ||
onClick={() => { | ||
setIsEditingId(commentId); | ||
form.reset({ text }); | ||
}} | ||
> | ||
Edit | ||
</Button> | ||
|
||
<CommentDeleteDialog commentId={commentId} /> | ||
</List> | ||
</PopoverContent> | ||
</Popover> | ||
)} | ||
</div> | ||
|
||
{isEditingId ? ( | ||
<form onSubmit={handleSubmit(onSubmit)} className="mt-2"> | ||
<Textarea {...register("text")} size="lg" label="Comment" error={!!errors.text} /> | ||
<div className=" mt-2 flex items-center gap-4"> | ||
<Button | ||
color="blue" | ||
type="submit" | ||
loading={loadingUpdateComment} | ||
disabled={loadingUpdateComment || !form.formState.isDirty} | ||
> | ||
Update | ||
</Button> | ||
<Button color="gray" variant="outlined" type="reset" onClick={() => setIsEditingId("")}> | ||
Cancel | ||
</Button> | ||
</div> | ||
</form> | ||
) : ( | ||
<Typography className="whitespace-pre-line">{text}</Typography> | ||
)} | ||
</div> | ||
); | ||
}; |
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,5 @@ | ||
import { z } from "zod"; | ||
|
||
export const quizCommentFormSchema = z.object({ | ||
text: z.string().min(5, { message: "Enter a comment." }), | ||
}); |
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,7 @@ | ||
import { gql } from "@generated/gql.ts"; | ||
|
||
export const UPDATE_QUIZ_COMMENT = gql(/* GraphQL */ ` | ||
mutation UpdateQuizComments($commentId: String!, $text: String!) { | ||
editComment(commentId: $commentId, text: $text) | ||
} | ||
`); |