chore: Use types from the web package

This commit is contained in:
Faruk AYDIN
2024-01-15 13:30:48 +01:00
parent 7831f2925b
commit 159931a6ea
77 changed files with 934 additions and 378 deletions

View File

@@ -2,15 +2,21 @@ import * as React from 'react';
import { useQuery } from '@apollo/client';
import { useLocation } from 'react-router-dom';
import { DateTime } from 'luxon';
import { TSubscription } from '@automatisch/types';
import { TSubscription } from 'types';
import { GET_BILLING_AND_USAGE } from 'graphql/queries/get-billing-and-usage.ee';
function transform(billingAndUsageData: NonNullable<UseBillingAndUsageDataReturn>) {
function transform(
billingAndUsageData: NonNullable<UseBillingAndUsageDataReturn>
) {
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;
const nextBillDateTitleDateObject = DateTime.fromMillis(
Number(nextBillDateTitle)
);
const formattedNextBillDateTitle = nextBillDateTitleDateObject.isValid
? nextBillDateTitleDateObject.toFormat('LLL dd, yyyy')
: nextBillDateTitle;
return {
...billingAndUsageData,
@@ -19,8 +25,8 @@ function transform(billingAndUsageData: NonNullable<UseBillingAndUsageDataReturn
nextBillDate: {
...billingAndUsageData.subscription.nextBillDate,
title: formattedNextBillDateTitle,
}
}
},
},
};
}
@@ -28,27 +34,35 @@ type UseBillingAndUsageDataReturn = {
subscription: TSubscription;
usage: {
task: number;
}
};
} | null;
export default function useBillingAndUsageData(): UseBillingAndUsageDataReturn {
const location = useLocation();
const state = location.state as { checkoutCompleted: boolean };
const { data, loading, startPolling, stopPolling } = useQuery(GET_BILLING_AND_USAGE);
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 pollDataUntilSubscriptionIsCreated() {
if (checkoutCompleted && !hasSubscription) {
startPolling(1000);
}
},
[checkoutCompleted, hasSubscription, startPolling]
);
React.useEffect(function stopPollingWhenSubscriptionIsCreated() {
if (checkoutCompleted && hasSubscription) {
stopPolling();
}
}, [checkoutCompleted, hasSubscription, stopPolling]);
React.useEffect(
function stopPollingWhenSubscriptionIsCreated() {
if (checkoutCompleted && hasSubscription) {
stopPolling();
}
},
[checkoutCompleted, hasSubscription, stopPolling]
);
if (loading) return null;