autumn-js
Version:
Autumn JS Library
148 lines (144 loc) • 3.55 kB
JavaScript
"use client";
import {
useAutumnContext
} from "./chunk-ILWLFCTA.mjs";
// src/libraries/react/hooks/useAutumn.tsx
var useAutumn = () => {
const { client } = useAutumnContext();
const {
prodChangeDialog,
paywallDialog,
pricingTableProvider,
customerProvider
} = useAutumnContext();
let {
setProps: setProdChangeDialogProps,
setOpen: setProdChangeDialogOpen,
setComponent: setProdChangeComponent
} = prodChangeDialog;
let {
setProps: setPaywallDialogProps,
setOpen: setPaywallDialogOpen,
setComponent: setPaywallComponent
} = paywallDialog;
const attachWithDialog = async (params) => {
const attachWithoutDialog = async (options) => {
let { dialog, ...rest } = params;
return await attach(rest);
};
const { productId, entityId, entityData } = params;
const checkRes = await client.check({
productId,
entityId,
entityData,
withPreview: "formatted"
});
if (checkRes.error) {
return checkRes;
}
let preview = checkRes.data.preview;
if (!preview) {
return await attachWithoutDialog();
} else {
setProdChangeDialogProps({
preview
});
setProdChangeDialogOpen(true);
}
return checkRes;
};
const attach = async (params) => {
const { dialog, callback, openInNewTab } = params;
if (dialog) {
setProdChangeComponent(dialog);
return await attachWithDialog(params);
}
const result = await client.attach(params);
if (result.error) {
return result;
}
let data = result.data;
if (data?.checkout_url && typeof window !== "undefined") {
if (openInNewTab) {
window.open(data.checkout_url, "_blank");
} else {
window.location.href = data.checkout_url;
}
}
try {
await callback?.();
} catch (error) {
return result;
}
await Promise.all([
pricingTableProvider.pricingTableProducts ? pricingTableProvider.refetch().catch((error) => {
console.warn("Failed to refetch pricing table data");
console.warn(error);
}) : Promise.resolve(),
customerProvider.refetchFirstTwo()
]);
if (setProdChangeDialogOpen) {
setProdChangeDialogOpen(false);
}
return result;
};
const cancel = async (params) => {
const res = await client.cancel(params);
if (res.error) {
return res;
}
return res;
};
const check = async (params) => {
let { dialog, withPreview } = params;
if (dialog) {
setPaywallComponent(dialog);
}
const res = await client.check({
...params,
withPreview: withPreview || dialog ? "formatted" : void 0
});
if (res.error) {
return res;
}
let data = res.data;
if (data && data.preview && dialog) {
let preview = data.preview;
setPaywallDialogProps({
preview
});
setPaywallDialogOpen(true);
}
return res;
};
const track = async (params) => {
const res = await client.track(params);
if (res.error) {
return res;
}
return res;
};
const openBillingPortal = async (params) => {
const res = await client.openBillingPortal(params);
if (res.error) {
return res;
}
let data = res.data;
if (data?.url && typeof window !== "undefined") {
window.open(data.url, "_blank");
return res;
} else {
return res;
}
};
return {
attach,
check,
track,
cancel,
openBillingPortal
};
};
export {
useAutumn
};