Implemented soundtrack deletion

This commit is contained in:
Eduard Urbach 2017-10-17 11:43:07 +02:00
parent d65f3fd7bb
commit 96c0d22192
3 changed files with 30 additions and 3 deletions

View File

@ -19,6 +19,7 @@ import (
func Edit(ctx *aero.Context) string { func Edit(ctx *aero.Context) string {
id := ctx.Get("id") id := ctx.Get("id")
track, err := arn.GetSoundTrack(id) track, err := arn.GetSoundTrack(id)
user := utils.GetUser(ctx)
if err != nil { if err != nil {
return ctx.Error(http.StatusNotFound, "Track not found", err) return ctx.Error(http.StatusNotFound, "Track not found", err)
@ -37,11 +38,11 @@ func Edit(ctx *aero.Context) string {
ctx.Data.(*arn.OpenGraph).Tags["og:image"] = track.MainAnime().Image.Large ctx.Data.(*arn.OpenGraph).Tags["og:image"] = track.MainAnime().Image.Large
} }
return ctx.HTML(components.SoundTrackTabs(track) + EditForm(track, "Edit soundtrack")) return ctx.HTML(components.SoundTrackTabs(track) + EditForm(track, "Edit soundtrack", user))
} }
// EditForm ... // EditForm ...
func EditForm(obj interface{}, title string) string { func EditForm(obj interface{}, title string, user *arn.User) string {
t := reflect.TypeOf(obj).Elem() t := reflect.TypeOf(obj).Elem()
v := reflect.ValueOf(obj).Elem() v := reflect.ValueOf(obj).Elem()
id := reflect.Indirect(v.FieldByName("ID")) id := reflect.Indirect(v.FieldByName("ID"))
@ -59,6 +60,12 @@ func EditForm(obj interface{}, title string) string {
RenderObject(&b, obj, "") RenderObject(&b, obj, "")
if user != nil && (user.Role == "editor" || user.Role == "admin") {
b.WriteString(`<div class="buttons">`)
b.WriteString(`<button class="action" data-action="deleteObject" data-trigger="click" data-return-path="/soundtracks" data-confirm-type="soundtrack">` + utils.Icon("trash") + `Delete ` + t.Name() + `</button>`)
b.WriteString(`</div>`)
}
b.WriteString("</div>") b.WriteString("</div>")
b.WriteString("</div>") b.WriteString("</div>")
@ -119,7 +126,9 @@ func RenderField(b *bytes.Buffer, v *reflect.Value, field reflect.StructField, i
b.WriteString(`</div>`) b.WriteString(`</div>`)
} }
b.WriteString(`<div class="buttons"><button class="action" data-action="arrayAppend" data-trigger="click" data-field="` + field.Name + `">` + utils.Icon("plus") + `Add ` + field.Name + `</button></div>`) b.WriteString(`<div class="buttons">`)
b.WriteString(`<button class="action" data-action="arrayAppend" data-trigger="click" data-field="` + field.Name + `">` + utils.Icon("plus") + `Add ` + field.Name + `</button>`)
b.WriteString(`</div>`)
default: default:
panic("No edit form implementation for " + idPrefix + field.Name + " with type " + field.Type.String()) panic("No edit form implementation for " + idPrefix + field.Name + " with type " + field.Type.String())
} }

View File

@ -1,4 +1,5 @@
export * from "./Actions/AnimeList" export * from "./Actions/AnimeList"
export * from "./Actions/Delete"
export * from "./Actions/Diff" export * from "./Actions/Diff"
export * from "./Actions/FollowUser" export * from "./Actions/FollowUser"
export * from "./Actions/Forum" export * from "./Actions/Forum"

17
scripts/Actions/Delete.ts Normal file
View File

@ -0,0 +1,17 @@
import { AnimeNotifier } from "../AnimeNotifier"
// Delete
export function deleteObject(arn: AnimeNotifier, button: HTMLButtonElement) {
let confirmType = button.dataset.confirmType
let returnPath = button.dataset.returnPath
if(!confirm(`Are you sure you want to delete this ${confirmType}?`)) {
return
}
let endpoint = arn.findAPIEndpoint(button)
arn.post(endpoint + "/delete", "")
.then(() => arn.app.load(returnPath))
.catch(err => arn.statusMessage.showError(err))
}