export const createPlaidLink = (options: PlaidLinkOptions) => {
const [loading, error] = useScript(PLAID_LINK_STABLE_URL);
const [plaid, setPlaid] = createSignal<PlaidFactory | null>();
const [iframeLoaded, setIframeLoaded] = createSignal(false);
const products = ((options as PlaidLinkOptionsWithPublicKey).product || [])
.slice()
.sort()
.join(",");
createEffect(() => {
if (loading()) {
console.log(`loading: ${loading()}`);
return;
}
if (
!options.token &&
!(options as PlaidLinkOptionsWithPublicKey).publicKey
) {
console.log("no token");
return;
}
if (error() || !window.Plaid) {
console.error("Error loading Plaid", error);
}
// exit
if (plaid() != null) {
plaid()!.exit({ force: true }, () => plaid()!.destroy());
}
const next = createPlaid(
{
...options,
onLoad: () => {
setIframeLoaded(true);
options.onLoad && options.onLoad();
},
},
window.Plaid.create
);
setPlaid(next);
onCleanup(() => {
next.exit({ force: true }, () => next.destroy());
});
});
const ready = createMemo(
() => plaid() != null && (!loading || iframeLoaded())
);
const openNoOp = () => {
if (!options.token) {
console.warn(
"solid-plaid-link: You cannot call open() without a valid token supplied to usePlaidLink. This is a no-op."
);
}
};
return {
error,
ready,
exit: plaid() ? plaid()!.exit : noop,
open: plaid() ? plaid()!.open : openNoOp,
};
};