({
id,
query = COMMENT_BY_ID_QUERY,
options = {},
}: UseCommentByIdProps)
| 23 | } |
| 24 | |
| 25 | const useCommentById = ({ |
| 26 | id, |
| 27 | query = COMMENT_BY_ID_QUERY, |
| 28 | options = {}, |
| 29 | }: UseCommentByIdProps): UseCommentByIdResult => { |
| 30 | const { user } = useAuthContext(); |
| 31 | const { requestMethod } = useRequestProtocol(); |
| 32 | const client = useQueryClient(); |
| 33 | |
| 34 | if (!requestMethod) { |
| 35 | throw new Error('Request method is required in useCommentById'); |
| 36 | } |
| 37 | |
| 38 | const { |
| 39 | data: commentById, |
| 40 | isError, |
| 41 | isLoading, |
| 42 | } = useQuery<CommentOnData>({ |
| 43 | queryKey: generateQueryKey(RequestKey.Comment, user, id), |
| 44 | queryFn: () => requestMethod(query, { id: `${id}` }), |
| 45 | ...options, |
| 46 | enabled: !!id && options.enabled, |
| 47 | }); |
| 48 | const comment = commentById || (options?.initialData as CommentOnData); |
| 49 | |
| 50 | const invalidate = useCallback(() => { |
| 51 | client.invalidateQueries({ |
| 52 | queryKey: generateQueryKey(RequestKey.Comment, user, id), |
| 53 | exact: true, |
| 54 | }); |
| 55 | }, [client, user, id]); |
| 56 | |
| 57 | return useMemo( |
| 58 | () => ({ |
| 59 | comment: comment?.comment, |
| 60 | onEdit: invalidate, |
| 61 | isError, |
| 62 | isLoading, |
| 63 | }), |
| 64 | [comment, isError, isLoading, invalidate], |
| 65 | ); |
| 66 | }; |
| 67 | |
| 68 | export default useCommentById; |
no test coverage detected