UNPKG

autumn-js

Version:
166 lines (162 loc) 4.17 kB
"use client"; import { usePricingTableBase } from "./chunk-7IU5I5F4.mjs"; // src/libraries/react/hooks/helpers/useAutumnBase.tsx var useAutumnBase = ({ // AutumnContext, context, client, refetchCustomer }) => { const { attachDialog, paywallDialog } = context || {}; const { refetch: refetchPricingTable } = usePricingTableBase({ client }); const attachWithoutDialog = async (params) => { const result = await client.attach(params); if (result.error) { return result; } let data = result.data; if (data?.checkout_url && typeof window !== "undefined") { if (params.openInNewTab) { window.open(data.checkout_url, "_blank"); } else { window.location.href = data.checkout_url; } } await refetchPricingTable(); if (refetchCustomer) { await refetchCustomer(); } attachDialog?.setOpen(false); return result; }; const checkout = async (params) => { const { data, error } = await client.checkout(params); const { dialog, ...rest } = params; if (params.dialog && params.productIds) { throw new Error( "Dialog and productIds are not supported together in checkout()" ); } if (error) { return { data, error }; } const hasPrepaid = data.product.items.some( (item) => item.usage_model === "prepaid" ); const showDialog = hasPrepaid && params.dialog; if (data.url && !showDialog) { if (params.openInNewTab) { window.open(data.url, "_blank"); } else { window.location.href = data.url; } return { data, error }; } if (params.dialog) { attachDialog?.setProps({ checkoutResult: data, checkoutParams: rest }); attachDialog?.setComponent(params.dialog); attachDialog?.setOpen(true); } return { data, error }; }; const attachWithDialog = async (params) => { let { ...rest } = params; const { productId, entityId, entityData } = params; const checkRes = await client.check({ productId, entityId, entityData, withPreview: true }); if (checkRes.error) { return checkRes; } let preview = checkRes.data.preview; if (!preview) { return await attachWithoutDialog(rest); } else { attachDialog?.setProps({ preview, attachParams: rest }); attachDialog?.setOpen(true); } return checkRes; }; const attach = async (params) => { const { dialog } = params; if (dialog && !attachDialog?.open) { attachDialog?.setComponent(dialog); return await attachWithDialog(params); } return await attachWithoutDialog(params); }; const cancel = async (params) => { const res = await client.cancel(params); if (res.error) { return res; } return res; }; const track = async (params) => { const res = await client.track(params); if (res.error) { return res; } return res; }; const openBillingPortal = async (params) => { let defaultParams = { openInNewTab: false }; let finalParams = { ...defaultParams, ...params }; const res = await client.openBillingPortal(finalParams); if (res.error) { return res; } let data = res.data; if (data?.url && typeof window !== "undefined") { if (finalParams.openInNewTab) { window.open(data.url, "_blank"); } else { window.open(data.url, "_self"); } return res; } else { return res; } }; const setupPayment = async (params) => { let defaultParams = { openInNewTab: false }; let finalParams = { ...defaultParams, ...params || {} }; const res = await client.setupPayment(finalParams); if (res.data?.url && typeof window !== "undefined") { if (finalParams.openInNewTab) { window.open(res.data.url, "_blank"); } else { window.open(res.data.url, "_self"); } return res; } else { return res; } }; return { attach, track, cancel, openBillingPortal, setupPayment, checkout }; }; export { useAutumnBase };