refactor: rewrite usePaymentPlans with RQ

This commit is contained in:
Rıdvan Akca
2024-03-12 18:03:14 +03:00
parent 7a8e8c1f3e
commit 2ebe71ddd0
7 changed files with 31 additions and 46 deletions

View File

@@ -1,10 +0,0 @@
import appConfig from '../../config/app.js';
import Billing from '../../helpers/billing/index.ee.js';
const getPaymentPlans = async () => {
if (!appConfig.isCloud) return;
return Billing.paddlePlans;
};
export default getPaymentPlans;

View File

@@ -12,7 +12,6 @@ import getFlow from './queries/get-flow.js';
import getFlows from './queries/get-flows.js';
import getInvoices from './queries/get-invoices.ee.js';
import getNotifications from './queries/get-notifications.js';
import getPaymentPlans from './queries/get-payment-plans.ee.js';
import getPermissionCatalog from './queries/get-permission-catalog.ee.js';
import getRole from './queries/get-role.ee.js';
import getRoles from './queries/get-roles.ee.js';
@@ -42,7 +41,6 @@ const queryResolvers = {
getFlows,
getInvoices,
getNotifications,
getPaymentPlans,
getPermissionCatalog,
getRole,
getRoles,

View File

@@ -32,7 +32,6 @@ type Query {
getCurrentUser: User
getConfig(keys: [String]): JSONObject
getInvoices: [Invoice]
getPaymentPlans: [PaymentPlan]
getPermissionCatalog: PermissionCatalog
getRole(id: String!): Role
getRoles: [Role]
@@ -690,13 +689,6 @@ type Invoice {
receipt_url: String
}
type PaymentPlan {
name: String
limit: String
price: String
productId: String
}
type ListSamlAuthProvider {
id: String
name: String

View File

@@ -14,16 +14,19 @@ import TableHead from '@mui/material/TableHead';
import TableRow from '@mui/material/TableRow';
import Paper from '@mui/material/Paper';
import LockIcon from '@mui/icons-material/Lock';
import usePaymentPlans from 'hooks/usePaymentPlans.ee';
import useCurrentUser from 'hooks/useCurrentUser';
import usePaddle from 'hooks/usePaddle.ee';
export default function UpgradeFreeTrial() {
const { plans, loading } = usePaymentPlans();
const { data: plans, isLoading: isPaymentPlansLoading } = usePaymentPlans();
const currentUser = useCurrentUser();
const { loaded: paddleLoaded } = usePaddle();
const [selectedIndex, setSelectedIndex] = React.useState(0);
const selectedPlan = plans?.[selectedIndex];
const selectedPlan = plans?.data?.[selectedIndex];
const updateSelection = (index) => setSelectedIndex(index);
const handleCheckout = React.useCallback(() => {
window.Paddle.Checkout?.open({
product: selectedPlan.productId,
@@ -34,7 +37,9 @@ export default function UpgradeFreeTrial() {
}),
});
}, [selectedPlan, currentUser]);
if (loading || !plans.length) return null;
if (isPaymentPlansLoading || !plans?.data?.length) return null;
return (
<React.Fragment>
<Card sx={{ mb: 3, p: 2 }}>
@@ -82,7 +87,7 @@ export default function UpgradeFreeTrial() {
</TableRow>
</TableHead>
<TableBody>
{plans.map((plan, index) => (
{plans?.data?.map((plan, index) => (
<TableRow
key={plan.productId}
onClick={() => updateSelection(index)}

View File

@@ -15,6 +15,8 @@ export const PaddleProvider = (props) => {
const isCloud = useCloud();
const navigate = useNavigate();
const { data } = usePaddleInfo();
const sandbox = data?.data?.sandbox;
const vendorId = data?.data?.vendorId;
const [loaded, setLoaded] = React.useState(false);
@@ -75,18 +77,18 @@ export const PaddleProvider = (props) => {
React.useEffect(
function initPaddleScript() {
if (!loaded || !data?.data?.vendorId) return;
if (!loaded || !vendorId) return;
if (data?.data?.sandbox) {
if (sandbox) {
window.Paddle.Environment.set('sandbox');
}
window.Paddle.Setup({
vendor: data?.data?.vendorId,
vendor: vendorId,
eventCallback: paddleEventHandler,
});
},
[loaded, data?.data?.sandbox, data?.data?.vendorId, paddleEventHandler],
[loaded, sandbox, vendorId, paddleEventHandler],
);
return (

View File

@@ -1,11 +0,0 @@
import { gql } from '@apollo/client';
export const GET_PAYMENT_PLANS = gql`
query GetPaymentPlans {
getPaymentPlans {
name
limit
price
productId
}
}
`;

View File

@@ -1,9 +1,18 @@
import { useQuery } from '@apollo/client';
import { GET_PAYMENT_PLANS } from 'graphql/queries/get-payment-plans.ee';
import { useQuery } from '@tanstack/react-query';
import api from 'helpers/api';
export default function usePaymentPlans() {
const { data, loading } = useQuery(GET_PAYMENT_PLANS);
return {
plans: data?.getPaymentPlans || [],
loading,
};
const query = useQuery({
queryKey: ['paymentPlans'],
queryFn: async ({ signal }) => {
const { data } = await api.get('/v1/payment/plans', {
signal,
});
return data;
},
});
return query;
}