87 lines
2.0 KiB
TypeScript
Raw Normal View History

2018-04-02 05:34:16 +00:00
import AnimeNotifier from "../AnimeNotifier"
2017-10-17 09:27:15 +00:00
// Charge up
export function chargeUp(arn: AnimeNotifier, button: HTMLElement) {
2019-11-17 09:25:14 +00:00
const amount = button.dataset.amount
2017-10-17 09:27:15 +00:00
arn.loading(true)
arn.statusMessage.showInfo("Creating PayPal transaction... This might take a few seconds.")
fetch("/api/paypal/payment/create", {
method: "POST",
body: amount,
credentials: "same-origin"
})
.then(response => response.json())
.then(payment => {
if(!payment || !payment.links) {
throw "Error creating PayPal payment"
}
2019-11-17 09:25:14 +00:00
const link = payment.links.find(link => link.rel === "approval_url")
2017-10-17 09:27:15 +00:00
if(!link) {
throw "Error finding PayPal payment link"
}
arn.statusMessage.showInfo("Redirecting to PayPal...", 5000)
2019-11-17 09:25:14 +00:00
const url = link.href
2017-10-17 09:27:15 +00:00
window.location.href = url
})
.catch(err => arn.statusMessage.showError(err))
.then(() => arn.loading(false))
}
2018-11-16 15:07:12 +00:00
// Toggle fade
2019-04-22 09:06:50 +00:00
export function toggleFade(_: AnimeNotifier, button: HTMLElement) {
2019-11-17 09:25:14 +00:00
const elementId = button.dataset.elementId
2019-04-22 06:59:08 +00:00
if(!elementId) {
console.error("Missing element ID:", elementId)
return
}
2019-11-17 09:25:14 +00:00
const element = document.getElementById(elementId)
2018-11-16 15:07:12 +00:00
2019-04-22 06:59:08 +00:00
if(!element) {
console.error("Invalid element ID:", elementId)
return
}
2018-11-16 15:07:12 +00:00
if(element.classList.contains("fade-out")) {
element.classList.remove("fade-out")
} else {
element.classList.add("fade-out")
}
}
2017-10-17 09:27:15 +00:00
// Buy item
export function buyItem(arn: AnimeNotifier, button: HTMLElement) {
2019-11-17 09:25:14 +00:00
const itemId = button.dataset.itemId
const itemName = button.dataset.itemName
const price = button.dataset.price
2017-10-17 09:27:15 +00:00
if(!confirm(`Would you like to buy ${itemName} for ${price} gems?`)) {
return
}
arn.loading(true)
fetch(`/api/shop/buy/${itemId}/1`, {
method: "POST",
credentials: "same-origin"
})
.then(response => response.text())
.then(body => {
2019-06-01 04:55:49 +00:00
if(body !== "") {
2017-10-17 09:27:15 +00:00
throw body
}
2018-04-02 05:34:16 +00:00
2017-10-17 09:27:15 +00:00
return arn.reloadContent()
})
.then(() => arn.statusMessage.showInfo(`You bought ${itemName} for ${price} gems. Check out your inventory to confirm the purchase.`, 4000))
.catch(err => arn.statusMessage.showError(err))
.then(() => arn.loading(false))
2019-11-17 09:44:30 +00:00
}