refactor: rewrite usePaddleInfo 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 getPaddleInfo = async () => {
|
|
||||||
if (!appConfig.isCloud) return;
|
|
||||||
|
|
||||||
return Billing.paddleInfo;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getPaddleInfo;
|
|
@@ -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 getPaddleInfo from './queries/get-paddle-info.ee.js';
|
|
||||||
import getPaymentPlans from './queries/get-payment-plans.ee.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';
|
||||||
@@ -43,7 +42,6 @@ const queryResolvers = {
|
|||||||
getFlows,
|
getFlows,
|
||||||
getInvoices,
|
getInvoices,
|
||||||
getNotifications,
|
getNotifications,
|
||||||
getPaddleInfo,
|
|
||||||
getPaymentPlans,
|
getPaymentPlans,
|
||||||
getPermissionCatalog,
|
getPermissionCatalog,
|
||||||
getRole,
|
getRole,
|
||||||
|
@@ -32,7 +32,6 @@ type Query {
|
|||||||
getCurrentUser: User
|
getCurrentUser: User
|
||||||
getConfig(keys: [String]): JSONObject
|
getConfig(keys: [String]): JSONObject
|
||||||
getInvoices: [Invoice]
|
getInvoices: [Invoice]
|
||||||
getPaddleInfo: GetPaddleInfo
|
|
||||||
getPaymentPlans: [PaymentPlan]
|
getPaymentPlans: [PaymentPlan]
|
||||||
getPermissionCatalog: PermissionCatalog
|
getPermissionCatalog: PermissionCatalog
|
||||||
getRole(id: String!): Role
|
getRole(id: String!): Role
|
||||||
@@ -683,11 +682,6 @@ type Usage {
|
|||||||
task: Int
|
task: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetPaddleInfo {
|
|
||||||
sandbox: Boolean
|
|
||||||
vendorId: Int
|
|
||||||
}
|
|
||||||
|
|
||||||
type Invoice {
|
type Invoice {
|
||||||
id: Int
|
id: Int
|
||||||
amount: Float
|
amount: Float
|
||||||
|
@@ -1,21 +1,27 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
import useCloud from 'hooks/useCloud';
|
import useCloud from 'hooks/useCloud';
|
||||||
import usePaddleInfo from 'hooks/usePaddleInfo.ee';
|
import usePaddleInfo from 'hooks/usePaddleInfo.ee';
|
||||||
import apolloClient from 'graphql/client';
|
import apolloClient from 'graphql/client';
|
||||||
|
|
||||||
export const PaddleContext = React.createContext({
|
export const PaddleContext = React.createContext({
|
||||||
loaded: false,
|
loaded: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const PaddleProvider = (props) => {
|
export const PaddleProvider = (props) => {
|
||||||
const { children } = props;
|
const { children } = props;
|
||||||
const isCloud = useCloud();
|
const isCloud = useCloud();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { sandbox, vendorId } = usePaddleInfo();
|
const { data } = usePaddleInfo();
|
||||||
|
|
||||||
const [loaded, setLoaded] = React.useState(false);
|
const [loaded, setLoaded] = React.useState(false);
|
||||||
|
|
||||||
const paddleEventHandler = React.useCallback(
|
const paddleEventHandler = React.useCallback(
|
||||||
async (payload) => {
|
async (payload) => {
|
||||||
const { event, eventData } = payload;
|
const { event, eventData } = payload;
|
||||||
|
|
||||||
if (event === 'Checkout.Close') {
|
if (event === 'Checkout.Close') {
|
||||||
const completed = eventData.checkout?.completed;
|
const completed = eventData.checkout?.completed;
|
||||||
if (completed) {
|
if (completed) {
|
||||||
@@ -24,6 +30,7 @@ export const PaddleProvider = (props) => {
|
|||||||
await apolloClient.refetchQueries({
|
await apolloClient.refetchQueries({
|
||||||
include: ['GetTrialStatus', 'GetBillingAndUsage'],
|
include: ['GetTrialStatus', 'GetBillingAndUsage'],
|
||||||
});
|
});
|
||||||
|
|
||||||
navigate(URLS.SETTINGS_BILLING_AND_USAGE, {
|
navigate(URLS.SETTINGS_BILLING_AND_USAGE, {
|
||||||
state: { checkoutCompleted: true },
|
state: { checkoutCompleted: true },
|
||||||
});
|
});
|
||||||
@@ -32,15 +39,18 @@ export const PaddleProvider = (props) => {
|
|||||||
},
|
},
|
||||||
[navigate],
|
[navigate],
|
||||||
);
|
);
|
||||||
|
|
||||||
const value = React.useMemo(() => {
|
const value = React.useMemo(() => {
|
||||||
return {
|
return {
|
||||||
loaded,
|
loaded,
|
||||||
};
|
};
|
||||||
}, [loaded]);
|
}, [loaded]);
|
||||||
|
|
||||||
React.useEffect(
|
React.useEffect(
|
||||||
function loadPaddleScript() {
|
function loadPaddleScript() {
|
||||||
if (!isCloud) return;
|
if (!isCloud) return;
|
||||||
const isInjected = document.getElementById('paddle-js');
|
const isInjected = document.getElementById('paddle-js');
|
||||||
|
|
||||||
if (isInjected) {
|
if (isInjected) {
|
||||||
setLoaded(true);
|
setLoaded(true);
|
||||||
return;
|
return;
|
||||||
@@ -51,28 +61,34 @@ export const PaddleProvider = (props) => {
|
|||||||
g.defer = true;
|
g.defer = true;
|
||||||
g.async = true;
|
g.async = true;
|
||||||
g.id = 'paddle-js';
|
g.id = 'paddle-js';
|
||||||
|
|
||||||
if (s.parentNode) {
|
if (s.parentNode) {
|
||||||
s.parentNode.insertBefore(g, s);
|
s.parentNode.insertBefore(g, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
g.onload = function () {
|
g.onload = function () {
|
||||||
setLoaded(true);
|
setLoaded(true);
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
[isCloud],
|
[isCloud],
|
||||||
);
|
);
|
||||||
|
|
||||||
React.useEffect(
|
React.useEffect(
|
||||||
function initPaddleScript() {
|
function initPaddleScript() {
|
||||||
if (!loaded || !vendorId) return;
|
if (!loaded || !data?.data?.vendorId) return;
|
||||||
if (sandbox) {
|
|
||||||
|
if (data?.data?.sandbox) {
|
||||||
window.Paddle.Environment.set('sandbox');
|
window.Paddle.Environment.set('sandbox');
|
||||||
}
|
}
|
||||||
|
|
||||||
window.Paddle.Setup({
|
window.Paddle.Setup({
|
||||||
vendor: vendorId,
|
vendor: data?.data?.vendorId,
|
||||||
eventCallback: paddleEventHandler,
|
eventCallback: paddleEventHandler,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[loaded, sandbox, vendorId, paddleEventHandler],
|
[loaded, data?.data?.sandbox, data?.data?.vendorId, paddleEventHandler],
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PaddleContext.Provider value={value}>{children}</PaddleContext.Provider>
|
<PaddleContext.Provider value={value}>{children}</PaddleContext.Provider>
|
||||||
);
|
);
|
||||||
|
@@ -1,9 +0,0 @@
|
|||||||
import { gql } from '@apollo/client';
|
|
||||||
export const GET_PADDLE_INFO = gql`
|
|
||||||
query GetPaddleInfo {
|
|
||||||
getPaddleInfo {
|
|
||||||
sandbox
|
|
||||||
vendorId
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
@@ -1,10 +1,17 @@
|
|||||||
import { useQuery } from '@apollo/client';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { GET_PADDLE_INFO } from 'graphql/queries/get-paddle-info.ee';
|
import api from 'helpers/api';
|
||||||
|
|
||||||
export default function usePaddleInfo() {
|
export default function usePaddleInfo() {
|
||||||
const { data, loading } = useQuery(GET_PADDLE_INFO);
|
const query = useQuery({
|
||||||
return {
|
queryKey: ['paddleInfo'],
|
||||||
sandbox: data?.getPaddleInfo?.sandbox,
|
queryFn: async ({ signal }) => {
|
||||||
vendorId: data?.getPaddleInfo?.vendorId,
|
const { data } = await api.get('/v1/payment/paddle-info', {
|
||||||
loading,
|
signal,
|
||||||
};
|
});
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user