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