Overview
TheuseUpdateSubscription hook is a custom React hook designed to update a subscription plan using an API endpoint. It manages the state of the updated subscription and provides a function to trigger the update process.
Usage
Parameters
payload(object, required): An object containing the subscription update details.subscriptionId(string): The ID of the subscription to update.planCode(string): The new plan code for the subscription.
authToken(string, required): The authentication token for API requests.
Returns
An object with the following properties:subscription(Subscription | undefined): The updated subscription details, or undefined if not yet updated.updateSubscription(function): A function to trigger the subscription update process.
Example
API Details
The hook internally uses the following API endpoint:- URL:
${Config.BILLING_API_URL}/api/v1/subscriptions/${subscriptionId} - Method: UPDATE (Note: This should typically be PUT or PATCH in RESTful APIs)
- Headers:
- ‘Content-Type’: ‘application/json’
- ‘Authorization’:
Bearer ${authToken}
- Body: JSON string containing
planCode
Performance Considerations
- The hook uses
useStateto manage the subscription state. - The API call is made only when the
updateSubscriptionfunction is explicitly called, preventing unnecessary requests.
Dependencies
This hook depends on the following:- A
Configobject withBILLING_API_URLdefined - A
Subscriptiontype definition (imported from ’@/billing-ui/types’)