• Custom React hook for managing a list of rewards associated with a project.

    This hook provides functionality to fetch, update, delete, and redeem rewards. It also handles caching, state updates, and error handling for smoother integration with APIs and user interfaces.

    Parameters

    • Optional projectId: string

      The ID of the project for which rewards are fetched. If omitted, rewards are not project-specific.

    • Optional params: {
          [key: string]: any;
      } = {}

      Query parameters to filter or modify the rewards request.

      • [key: string]: any

    Returns UseRewardsReturn

    The returning object contains:

    • fetched: Boolean indicating whether the rewards have been successfully fetched.
    • rewards: The list of rewards, extended with additional methods.
    • refresh: FetchRewardsMethod - Function to refresh the rewards list.

    Implementation Details

    • Caching: API responses are cached for 120 seconds (120,000 ms) to minimize redundant network requests.
    • Effect Trigger: The useEffect hook initiates the fetch process when the projectId or params change.
    • Error Handling: Any errors encountered during API operations are logged to the console and re-thrown with additional context.
    • Reward Extensions: Rewards are extended with additional methods (delete, push, redeem) for easier handling within UI components.

    Example

    import { useRewards } from 'react-playmakers';

    const RewardsList = () => {
    const { rewardList, refresh, fetched } = useRewards('pXXXXXXXX', { status: 'active' });

    useEffect(() => {
    if (fetched) {
    console.log('Fetched rewards:', rewardList);
    }
    }, [fetched, rewardList]);

    return (
    <div>
    <h1>Rewards</h1>
    <button onClick={() => refresh(true)}>Refresh Rewards</button>
    <ul>
    {rewardList.map(reward => (
    <li key={reward.id}>
    {reward.name} - {reward.points} points
    <button onClick={reward.delete}>Delete</button>
    <button onClick={() => reward.push({ name: 'Updated Name' })}>
    Update
    </button>
    <button onClick={reward.redeem}>Redeem</button>
    </li>
    ))}
    </ul>
    </div>
    );
    };

Generated using TypeDoc