Template Duplication

This commit is contained in:
Julian Krauser 2024-12-24 10:24:35 +01:00
parent b3125a26da
commit faa691b834
2 changed files with 45 additions and 4 deletions

View file

@ -2,13 +2,19 @@
<div class="flex flex-col h-fit w-full border border-primary rounded-md"> <div class="flex flex-col h-fit w-full border border-primary rounded-md">
<div class="bg-primary p-2 text-white flex flex-row justify-between items-center"> <div class="bg-primary p-2 text-white flex flex-row justify-between items-center">
<p>{{ template.template }}</p> <p>{{ template.template }}</p>
<div class="flex flex-row"> <div class="flex flex-row justify-end w-24">
<RouterLink <RouterLink
v-if="can('update', 'settings', 'template')" v-if="can('update', 'settings', 'template')"
:to="{ name: 'admin-settings-template-edit', params: { id: template.id } }" :to="{ name: 'admin-settings-template-edit', params: { id: template.id } }"
> >
<PencilIcon class="w-5 h-5 p-1 box-content cursor-pointer" /> <PencilIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
</RouterLink> </RouterLink>
<button v-if="status == null" class="!p-0 !h-fit !w-fit" title="duplizieren" @click="cloneElement">
<DocumentDuplicateIcon 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'" />
<div v-if="can('delete', 'settings', 'template')" @click="openDeleteModal"> <div v-if="can('delete', 'settings', 'template')" @click="openDeleteModal">
<TrashIcon class="w-5 h-5 p-1 box-content cursor-pointer" /> <TrashIcon class="w-5 h-5 p-1 box-content cursor-pointer" />
</div> </div>
@ -26,11 +32,14 @@
<script setup lang="ts"> <script setup lang="ts">
import { defineComponent, defineAsyncComponent, markRaw, type PropType } from "vue"; import { defineComponent, defineAsyncComponent, markRaw, type PropType } from "vue";
import { mapState, mapActions } from "pinia"; import { mapState, mapActions } from "pinia";
import { PencilIcon, TrashIcon } from "@heroicons/vue/24/outline"; import { PencilIcon, TrashIcon, DocumentDuplicateIcon } from "@heroicons/vue/24/outline";
import { useAbilityStore } from "@/stores/ability"; import { useAbilityStore } from "@/stores/ability";
import { useModalStore } from "@/stores/modal"; import { useModalStore } from "@/stores/modal";
import type { QualificationViewModel } from "@/viewmodels/admin/qualification.models";
import type { TemplateViewModel } from "../../../../viewmodels/admin/template.models"; import type { TemplateViewModel } from "../../../../viewmodels/admin/template.models";
import { useTemplateStore } from "@/stores/admin/template";
import Spinner from "@/components/Spinner.vue";
import SuccessCheckmark from "@/components/SuccessCheckmark.vue";
import FailureXMark from "@/components/FailureXMark.vue";
</script> </script>
<script lang="ts"> <script lang="ts">
@ -38,17 +47,42 @@ export default defineComponent({
props: { props: {
template: { type: Object as PropType<TemplateViewModel>, default: {} }, template: { type: Object as PropType<TemplateViewModel>, default: {} },
}, },
data() {
return {
status: null as null | "loading" | { status: "success" | "failed"; reason?: string },
timeout: undefined as any,
};
},
computed: { computed: {
...mapState(useAbilityStore, ["can"]), ...mapState(useAbilityStore, ["can"]),
}, },
beforeUnmount() {
try {
clearTimeout(this.timeout);
} catch (error) {}
},
methods: { methods: {
...mapActions(useModalStore, ["openModal"]), ...mapActions(useModalStore, ["openModal"]),
...mapActions(useTemplateStore,["cloneTemplate"]),
openDeleteModal() { openDeleteModal() {
this.openModal( this.openModal(
markRaw(defineAsyncComponent(() => import("@/components/admin/settings/template/DeleteTemplateModal.vue"))), markRaw(defineAsyncComponent(() => import("@/components/admin/settings/template/DeleteTemplateModal.vue"))),
this.template.id this.template.id
); );
}, },
cloneElement(){
this.cloneTemplate(this.template.id)
.then((res) => {
this.status = { status: "success" };
this.timeout = setTimeout(() => {
this.status = null;
this.$router.push({ name: "admin-settings-template-edit", params: { id: res.data } });
}, 2000);
})
.catch(() => {
this.status = { status: "failed" };
});
}
}, },
}); });
</script> </script>

View file

@ -48,6 +48,13 @@ export const useTemplateStore = defineStore("template", {
this.fetchTemplates(); this.fetchTemplates();
return result; return result;
}, },
async cloneTemplate(cloneId: number): Promise<AxiosResponse<any, any>> {
const result = await http.post(`/admin/template/clone`, {
cloneId: cloneId,
});
this.fetchTemplates();
return result;
},
async deleteTemplate(template: number): Promise<AxiosResponse<any, any>> { async deleteTemplate(template: number): Promise<AxiosResponse<any, any>> {
const result = await http.delete(`/admin/template/${template}`); const result = await http.delete(`/admin/template/${template}`);
this.fetchTemplates(); this.fetchTemplates();