Overview
TheuseBillingPlans hook is a custom React hook designed to fetch, filter, and manage billing plans for a subscription-based service. It provides an easy way to retrieve billing plans based on authentication and billing cycle preferences.
Usage
Parameters
authToken(string, required): The authentication token for API requests.cycle(string, optional): The billing cycle to filter plans. Defaults toBILLING_CYCLES.MONTHLY.
Returns
An object with the following properties:plans(Array[Plan]): An array of billing plans filtered by the specified cycle.loading(boolean): Indicates whether the plans are currently being fetched.error(string | null): Contains an error message if an error occurred during fetching, otherwise null.
Plan Object Structure
Each plan in theplans array has the following structure:
Example
API Details
The hook internally uses the following API endpoint:- URL:
${Config.BILLING_API_URL}/api/v1/plans - Method: GET
- Headers:
- ‘Content-Type’: ‘application/json’
- ‘Authorization’:
Bearer ${authToken}
Error Handling
If an error occurs during the API call, theerror state will be set to ‘Failed to fetch plans’. You should handle this in your component to display an appropriate error message to the user.
Performance Considerations
- The hook uses
useMemoto optimize performance by only recalculating filtered plans when the cycle or plans change. - API calls are made only once when the component mounts or when the
authTokenchanges, preventing unnecessary requests.
Customization
TheCURRENCIES_MAPPING object can be modified to support additional currencies:
Dependencies
This hook depends on the following:- A
Configobject withBILLING_API_URLdefined BILLING_CYCLESandENTERPRISE_PLAN_NAMEconstants from atypesfile