template storing

This commit is contained in:
Julian Krauser 2024-12-22 10:29:31 +01:00
parent 467dfd8c1b
commit 78a9d206c3
11 changed files with 581 additions and 63 deletions

View file

@ -3,22 +3,20 @@
<template #topBar>
<div class="flex flex-row items-center justify-between pt-5 pb-3 px-7">
<h1 class="font-bold text-xl h-8">Templates</h1>
<RouterLink :to="{ name: 'admin-settings-template-info' }">
<InformationCircleIcon class="text-gray-500 h-5 w-5" />
</RouterLink>
</div>
</template>
<template #diffMain>
<div class="flex flex-col gap-4 grow pl-7">
<div class="flex flex-col gap-2 grow pr-7">
<EmailEditor
ref="emailEditor"
minHeight="100%"
:options="options"
@load="editorLoaded"
@ready="editorReady"
/>
<div class="flex flex-col gap-2 grow overflow-y-scroll pr-7">
<TemplateListItem v-for="template in templates" :key="template.id" :template="template" />
</div>
<div class="flex flex-row gap-4">
<button primary class="!w-fit" @click="saveDesign">Save Design</button>
<button primary-outline class="!w-fit" @click="loadBlank">leeren</button>
<button v-if="can('create', 'settings', 'template')" primary class="!w-fit" @click="openCreateModal">
Template erstellen
</button>
</div>
</div>
</template>
@ -26,61 +24,33 @@
</template>
<script setup lang="ts">
import { defineComponent } from "vue";
import { defineAsyncComponent, defineComponent, markRaw } from "vue";
import { mapState, mapActions } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import { EmailEditor } from "vue-email-editor";
import { options } from "@/helpers/unlayerEditor";
import TemplateListItem from "@/components/admin/settings/template/TemplateListItem.vue";
import { useTemplateStore } from "@/stores/admin/template";
import { useAbilityStore } from "@/stores/ability";
import { useModalStore } from "@/stores/modal";
import { RouterLink } from "vue-router";
import { InformationCircleIcon } from "@heroicons/vue/24/outline";
</script>
<script lang="ts">
export default defineComponent({
computed: {
...mapState(useTemplateStore, ["templates"]),
...mapState(useAbilityStore, ["can"]),
},
mounted() {
this.fetchTemplates();
},
methods: {
// called when the editor is created
editorLoaded() {
console.log("editorLoaded");
// Pass the template JSON here
// this.$refs.emailEditor.editor.loadDesign({});
},
// called when the editor has finished loading
editorReady() {
console.log("editorReady");
(this.$refs.emailEditor as typeof EmailEditor).editor.setBodyValues({
contentWidth: "100%",
backgroundColor: "#ffffff",
linkStyle: {
linkColor: "#990b00",
linkHoverColor: "#bb1e10",
linkUnderline: false,
linkHoverUnderline: false,
},
});
},
loadBlank() {
(this.$refs.emailEditor as typeof EmailEditor).editor.loadBlank();
},
loadDesign() {
(this.$refs.emailEditor as typeof EmailEditor).editor.loadDesign((design: any) => {
console.log("saveDesign", design);
});
},
saveDesign() {
(this.$refs.emailEditor as typeof EmailEditor).editor.saveDesign((design: any) => {
console.log("saveDesign", design);
});
},
exportHtml() {
(this.$refs.emailEditor as typeof EmailEditor).editor.exportHtml((data: any) => {}, {
minify: true,
onlyHeader: true,
});
(this.$refs.emailEditor as typeof EmailEditor).editor.exportHtml((data: any) => {}, {
minify: true,
});
(this.$refs.emailEditor as typeof EmailEditor).editor.exportHtml((data: any) => {}, {
minify: true,
onlyFooter: true,
});
...mapActions(useTemplateStore, ["fetchTemplates"]),
...mapActions(useModalStore, ["openModal"]),
openCreateModal() {
this.openModal(
markRaw(defineAsyncComponent(() => import("@/components/admin/settings/template/CreateTemplateModal.vue")))
);
},
},
});

View file

@ -0,0 +1,158 @@
<template>
<MainTemplate>
<template #headerInsert>
<RouterLink to="../" class="text-primary">zurück zur Liste (abbrechen)</RouterLink>
</template>
<template #topBar>
<div class="flex flex-row items-center justify-between pt-5 pb-3 px-7">
<h1 class="font-bold text-xl h-8">Template {{ origin?.template }} - Daten bearbeiten</h1>
</div>
</template>
<template #main>
<Spinner v-if="loading == 'loading'" class="mx-auto" />
<p v-else-if="loading == 'failed'">laden fehlgeschlagen</p>
<form
v-else-if="template != null"
class="flex flex-col gap-4 py-2 w-full h-full mx-auto"
@submit.prevent="triggerUpdate"
>
<div class="flex flex-col xl:flex-row gap-4">
<div class="w-full">
<label for="template">Bezeichnung</label>
<input type="text" id="template" required v-model="template.template" />
</div>
<div class="w-full">
<label for="description">Beschreibung (optional)</label>
<input type="text" id="description" v-model="template.description" />
</div>
</div>
<div class="flex flex-col w-full grow max-xl:hidden">
<EmailEditor
ref="emailEditor"
minHeight="100%"
:options="options"
@ready="editorReady"
@load="loadDesign()"
/>
</div>
<div class="px-7 xl:hidden">
<p>
Der externe Editor ist nicht auf kleine Auflösungen optimiert. Wechseln Sie auf ein Desktop-Gerät, einen
größeren Bildschirm oder ändern Sie die Skalierung dieser Seite.
</p>
</div>
<div class="flex flex-row justify-end gap-2">
<button primary-outline type="reset" class="!w-fit" :disabled="status == 'loading'" @click="resetForm">
verwerfen
</button>
<button primary type="submit" class="!w-fit" :disabled="status == 'loading'">speichern</button>
<Spinner v-if="status == 'loading'" class="my-auto" />
<SuccessCheckmark v-else-if="status?.status == 'success'" />
<FailureXMark v-else-if="status?.status == 'failed'" />
</div>
</form>
</template>
</MainTemplate>
</template>
<script setup lang="ts">
import { defineComponent } from "vue";
import { mapState, mapActions } from "pinia";
import MainTemplate from "@/templates/Main.vue";
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
import { RouterLink } from "vue-router";
import { EmailEditor } from "vue-email-editor";
import { configureEditor, exportEditor, loadEditor, options } from "@/helpers/unlayerEditor";
import type { TemplateViewModel, UpdateTemplateViewModel } from "../../../../viewmodels/admin/template.models";
import { useTemplateStore } from "../../../../stores/admin/template";
import cloneDeep from "lodash.clonedeep";
import isEqual from "lodash.isequal";
</script>
<script lang="ts">
export default defineComponent({
props: {
id: String,
},
data() {
return {
loading: "loading" as "loading" | "fetched" | "failed",
status: null as null | "loading" | { status: "success" | "failed"; reason?: string },
origin: null as null | TemplateViewModel,
template: null as null | TemplateViewModel,
timeout: null as any,
};
},
computed: {
canSaveOrReset(): boolean {
return isEqual(this.origin, this.template);
},
},
mounted() {
this.fetchItem();
},
beforeUnmount() {
try {
clearTimeout(this.timeout);
} catch (error) {}
},
methods: {
...mapActions(useTemplateStore, ["fetchTemplateById", "updateActiveTemplate"]),
editorReady() {
configureEditor(this.$refs.emailEditor as typeof EmailEditor);
},
loadBlank() {
loadEditor(this.$refs.emailEditor as typeof EmailEditor);
},
loadDesign() {
loadEditor(this.$refs.emailEditor as typeof EmailEditor, this.origin?.design);
},
resetForm() {
this.template = cloneDeep(this.origin);
this.loadDesign();
},
fetchItem() {
this.fetchTemplateById(parseInt(this.id ?? ""))
.then((result) => {
this.template = result.data;
this.origin = cloneDeep(result.data);
this.loading = "fetched";
})
.catch((err) => {
console.log(err);
this.loading = "failed";
});
},
triggerUpdate(e: any) {
if (this.template == null) return;
let { design, headerHTML, bodyHTML, footerHTML } = exportEditor(this.$refs.emailEditor as typeof EmailEditor);
let formData = e.target.elements;
let updateTemplate: UpdateTemplateViewModel = {
id: this.template.id,
template: formData.template.value,
description: formData.description.value,
design,
headerHTML,
bodyHTML,
footerHTML,
};
this.status = "loading";
this.updateActiveTemplate(updateTemplate)
.then(() => {
this.fetchItem();
this.status = { status: "success" };
})
.catch((err) => {
this.status = { status: "failed" };
})
.finally(() => {
this.timeout = setTimeout(() => {
this.status = null;
}, 2000);
});
},
},
});
</script>

View file

@ -0,0 +1,24 @@
<template>
<MainTemplate>
<template #topBar>
<div class="flex flex-row items-center justify-between pt-5 pb-3 px-7">
<h1 class="font-bold text-xl h-8">Templates - Verwendungsinformation</h1>
</div>
</template>
<template #main>
<p>
Mit diesem Editor können Vorlagen erstellt werden, welche später dafür genutzt werden können, um pdfs zu drucken
oder Mails zu versenden.
</p>
<p>
Es können Platzhalter in das Design integriert werden. Dort werden dann später Werte automatisch eingetragen.
Diese Werte stammen zum Beispiel aus dem Kalender oder aus Abfragen, welche mit dem Query-Builder erstellt
wurden.
</p>
</template>
</MainTemplate>
</template>
<script setup lang="ts">
import MainTemplate from "@/templates/Main.vue";
</script>