async function showPage({ rootEl, peertubeHelpers }) {
// Redirect to login page if not auth
if (!peertubeHelpers.isLoggedIn()) return (window.location.href = "/login");
const { translate } = peertubeHelpers;
const loading = await peertubeHelpers.translate("Loading");
rootEl.innerHTML = loading + "...";
// Get settings
const settings = await peertubeHelpers.getSettings();
const currency = settings['sell-currency'];
const description = await peertubeHelpers.markdownRenderer.enhancedMarkdownToHTML(settings["sell-description"]);
const plans = [];
for(let i = 1; i <= 5; i++) {
const name = settings["plan-" + i + "-name"];
const key = settings["plan-" + i + "-key"];
const storage = settings["plan-" + i + "-storage"];
const price = settings["plan-" + i + "-price"];
plans.push({
name: name,
key: key,
storage: storage,
price: price
});
}
// Fetch session id and subscribed plan (if any)
let session_id = null, sub_plan = null;
const response = await fetch(peertubeHelpers.getBaseRouterRoute() + "/get-session-id", {
method: "GET",
headers: peertubeHelpers.getAuthHeader(),
});
const data = await response.json();
// If have error
if (!data || !data.status || (data.status && data.status !== "success")) {
peertubeHelpers.notifier.error(data.message || "Unknown error");
}else{
session_id = data?.data?.session_id;
sub_plan = data?.data?.sub_plan;
}
rootEl.innerHTML = `
${await peertubeHelpers.translate("Choose your Storage Plan")}
${description.length ? description : await peertubeHelpers.translate("You want tu spport us ? Or need more space ? Your in the right place!")}
${(await Promise.all(plans.map(async (plan) =>
`
`))).join("")}
${await translate("Secure transaction with Stripe")}
${session_id ? `
` : ""}
${sub_plan ? `
${await translate("Your current plan")}: ${sub_plan.name}, ${currency}${sub_plan.price} /${await peertubeHelpers.translate("month")}, ${sub_plan.storage} ${await peertubeHelpers.translate("GB Storage")}
` : ""}
`;
const checkForListen = () => {
document.querySelectorAll(".ncdSubscriptionForm").length === 5 ? listenSubmitSubscription(peertubeHelpers) : setTimeout(() => checkForListen(), 1000);
};
checkForListen();
}
function listenSubmitSubscription(peertubeHelpers) {
const baseUrl = peertubeHelpers.getBaseRouterRoute();
// Sub
document.querySelectorAll(".ncdSubscriptionForm").forEach(el => {
el.addEventListener("submit", (e) => {
e.preventDefault();
try {
const form = new URLSearchParams(new FormData(e.target));
fetch(baseUrl + "/create-checkout-session", {
method: "POST",
headers: peertubeHelpers.getAuthHeader(),
body: form,
}).then((res) => res.json()).then((data) => {
if(!data || !data.status || data.status !== "success") {
peertubeHelpers.notifier.error(data.message || "Unknown error");
return;
}
window.location.href = data.data.redirectUrl;
}).catch(err => {
console.error(err);
peertubeHelpers.notifier.error(err);
})
} catch (error) {
peertubeHelpers.notifier.error(error);
}
});
})
// Manage sub
document.getElementById("formManageSub")?.addEventListener("submit", (e) => {
e.preventDefault();
try {
const form = new URLSearchParams(new FormData(e.target));
fetch(baseUrl + "/create-portal-session", {
method: "POST",
headers: peertubeHelpers.getAuthHeader(),
body: form,
}).then((res) => res.json()).then((data) => {
if(!data || !data.status || data.status !== "success") {
peertubeHelpers.notifier.error(data.message || "Unknown error");
return;
}
window.location.href = data.data.redirectUrl;
}).catch(err => {
console.error(err);
peertubeHelpers.notifier.error(err);
})
} catch (error) {
peertubeHelpers.notifier.error(error);
}
});
}
export { showPage };