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) {
|
2017-10-17 21:17:04 +00:00
|
|
|
let dataType = button.dataset.type
|
|
|
|
|
2019-04-22 06:02:51 +00:00
|
|
|
if(!dataType) {
|
|
|
|
console.error("Missing data type:", button)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let response = await arn.post(`/api/new/${dataType}`)
|
|
|
|
|
|
|
|
if(!response) {
|
|
|
|
throw `Failed creating ${dataType}`
|
|
|
|
}
|
|
|
|
|
|
|
|
let json = await response.json()
|
|
|
|
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) {
|
2017-10-17 09:43:07 +00:00
|
|
|
let confirmType = button.dataset.confirmType
|
|
|
|
let returnPath = button.dataset.returnPath
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 09:43:07 +00:00
|
|
|
let endpoint = arn.findAPIEndpoint(button)
|
|
|
|
|
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)
|
|
|
|
}
|
2017-10-17 09:43:07 +00:00
|
|
|
}
|