Overview
TheuseSubscription hook is a custom React hook designed to fetch and manage the current subscription details of a user. It handles the API call, manages loading and error states, and periodically updates the subscription information.
Usage
Parameters
authToken(string, required): The authentication token for API requests.
Returns
An object with the following properties:subscription(Subscription | undefined): The current subscription details, or undefined if not found.loading(boolean): Indicates whether the subscription details are currently being fetched.error(string | null): Contains an error message if an error occurred during the process, otherwise null.
Example
API Details
The hook internally uses the following API endpoint:- URL:
${Config.BILLING_API_URL}/api/v1/subscriptions/current - 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 the error message. You should handle this in your component to display an appropriate error message to the user.
Performance Considerations
- The hook uses
useStateto manage its internal states (subscription, loading, and error). - The API call is made initially when the component mounts and then every 2 minutes using
setInterval. - The interval is cleared when the component unmounts to prevent memory leaks.
Subscription Object Structure
TheparseSubscriptionResponse function maps the API response to the following structure:
Dependencies
This hook depends on the following:- A
Configobject withBILLING_API_URLdefined - A
Subscriptiontype definition (imported from ’@/billing-ui/types’)