refactor: remove useBillingAndUsageData hook
This commit is contained in:
@@ -1,52 +0,0 @@
|
||||
import * as React from 'react';
|
||||
import { useQuery } from '@apollo/client';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { DateTime } from 'luxon';
|
||||
import { GET_BILLING_AND_USAGE } from 'graphql/queries/get-billing-and-usage.ee';
|
||||
function transform(billingAndUsageData) {
|
||||
const nextBillDate = billingAndUsageData.subscription.nextBillDate;
|
||||
const nextBillDateTitle = nextBillDate.title;
|
||||
const nextBillDateTitleDateObject = DateTime.fromMillis(
|
||||
Number(nextBillDateTitle),
|
||||
);
|
||||
const formattedNextBillDateTitle = nextBillDateTitleDateObject.isValid
|
||||
? nextBillDateTitleDateObject.toFormat('LLL dd, yyyy')
|
||||
: nextBillDateTitle;
|
||||
return {
|
||||
...billingAndUsageData,
|
||||
subscription: {
|
||||
...billingAndUsageData.subscription,
|
||||
nextBillDate: {
|
||||
...billingAndUsageData.subscription.nextBillDate,
|
||||
title: formattedNextBillDateTitle,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
export default function useBillingAndUsageData() {
|
||||
const location = useLocation();
|
||||
const state = location.state;
|
||||
const { data, loading, startPolling, stopPolling } = useQuery(
|
||||
GET_BILLING_AND_USAGE,
|
||||
);
|
||||
const checkoutCompleted = state?.checkoutCompleted;
|
||||
const hasSubscription = !!data?.getBillingAndUsage?.subscription?.status;
|
||||
React.useEffect(
|
||||
function pollDataUntilSubscriptionIsCreated() {
|
||||
if (checkoutCompleted && !hasSubscription) {
|
||||
startPolling(1000);
|
||||
}
|
||||
},
|
||||
[checkoutCompleted, hasSubscription, startPolling],
|
||||
);
|
||||
React.useEffect(
|
||||
function stopPollingWhenSubscriptionIsCreated() {
|
||||
if (checkoutCompleted && hasSubscription) {
|
||||
stopPolling();
|
||||
}
|
||||
},
|
||||
[checkoutCompleted, hasSubscription, stopPolling],
|
||||
);
|
||||
if (loading) return null;
|
||||
return transform(data.getBillingAndUsage);
|
||||
}
|
@@ -24,7 +24,7 @@ export default function useSubscription() {
|
||||
const checkoutCompleted = state?.checkoutCompleted;
|
||||
const [isPolling, setIsPolling] = React.useState(false);
|
||||
|
||||
const { data, isLoading: isSubscriptionLoading } = useQuery({
|
||||
const { data } = useQuery({
|
||||
queryKey: ['subscription'],
|
||||
queryFn: async ({ signal }) => {
|
||||
const { data } = await api.get(`/v1/users/me/subscription`, {
|
||||
@@ -38,32 +38,17 @@ export default function useSubscription() {
|
||||
|
||||
const subscription = data?.data;
|
||||
|
||||
const hasSubscription = !!subscription?.status;
|
||||
const hasSubscription = subscription?.status === 'active';
|
||||
|
||||
React.useEffect(
|
||||
function pollDataUntilSubscriptionIsCreated() {
|
||||
if (checkoutCompleted && !hasSubscription) {
|
||||
setIsPolling(true);
|
||||
if (checkoutCompleted) {
|
||||
setIsPolling(!hasSubscription);
|
||||
}
|
||||
},
|
||||
[checkoutCompleted, hasSubscription],
|
||||
);
|
||||
|
||||
React.useEffect(
|
||||
function stopPollingWhenSubscriptionIsCreated() {
|
||||
if (checkoutCompleted && hasSubscription) {
|
||||
setIsPolling(false);
|
||||
}
|
||||
},
|
||||
[checkoutCompleted, hasSubscription],
|
||||
);
|
||||
|
||||
const cancellationEffectiveDate = subscription?.cancellationEffectiveDate;
|
||||
|
||||
const hasCancelled = !!cancellationEffectiveDate;
|
||||
|
||||
if (isSubscriptionLoading || !hasCancelled) return null;
|
||||
|
||||
return {
|
||||
data: transform(subscription),
|
||||
};
|
||||
|
Reference in New Issue
Block a user