56 lines
1.2 KiB
TypeScript
Raw Normal View History

2018-04-02 05:34:16 +00:00
import AnimeNotifier from "../AnimeNotifier"
2017-10-17 09:43:07 +00:00
2017-10-17 21:17:04 +00:00
// New
2019-04-22 06:02:51 +00:00
export async function newObject(arn: AnimeNotifier, button: HTMLButtonElement) {
2019-11-17 09:25:14 +00:00
const dataType = button.dataset.type
2017-10-17 21:17:04 +00:00
2019-04-22 06:02:51 +00:00
if(!dataType) {
console.error("Missing data type:", button)
return
}
try {
2019-11-17 09:25:14 +00:00
const response = await arn.post(`/api/new/${dataType}`)
2019-04-22 06:02:51 +00:00
if(!response) {
throw `Failed creating ${dataType}`
}
2019-11-17 09:25:14 +00:00
const json = await response.json()
2019-04-22 06:02:51 +00:00
await arn.app.load(`/${dataType}/${json.id}/edit`)
} catch(err) {
arn.statusMessage.showError(err)
}
2017-10-17 21:17:04 +00:00
}
2017-10-17 09:43:07 +00:00
// Delete
2019-04-22 06:02:51 +00:00
export async function deleteObject(arn: AnimeNotifier, button: HTMLButtonElement) {
2019-11-17 09:25:14 +00:00
const confirmType = button.dataset.confirmType
const returnPath = button.dataset.returnPath
2017-10-17 09:43:07 +00:00
if(!confirm(`Are you sure you want to delete this ${confirmType}?`)) {
return
}
2018-03-26 19:54:53 +00:00
// Double confirmation on anime
if(confirmType === "anime") {
if(!confirm(`Just making sure because this is not reversible. Are you absolutely sure you want to delete this ${confirmType}?`)) {
return
}
}
2019-11-17 09:25:14 +00:00
const endpoint = arn.findAPIEndpoint(button)
2017-10-17 09:43:07 +00:00
2019-04-22 06:02:51 +00:00
try {
await arn.post(endpoint + "/delete")
2018-11-12 05:52:02 +00:00
if(returnPath) {
2019-04-22 06:02:51 +00:00
await arn.app.load(returnPath)
2018-11-12 05:52:02 +00:00
} else {
2019-04-22 06:02:51 +00:00
await arn.reloadContent()
2018-11-12 05:52:02 +00:00
}
2019-04-22 06:02:51 +00:00
} catch(err) {
arn.statusMessage.showError(err)
}
2019-11-17 09:44:30 +00:00
}