2022-05-06 17:21:30 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { InstallAppButtonProps } from "../../types";
|
2022-05-06 17:21:30 +00:00
|
|
|
|
|
|
|
export default function InstallAppButton(props: InstallAppButtonProps) {
|
|
|
|
const getLinkToken = async () => {
|
|
|
|
const res = await fetch("/api/integrations/vital/token", {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({}),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error("Failed to get link token");
|
|
|
|
}
|
|
|
|
return await res.json();
|
|
|
|
};
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{props.render({
|
|
|
|
onClick() {
|
|
|
|
setLoading(true);
|
|
|
|
getLinkToken()
|
|
|
|
.then((data) => {
|
|
|
|
setLoading(false);
|
|
|
|
window.open(`${data.url}&token=${data.token}`, "_self");
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
setLoading(false);
|
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
loading: loading,
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|