• Custom React hook for managing a comment.

    Parameters

    • Optional commentId: string

      The unique identifier of the comment to manage. If provided, it will attempt to fetch the comment upon hook initialization.

    Returns UseCommentReturn

    The returning object contains:

    • comment: The current comment object.
    • fetched: Boolean indicating whether the comment data has been fetched successfully.
    • refresh: CommentFetchMethod - Function to refresh the comment data by fetching it again from the API.
    • createApproveComment: CommentCreateStudioMethod - Function to create a comment in an approved state (studio accepted).
    • createComment: CommentCreateMethod - Function to create a new comment.
    • createRejectComment: CommentCreateStudioMethod - Function to create a comment in a rejected state (studio rejected).
    • deleteComment: CommentDeleteMethod - Function to delete the current comment.
    • editComment: CommentEditMethod - Function to edit the body of the current comment.
    • hideComment: CommentHideMethod - Function to hide the current comment.

    Implementation Details

    • Caching: fetched comments are cached for 120 seconds.
    • Effect Trigger: A useEffect hook automatically fetches the comment data when the commentId changes.
    • CRUD Operations: Includes built-in methods for creating, editing, deleting, and fetching comments.
    • Comment Actions: Provides methods to approve, reject, and hide comments by modifying their state.
    • Error Handling: Logs errors encountered during API interactions to the console and re-throws them with additional context for debugging.

    Example

    import { useComment } from 'react-playmakers';

    // Using the hook in a component
    const CommentComponent = () => {
    const {
    comment,
    fetched,
    refresh,
    createComment,
    editComment,
    deleteComment,
    hideComment,
    createApproveComment,
    createRejectComment,
    } = useComment('cXXXXXXXX');

    // Fetch the comment on demand
    useEffect(() => {
    if (!fetched) {
    refresh();
    }
    }, [fetched, refresh]);

    // Handle creating a new comment
    const handleCreate = async () => {
    const newComment = await createComment('SXXXXXXXX', 'New comment body');
    console.log('Created comment:', newComment);
    };

    // Handle editing the current comment
    const handleEdit = async () => {
    if (comment) {
    const updatedComment = await editComment('Updated comment body');
    console.log('Updated comment:', updatedComment);
    }
    };

    // Handle deleting the current comment
    const handleDelete = async () => {
    if (comment) {
    await deleteComment();
    console.log('Comment deleted');
    }
    };

    return (
    <div>
    <h3>Comment Details</h3>
    <p>{comment?.body || 'No comment available'}</p>
    <button onClick={handleCreate}>Create Comment</button>
    <button onClick={handleEdit}>Edit Comment</button>
    <button onClick={handleDelete}>Delete Comment</button>
    </div>
    );
    };

Generated using TypeDoc