feat(UsageAlert): use new plan upgrade page

This commit is contained in:
Ali BARIN
2023-03-21 20:25:46 +00:00
parent 6f2ca00263
commit e25aab742b
4 changed files with 18 additions and 41 deletions

View File

@@ -1,4 +1,5 @@
import * as React from 'react'; import * as React from 'react';
import { Link } from 'react-router-dom';
import Alert from '@mui/material/Alert'; import Alert from '@mui/material/Alert';
import Snackbar from '@mui/material/Snackbar'; import Snackbar from '@mui/material/Snackbar';
import Typography from '@mui/material/Typography'; import Typography from '@mui/material/Typography';
@@ -34,8 +35,9 @@ export default function UsageAlert() {
</Typography> </Typography>
<Button <Button
component={Link}
size="small" size="small"
href={usageAlert.url} to={usageAlert.url}
sx={{ minWidth: 100 }} sx={{ minWidth: 100 }}
> >
{formatMessage('usageAlert.viewPlans')} {formatMessage('usageAlert.viewPlans')}

View File

@@ -1,10 +0,0 @@
import { gql } from '@apollo/client';
export const GET_PAYMENT_PORTAL_URL = gql`
query GetPaymentPortalUrl {
getPaymentPortalUrl {
url
}
}
`;

View File

@@ -1,16 +0,0 @@
import { useQuery } from '@apollo/client';
import { GET_PAYMENT_PORTAL_URL } from 'graphql/queries/get-payment-portal-url.ee';
type UsePaymentPortalUrlReturn = {
url: string;
loading: boolean;
};
export default function usePaymentPortalUrl(): UsePaymentPortalUrlReturn {
const { data, loading } = useQuery(GET_PAYMENT_PORTAL_URL);
return {
url: data?.getPaymentPortalUrl?.url,
loading
};
}

View File

@@ -1,33 +1,34 @@
import * as URLS from 'config/urls';
import useFormatMessage from './useFormatMessage'; import useFormatMessage from './useFormatMessage';
import useUsageData from './useUsageData.ee'; import useUsageData from './useUsageData.ee';
import usePaymentPortalUrl from './usePaymentPortalUrl.ee';
type UseUsageAlertReturn = { type UseUsageAlertReturn = {
showAlert: boolean; showAlert: true;
hasExceededLimit?: boolean; hasExceededLimit: boolean;
alertMessage?: string; alertMessage: string;
url?: string; url: string;
consumptionPercentage?: number; consumptionPercentage: number;
}; };
export default function useUsageAlert(): UseUsageAlertReturn { type UseUsageNoAlertReturn = {
const { url, loading: paymentPortalUrlLoading } = usePaymentPortalUrl(); showAlert: false;
};
export default function useUsageAlert(): UseUsageAlertReturn | UseUsageNoAlertReturn {
const { const {
allowedTaskCount, allowedTaskCount,
consumedTaskCount, consumedTaskCount,
nextResetAt, nextResetAt,
loading: usageDataLoading loading
} = useUsageData(); } = useUsageData();
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
if (paymentPortalUrlLoading || usageDataLoading) { if (loading) {
return { showAlert: false }; return { showAlert: false };
} }
const hasLoaded = !paymentPortalUrlLoading || usageDataLoading;
const withinUsageThreshold = consumedTaskCount > allowedTaskCount * 0.7; const withinUsageThreshold = consumedTaskCount > allowedTaskCount * 0.7;
const consumptionPercentage = consumedTaskCount / allowedTaskCount * 100; const consumptionPercentage = consumedTaskCount / allowedTaskCount * 100;
const showAlert = hasLoaded && withinUsageThreshold;
const hasExceededLimit = consumedTaskCount >= allowedTaskCount; const hasExceededLimit = consumedTaskCount >= allowedTaskCount;
const alertMessage = formatMessage('usageAlert.informationText', { const alertMessage = formatMessage('usageAlert.informationText', {
@@ -37,10 +38,10 @@ export default function useUsageAlert(): UseUsageAlertReturn {
}); });
return { return {
showAlert, showAlert: withinUsageThreshold,
hasExceededLimit, hasExceededLimit,
alertMessage, alertMessage,
consumptionPercentage, consumptionPercentage,
url, url: URLS.SETTINGS_PLAN_UPGRADE,
}; };
} }