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

@ -0,0 +1,55 @@
import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type { AxiosResponse } from "axios";
import type { CreateTemplateViewModel, UpdateTemplateViewModel } from "../../viewmodels/admin/template.models";
export const useTemplateStore = defineStore("template", {
state: () => {
return {
templates: [] as Array<any>,
loading: "loading" as "loading" | "fetched" | "failed",
};
},
actions: {
fetchTemplates() {
this.loading = "loading";
http
.get("/admin/template")
.then((result) => {
this.templates = result.data;
this.loading = "fetched";
})
.catch((err) => {
this.loading = "failed";
});
},
fetchTemplateById(id: number): Promise<AxiosResponse<any, any>> {
return http.get(`/admin/template/${id}`);
},
async createTemplate(template: CreateTemplateViewModel): Promise<AxiosResponse<any, any>> {
const result = await http.post(`/admin/template`, {
template: template.template,
description: template.description,
});
this.fetchTemplates();
return result;
},
async updateActiveTemplate(template: UpdateTemplateViewModel): Promise<AxiosResponse<any, any>> {
const result = await http.patch(`/admin/template/${template.id}`, {
template: template.template,
description: template.description,
design: template.design,
headerHTML: template.headerHTML,
bodyHTML: template.bodyHTML,
footerHTML: template.footerHTML,
});
this.fetchTemplates();
return result;
},
async deleteTemplate(template: number): Promise<AxiosResponse<any, any>> {
const result = await http.delete(`/admin/template/${template}`);
this.fetchTemplates();
return result;
},
},
});