advent22/ui/src/components/admin/DoorMapEditor.vue

181 lines
4.6 KiB
Vue
Raw Normal View History

2023-01-17 18:25:56 +00:00
<template>
2023-09-14 12:51:30 +00:00
<BulmaDrawer header="Türchen bearbeiten" @open="on_open">
2023-10-27 15:09:44 +00:00
<nav class="level is-mobile mb-0" style="overflow-x: auto">
2023-09-14 13:57:20 +00:00
<BulmaButton
:disabled="current_step === 0"
class="level-item is-link"
@click="current_step--"
icon="fa-solid fa-backward"
/>
2023-09-14 13:54:23 +00:00
2023-09-14 13:57:20 +00:00
<BulmaBreadcrumbs
:steps="steps"
v-model="current_step"
class="level-item mb-0"
/>
2023-09-14 13:54:23 +00:00
2023-09-14 13:57:20 +00:00
<BulmaButton
:disabled="current_step === 2"
class="level-item is-link"
@click="current_step++"
icon="fa-solid fa-forward"
/>
2023-09-14 13:54:23 +00:00
</nav>
2023-01-19 00:22:01 +00:00
2023-09-21 10:03:53 +00:00
<div class="card-content pb-0">
<div v-if="doors.length > 0" class="content">
<p>Für diese Tage ist ein Türchen vorhanden:</p>
<div class="tags">
<span
v-for="(door, index) in doors.toSorted((a, b) => a.day - b.day)"
:key="`door-${index}`"
class="tag is-primary"
>
{{ door.day }}
</span>
</div>
</div>
</div>
2023-09-21 13:29:47 +00:00
<DoorPlacer v-if="current_step === 0" :doors="doors" />
<DoorChooser v-if="current_step === 1" :doors="doors" />
2023-09-10 03:38:24 +00:00
<div v-if="current_step === 2" class="card-content">
2023-09-10 00:54:29 +00:00
<Calendar :doors="doors" />
</div>
2023-09-09 22:18:16 +00:00
2023-09-10 03:38:24 +00:00
<footer class="card-footer is-flex is-justify-content-space-around">
2023-09-10 00:54:29 +00:00
<BulmaButton
2023-09-10 03:38:24 +00:00
class="card-footer-item is-danger"
2023-09-10 00:54:29 +00:00
@click="on_download"
icon="fa-solid fa-cloud-arrow-down"
:busy="loading_doors"
2023-09-10 00:54:29 +00:00
text="Laden"
/>
<BulmaButton
2023-09-10 03:38:24 +00:00
class="card-footer-item is-warning"
2023-09-10 00:54:29 +00:00
@click="on_discard"
icon="fa-solid fa-trash"
text="Löschen"
/>
<BulmaButton
2023-09-10 03:38:24 +00:00
class="card-footer-item is-success"
2023-09-10 00:54:29 +00:00
@click="on_upload"
icon="fa-solid fa-cloud-arrow-up"
:busy="saving_doors"
2023-09-10 00:54:29 +00:00
text="Speichern"
/>
2023-09-10 03:38:24 +00:00
</footer>
2023-09-09 22:18:16 +00:00
</BulmaDrawer>
2023-01-17 18:25:56 +00:00
</template>
<script lang="ts">
2023-09-12 16:55:34 +00:00
import { DoorsSaved } from "@/lib/api";
import { Door } from "@/lib/door";
2023-09-06 16:25:35 +00:00
import { Options, Vue } from "vue-class-component";
2023-01-17 18:25:56 +00:00
2023-09-13 16:08:05 +00:00
import Calendar from "../Calendar.vue";
import BulmaBreadcrumbs, { Step } from "../bulma/Breadcrumbs.vue";
import BulmaButton from "../bulma/Button.vue";
2023-09-14 12:51:30 +00:00
import BulmaDrawer from "../bulma/Drawer.vue";
2023-09-13 16:08:05 +00:00
import DoorChooser from "../editor/DoorChooser.vue";
import DoorPlacer from "../editor/DoorPlacer.vue";
2023-01-24 00:14:50 +00:00
2023-01-17 18:25:56 +00:00
@Options({
components: {
2023-02-02 12:50:12 +00:00
BulmaBreadcrumbs,
2023-09-07 15:43:05 +00:00
BulmaButton,
2023-09-09 22:18:16 +00:00
BulmaDrawer,
2023-01-19 17:59:18 +00:00
DoorPlacer,
2023-01-24 00:14:50 +00:00
DoorChooser,
2023-09-07 00:41:38 +00:00
Calendar,
2023-01-17 18:25:56 +00:00
},
})
2023-01-24 23:19:25 +00:00
export default class extends Vue {
2023-09-06 16:25:35 +00:00
public readonly steps: Step[] = [
2023-01-20 00:42:47 +00:00
{ label: "Platzieren", icon: "fa-solid fa-crosshairs" },
{ label: "Ordnen", icon: "fa-solid fa-list-ol" },
2023-09-09 23:53:49 +00:00
{ label: "Vorschau", icon: "fa-solid fa-magnifying-glass" },
2023-01-20 00:42:47 +00:00
];
2023-09-06 16:25:35 +00:00
public current_step = 0;
public doors: Door[] = [];
2023-09-07 03:30:21 +00:00
public loading_doors = false;
public saving_doors = false;
2023-09-07 16:44:44 +00:00
2023-09-14 12:51:30 +00:00
private load_doors(): Promise<void> {
return new Promise<void>((resolve, reject) => {
2023-09-13 15:24:25 +00:00
this.$advent22
.api_get<DoorsSaved>("admin/doors")
.then((data) => {
this.doors.length = 0;
for (const value of data) {
this.doors.push(Door.load(value));
}
2023-09-14 12:51:30 +00:00
resolve();
2023-09-13 15:24:25 +00:00
})
2023-09-13 15:58:15 +00:00
.catch((error) => {
2023-11-02 00:41:42 +00:00
this.$advent22.alert_user_error(error);
2023-09-13 15:24:25 +00:00
reject();
});
});
2023-09-07 16:44:44 +00:00
}
2023-09-09 22:18:16 +00:00
private save_doors(): Promise<void> {
2023-09-13 15:24:25 +00:00
return new Promise<void>((resolve, reject) => {
const data: DoorsSaved = [];
for (const door of this.doors) {
data.push(door.save());
}
this.$advent22
.api_put("admin/doors", data)
.then(resolve)
2023-09-13 15:58:15 +00:00
.catch((error) => {
2023-11-02 00:41:42 +00:00
this.$advent22.alert_user_error(error);
2023-09-13 15:24:25 +00:00
reject();
});
});
2023-09-07 03:30:21 +00:00
}
2023-09-07 20:54:11 +00:00
2023-09-14 12:51:30 +00:00
public on_open(ready: () => void, fail: () => void): void {
this.load_doors().then(ready).catch(fail);
2023-09-09 22:18:16 +00:00
}
public on_download() {
if (confirm("Aktuelle Änderungen verwerfen und Status vom Server laden?")) {
this.loading_doors = true;
2023-09-13 15:24:25 +00:00
this.load_doors()
2023-09-13 19:20:11 +00:00
.then(() => alert("Erfolgreich!"))
2023-09-13 15:24:25 +00:00
.catch(() => {})
.finally(() => (this.loading_doors = false));
2023-09-09 22:18:16 +00:00
}
}
public on_discard() {
if (confirm("Alle Türchen löschen? (nur lokal)")) {
// empty `doors` array
this.doors.length = 0;
}
}
public on_upload() {
if (confirm("Aktuelle Änderungen an den Server schicken?")) {
this.saving_doors = true;
2023-09-13 15:24:25 +00:00
this.save_doors()
.then(() => {
this.load_doors()
2023-09-13 19:20:11 +00:00
.then(() => alert("Erfolgreich!"))
2023-09-13 15:24:25 +00:00
.catch(() => {})
.finally(() => (this.saving_doors = false));
})
.catch(() => (this.saving_doors = false));
2023-09-09 22:18:16 +00:00
}
}
2023-09-07 20:54:11 +00:00
}
2023-09-09 22:18:16 +00:00
</script>