({
postId,
sortBy,
}: UsePostCommentsProps)
| 25 | * post has comments. |
| 26 | */ |
| 27 | export const usePostComments = ({ |
| 28 | postId, |
| 29 | sortBy, |
| 30 | }: UsePostCommentsProps): UsePostCommentsResult => { |
| 31 | const { tokenRefreshed } = useContext(AuthContext); |
| 32 | const { requestMethod } = useRequestProtocol(); |
| 33 | const queryKey = generateCommentsQueryKey({ postId, sortBy }); |
| 34 | const { data: comments, isLoading } = useQuery<PostCommentsData>({ |
| 35 | queryKey, |
| 36 | |
| 37 | queryFn: (): Promise<PostCommentsData> => |
| 38 | requestMethod( |
| 39 | POST_COMMENTS_QUERY, |
| 40 | { postId, [initialDataKey]: comments, first: 500, sortBy }, |
| 41 | { requestKey: JSON.stringify(queryKey) }, |
| 42 | ), |
| 43 | enabled: !!postId && tokenRefreshed, |
| 44 | refetchInterval: 60 * 1000, |
| 45 | refetchOnWindowFocus: false, |
| 46 | }); |
| 47 | |
| 48 | return { |
| 49 | queryKey, |
| 50 | comments, |
| 51 | isLoading, |
| 52 | commentsCount: comments?.postComments?.edges?.length || 0, |
| 53 | }; |
| 54 | }; |
no test coverage detected