• Custom React hook for managing a category.

    Parameters

    • Optional categoryId: string

      The optional ID of the category to fetch and manage.

    Returns UseCategoryReturn

    The returning object contains:

    • category: The current category object, or null if no category is selected or fetched.
    • parents: An array of parent categories, excluding the root category.
    • fetched: Boolean indicating whether the category data has been successfully fetched.
    • rename: CategoryRenameMethod - Function to rename the current category.
    • create: CategoryCreateMethod - Function to create a new category.
    • delete: CategoryRenameMethod - Function to delete the current category.

    Implementation Details

    • Caching: API responses are cached for 5 minutes (300,000 ms) to minimize redundant network requests.
    • Effect Trigger: A useEffect hook is used to initiate the data fetch whenever the categoryId changes.
    • Error Handling: Errors encountered during API calls are logged to the console and re-thrown with additional context for debugging purposes.

    Example

    import { useCategory } from 'react-playmakers';

    // Using the hook in a component
    const CategoryComponent = () => {

    const {
    category,
    parents,
    fetched,
    create,
    rename,
    delete: deleteCategory,
    } = useCategory("CXXXXXXXX");

    useEffect(() => {
    if (fetched) {
    console.log("Category fetched:", category);
    }
    }, [fetched]);

    // Handle creating a category
    const handleCreate = async () => {
    await create("New Category", "CXXXXXXXX", "pXXXXXXXX");
    };

    // Handle renaming the current category
    const handleRename = async () => {
    await rename("Updated Category Name");
    };

    // Handle deleting the current category
    const handleDelete = async () => {
    await deleteCategory();
    };

    return (
    <div>
    <h1>{category?.name}</h1>
    <button onClick={handleCreate}>Create</button>
    <button onClick={handleRename}>Rename</button>
    <button onClick={handleDelete}>Delete</button>
    </div>
    );
    };

Generated using TypeDoc