ff-admin/src/components/admin/settings/templateUsage/TemplateUsageListItem.vue

120 lines
5.2 KiB
Vue
Raw Normal View History

2024-12-23 14:00:32 +01:00
<template>
<form ref="form" class="flex flex-col h-fit w-full border border-primary rounded-md" @submit.prevent="updateUsage">
<div class="bg-primary p-2 text-white flex flex-row justify-between items-center">
<p>Templates zu "{{ templateUsage.scope }}" zuweisen</p>
<div class="flex flex-row justify-end w-16">
2024-12-24 13:53:00 +01:00
<button type="button" class="!p-0 !h-fit !w-fit" title="Vorschau erzeugen" @click="previewUsage">
<EyeIcon class="w-5 h-5 p-1 box-content pointer-events-none" />
</button>
2024-12-26 12:04:00 +01:00
<button v-if="status == null && can('create','settings','newsletter_config')" type="submit" class="!p-0 !h-fit !w-fit" title="speichern">
2024-12-23 14:00:32 +01:00
<ArchiveBoxArrowDownIcon class="w-5 h-5 p-1 box-content pointer-events-none" />
</button>
<Spinner v-else-if="status == 'loading'" class="my-auto" />
<SuccessCheckmark v-else-if="status?.status == 'success'" />
<FailureXMark v-else-if="status?.status == 'failed'" />
2024-12-26 12:04:00 +01:00
<button type="button" v-if="can('create','settings','newsletter_config')" class="!p-0 !h-fit !w-fit" title="zurücksetzen" @click="resetForm">
2024-12-23 14:00:32 +01:00
<ArchiveBoxXMarkIcon class="w-5 h-5 p-1 box-content pointer-events-none" />
</button>
</div>
</div>
<div class="flex flex-col p-2 gap-2">
<div class="flex flex-row gap-2 items-center">
<p class="min-w-16">Kopfzeile:</p>
<select ref="header" id="header" :value="templateUsage.header?.id ?? 'def'">
2024-12-26 12:04:00 +01:00
<option value="def">Standard-Vorlage verwenden</option>
2024-12-23 14:00:32 +01:00
<option v-for="template in templates" :key="template.id" :value="template.id">{{ template.template }}</option>
</select>
</div>
<div class="flex flex-row gap-2 items-center">
<p class="min-w-16">Hauptteil:</p>
<select ref="body" id="body" :value="templateUsage.body?.id ?? 'def'">
2024-12-26 12:04:00 +01:00
<option value="def">Standard-Vorlage verwenden</option>
2024-12-23 14:00:32 +01:00
<option v-for="template in templates" :key="template.id" :value="template.id">{{ template.template }}</option>
</select>
</div>
<div class="flex flex-row gap-2 items-center">
<p class="min-w-16">Fußzeile:</p>
<select ref="footer" id="footer" :value="templateUsage.footer?.id ?? 'def'">
2024-12-26 12:04:00 +01:00
<option value="def">Standard-Vorlage verwenden</option>
2024-12-23 14:00:32 +01:00
<option v-for="template in templates" :key="template.id" :value="template.id">{{ template.template }}</option>
</select>
</div>
</div>
</form>
</template>
<script setup lang="ts">
2024-12-24 13:53:00 +01:00
import { defineAsyncComponent, defineComponent, markRaw, type PropType } from "vue";
2024-12-23 14:00:32 +01:00
import { mapState, mapActions } from "pinia";
2024-12-24 13:53:00 +01:00
import { ArchiveBoxArrowDownIcon, ArchiveBoxXMarkIcon, EyeIcon } from "@heroicons/vue/24/outline";
2024-12-26 12:34:36 +01:00
import type { TemplateUsageViewModel } from "@/viewmodels/admin/templateUsage.models";
import { useTemplateStore } from "@/stores/admin/template";
import { useTemplateUsageStore } from "@/stores/admin/templateUsage";
2024-12-23 14:00:32 +01:00
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
2024-12-24 13:53:00 +01:00
import { useModalStore } from "@/stores/modal";
2024-12-26 12:04:00 +01:00
import { useAbilityStore } from "@/stores/ability";
2024-12-23 14:00:32 +01:00
</script>
<script lang="ts">
export default defineComponent({
props: {
templateUsage: { type: Object as PropType<TemplateUsageViewModel>, default: {} },
},
data() {
return {
status: null as null | "loading" | { status: "success" | "failed"; reason?: string },
timeout: undefined as any,
};
},
computed: {
...mapState(useTemplateStore, ["templates"]),
2024-12-26 12:04:00 +01:00
...mapState(useAbilityStore, ["can"]),
2024-12-23 14:00:32 +01:00
},
beforeUnmount() {
try {
clearTimeout(this.timeout);
} catch (error) {}
},
methods: {
2024-12-24 13:53:00 +01:00
...mapActions(useModalStore, ["openModal"]),
2024-12-23 14:00:32 +01:00
...mapActions(useTemplateUsageStore, ["updateTemplateUsage"]),
2024-12-24 13:53:00 +01:00
previewUsage() {
this.openModal(
markRaw(defineAsyncComponent(() => import("@/components/admin/settings/templateUsage/TemplatePreviewModal.vue"))),
this.templateUsage.scope
)
},
2024-12-23 14:00:32 +01:00
updateUsage(e: any) {
const fromData = e.target.elements;
const headerId = fromData.header.value === "def" ? null : fromData.header.value;
const bodyId = fromData.body.value === "def" ? null : fromData.body.value;
const footerId = fromData.footer.value === "def" ? null : fromData.footer.value;
2024-12-24 13:53:00 +01:00
this.status = "loading"
2024-12-23 14:00:32 +01:00
this.updateTemplateUsage({
scope: this.templateUsage.scope,
headerId: headerId,
bodyId: bodyId,
footerId: footerId,
})
.then(() => {
this.status = { status: "success" };
this.timeout = setTimeout(() => {
this.status = null;
}, 2000);
})
.catch(() => {
2024-12-26 12:04:00 +01:00
this.status = { status: "failed" };
2024-12-23 14:00:32 +01:00
});
},
resetForm() {
(this.$refs.header as HTMLSelectElement).value = String(this.templateUsage.header?.id ?? "def");
(this.$refs.body as HTMLSelectElement).value = String(this.templateUsage.body?.id ?? "def");
(this.$refs.footer as HTMLSelectElement).value = String(this.templateUsage.footer?.id ?? "def");
},
},
});
</script>