refactor: rewrite usePaymentPlans with RQ
This commit is contained in:
@@ -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;
|
|
@@ -12,7 +12,6 @@ import getFlow from './queries/get-flow.js';
|
|||||||
import getFlows from './queries/get-flows.js';
|
import getFlows from './queries/get-flows.js';
|
||||||
import getInvoices from './queries/get-invoices.ee.js';
|
import getInvoices from './queries/get-invoices.ee.js';
|
||||||
import getNotifications from './queries/get-notifications.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 getPermissionCatalog from './queries/get-permission-catalog.ee.js';
|
||||||
import getRole from './queries/get-role.ee.js';
|
import getRole from './queries/get-role.ee.js';
|
||||||
import getRoles from './queries/get-roles.ee.js';
|
import getRoles from './queries/get-roles.ee.js';
|
||||||
@@ -42,7 +41,6 @@ const queryResolvers = {
|
|||||||
getFlows,
|
getFlows,
|
||||||
getInvoices,
|
getInvoices,
|
||||||
getNotifications,
|
getNotifications,
|
||||||
getPaymentPlans,
|
|
||||||
getPermissionCatalog,
|
getPermissionCatalog,
|
||||||
getRole,
|
getRole,
|
||||||
getRoles,
|
getRoles,
|
||||||
|
@@ -32,7 +32,6 @@ type Query {
|
|||||||
getCurrentUser: User
|
getCurrentUser: User
|
||||||
getConfig(keys: [String]): JSONObject
|
getConfig(keys: [String]): JSONObject
|
||||||
getInvoices: [Invoice]
|
getInvoices: [Invoice]
|
||||||
getPaymentPlans: [PaymentPlan]
|
|
||||||
getPermissionCatalog: PermissionCatalog
|
getPermissionCatalog: PermissionCatalog
|
||||||
getRole(id: String!): Role
|
getRole(id: String!): Role
|
||||||
getRoles: [Role]
|
getRoles: [Role]
|
||||||
@@ -690,13 +689,6 @@ type Invoice {
|
|||||||
receipt_url: String
|
receipt_url: String
|
||||||
}
|
}
|
||||||
|
|
||||||
type PaymentPlan {
|
|
||||||
name: String
|
|
||||||
limit: String
|
|
||||||
price: String
|
|
||||||
productId: String
|
|
||||||
}
|
|
||||||
|
|
||||||
type ListSamlAuthProvider {
|
type ListSamlAuthProvider {
|
||||||
id: String
|
id: String
|
||||||
name: String
|
name: String
|
||||||
|
@@ -14,16 +14,19 @@ import TableHead from '@mui/material/TableHead';
|
|||||||
import TableRow from '@mui/material/TableRow';
|
import TableRow from '@mui/material/TableRow';
|
||||||
import Paper from '@mui/material/Paper';
|
import Paper from '@mui/material/Paper';
|
||||||
import LockIcon from '@mui/icons-material/Lock';
|
import LockIcon from '@mui/icons-material/Lock';
|
||||||
|
|
||||||
import usePaymentPlans from 'hooks/usePaymentPlans.ee';
|
import usePaymentPlans from 'hooks/usePaymentPlans.ee';
|
||||||
import useCurrentUser from 'hooks/useCurrentUser';
|
import useCurrentUser from 'hooks/useCurrentUser';
|
||||||
import usePaddle from 'hooks/usePaddle.ee';
|
import usePaddle from 'hooks/usePaddle.ee';
|
||||||
|
|
||||||
export default function UpgradeFreeTrial() {
|
export default function UpgradeFreeTrial() {
|
||||||
const { plans, loading } = usePaymentPlans();
|
const { data: plans, isLoading: isPaymentPlansLoading } = usePaymentPlans();
|
||||||
const currentUser = useCurrentUser();
|
const currentUser = useCurrentUser();
|
||||||
const { loaded: paddleLoaded } = usePaddle();
|
const { loaded: paddleLoaded } = usePaddle();
|
||||||
const [selectedIndex, setSelectedIndex] = React.useState(0);
|
const [selectedIndex, setSelectedIndex] = React.useState(0);
|
||||||
const selectedPlan = plans?.[selectedIndex];
|
const selectedPlan = plans?.data?.[selectedIndex];
|
||||||
const updateSelection = (index) => setSelectedIndex(index);
|
const updateSelection = (index) => setSelectedIndex(index);
|
||||||
|
|
||||||
const handleCheckout = React.useCallback(() => {
|
const handleCheckout = React.useCallback(() => {
|
||||||
window.Paddle.Checkout?.open({
|
window.Paddle.Checkout?.open({
|
||||||
product: selectedPlan.productId,
|
product: selectedPlan.productId,
|
||||||
@@ -34,7 +37,9 @@ export default function UpgradeFreeTrial() {
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
}, [selectedPlan, currentUser]);
|
}, [selectedPlan, currentUser]);
|
||||||
if (loading || !plans.length) return null;
|
|
||||||
|
if (isPaymentPlansLoading || !plans?.data?.length) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
<Card sx={{ mb: 3, p: 2 }}>
|
<Card sx={{ mb: 3, p: 2 }}>
|
||||||
@@ -82,7 +87,7 @@ export default function UpgradeFreeTrial() {
|
|||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHead>
|
</TableHead>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{plans.map((plan, index) => (
|
{plans?.data?.map((plan, index) => (
|
||||||
<TableRow
|
<TableRow
|
||||||
key={plan.productId}
|
key={plan.productId}
|
||||||
onClick={() => updateSelection(index)}
|
onClick={() => updateSelection(index)}
|
||||||
|
@@ -15,6 +15,8 @@ export const PaddleProvider = (props) => {
|
|||||||
const isCloud = useCloud();
|
const isCloud = useCloud();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { data } = usePaddleInfo();
|
const { data } = usePaddleInfo();
|
||||||
|
const sandbox = data?.data?.sandbox;
|
||||||
|
const vendorId = data?.data?.vendorId;
|
||||||
|
|
||||||
const [loaded, setLoaded] = React.useState(false);
|
const [loaded, setLoaded] = React.useState(false);
|
||||||
|
|
||||||
@@ -75,18 +77,18 @@ export const PaddleProvider = (props) => {
|
|||||||
|
|
||||||
React.useEffect(
|
React.useEffect(
|
||||||
function initPaddleScript() {
|
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.Environment.set('sandbox');
|
||||||
}
|
}
|
||||||
|
|
||||||
window.Paddle.Setup({
|
window.Paddle.Setup({
|
||||||
vendor: data?.data?.vendorId,
|
vendor: vendorId,
|
||||||
eventCallback: paddleEventHandler,
|
eventCallback: paddleEventHandler,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[loaded, data?.data?.sandbox, data?.data?.vendorId, paddleEventHandler],
|
[loaded, sandbox, vendorId, paddleEventHandler],
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@@ -1,11 +0,0 @@
|
|||||||
import { gql } from '@apollo/client';
|
|
||||||
export const GET_PAYMENT_PLANS = gql`
|
|
||||||
query GetPaymentPlans {
|
|
||||||
getPaymentPlans {
|
|
||||||
name
|
|
||||||
limit
|
|
||||||
price
|
|
||||||
productId
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
@@ -1,9 +1,18 @@
|
|||||||
import { useQuery } from '@apollo/client';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { GET_PAYMENT_PLANS } from 'graphql/queries/get-payment-plans.ee';
|
|
||||||
|
import api from 'helpers/api';
|
||||||
|
|
||||||
export default function usePaymentPlans() {
|
export default function usePaymentPlans() {
|
||||||
const { data, loading } = useQuery(GET_PAYMENT_PLANS);
|
const query = useQuery({
|
||||||
return {
|
queryKey: ['paymentPlans'],
|
||||||
plans: data?.getPaymentPlans || [],
|
queryFn: async ({ signal }) => {
|
||||||
loading,
|
const { data } = await api.get('/v1/payment/plans', {
|
||||||
};
|
signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user