-
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 #21 from L-Steinmacher/refactor
refactor: userController hook to abstract and clean up CommentLayout
- Loading branch information
Showing
3 changed files
with
104 additions
and
217 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,89 @@ | ||
import { useSession } from "next-auth/react"; | ||
import { type RefObject, useEffect, useRef, useState } from "react"; | ||
import { type Comment } from "~/interfaces/comments"; | ||
import { api } from "~/utils/api"; | ||
|
||
export default function useController({ slug }: { slug: string}) { | ||
const { data: commentsData } = api.comments.getCommentsForPost.useQuery({ | ||
slug, | ||
}); | ||
|
||
const [submitting, setSubmitting] = useState<boolean>(false); | ||
const [token, setToken] = useState<string | null>(''); | ||
const [comment, setComment] = useState<string>(''); | ||
const [allComments, setAllComments] = useState<Comment[]>( | ||
commentsData || [], | ||
); | ||
const [errors, setErrors] = useState<string[]>([]); | ||
const [gotime, setGotime] = useState<boolean>(false); | ||
|
||
const { data: sessionData } = useSession(); | ||
|
||
const commentContainerRef: RefObject<HTMLDivElement> = useRef(null); | ||
const userIsLoggedIn = !!sessionData; | ||
|
||
const utils = api.useContext(); | ||
// const createCommentMutation = api.comments.createComment.useMutation(); | ||
|
||
useEffect(() => { | ||
if (commentsData) { | ||
setAllComments(commentsData); | ||
} | ||
}, [commentsData]); | ||
|
||
const addComment = api.comments.createComment.useMutation({ | ||
onSuccess: async () => { | ||
await utils.comments.getCommentsForPost.refetch({ slug }); | ||
const lastComment = commentContainerRef.current | ||
?.lastElementChild as HTMLElement | null; | ||
if (lastComment) { | ||
lastComment.scrollIntoView({ behavior: 'smooth' }); | ||
} | ||
}, | ||
onError: error => { | ||
console.error('Error adding comment:', error); | ||
setErrors(prevErrors => [...prevErrors, error.message]); | ||
}, | ||
onSettled: () => { | ||
setSubmitting(false); | ||
setComment(''); | ||
setGotime(false); | ||
}, | ||
}); | ||
|
||
const submitComment = () => { | ||
setErrors([]); | ||
|
||
addComment.mutate({ | ||
content: comment, | ||
postSlug: slug, | ||
token: token || '', | ||
}); | ||
}; | ||
|
||
// First we wait for the recaptcha token to be set, only then will the boolean gotime to be true | ||
useEffect(() => { | ||
if (!gotime) { | ||
return; | ||
} | ||
submitComment(); | ||
// linkter wants submitComment in dep array but that causes issues. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [gotime]); | ||
|
||
return { | ||
addComment, | ||
allComments, | ||
comment, | ||
commentContainerRef, | ||
errors, | ||
gotime, | ||
setComment, | ||
setGotime, | ||
setToken, | ||
setSubmitting, | ||
submitting, | ||
token, | ||
userIsLoggedIn, | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.