ff-admin/src/stores/admin/club/newsletter/newsletterPrintout.ts

163 lines
6 KiB
TypeScript
Raw Normal View History

2024-12-28 18:03:20 +01:00
import { defineStore } from "pinia";
import { http, newEventSource, streamingFetch } from "@/serverCom";
2024-12-28 18:03:20 +01:00
import { useNewsletterStore } from "./newsletter";
import type { AxiosResponse } from "axios";
2024-12-31 14:23:42 +01:00
import type { EventSourcePolyfill } from "event-source-polyfill";
import { useNotificationStore, type NotificationType } from "../../../notification";
2024-12-28 18:03:20 +01:00
export const useNewsletterPrintoutStore = defineStore("newsletterPrintout", {
state: () => {
return {
printout: [] as Array<string>,
loading: "loading" as "loading" | "fetched" | "failed",
printing: undefined as undefined | "loading" | "success" | "failed",
sending: undefined as undefined | "loading" | "success" | "failed",
sendingPreview: undefined as undefined | "loading" | "success" | "failed",
2025-01-12 14:05:08 +01:00
pdfSourceMessages: [] as Array<{ kind: string; factor: string; [key: string]: string }>,
mailSourceMessages: [] as Array<{ kind: string; factor: string; [key: string]: string }>,
pdfPrintingAbort: undefined as undefined | AbortController,
mailSendingAbort: undefined as undefined | AbortController,
2024-12-28 18:03:20 +01:00
};
},
actions: {
fetchNewsletterPrintout() {
const newsletterId = useNewsletterStore().activeNewsletter;
this.loading = "loading";
http
.get(`/admin/newsletter/${newsletterId}/printouts`)
.then((result) => {
this.printout = result.data;
this.loading = "fetched";
})
.catch((err) => {
this.loading = "failed";
});
},
fetchNewsletterPrintoutById(printout: string): Promise<AxiosResponse<any, any>> {
const newsletterId = useNewsletterStore().activeNewsletter;
return http.get(`/admin/newsletter/${newsletterId}/printout/${printout}`, {
responseType: "blob",
});
},
fetchNewsletterPrintoutPreview(): Promise<AxiosResponse<any, any>> {
const newsletterId = useNewsletterStore().activeNewsletter;
return http.get(`/admin/newsletter/${newsletterId}/printoutpreview`, {
responseType: "blob",
});
},
2025-04-10 08:29:13 +02:00
fetchNewsletterPrintReceivers(): Promise<AxiosResponse<any, any>> {
const newsletterId = useNewsletterStore().activeNewsletter;
return http.get(`/admin/newsletter/${newsletterId}/printrecipients`);
},
fetchNewsletterMailReceivers(): Promise<AxiosResponse<any, any>> {
const newsletterId = useNewsletterStore().activeNewsletter;
return http.get(`/admin/newsletter/${newsletterId}/mailrecipients`);
},
2024-12-28 18:03:20 +01:00
createNewsletterMailPreview() {
this.sendingPreview = "loading";
const newsletterId = useNewsletterStore().activeNewsletter;
if (newsletterId == null) return;
return http
.post(`/admin/newsletter/${newsletterId}/mailpreview`)
.then((res) => {
this.sendingPreview = "success";
})
.catch((err) => {
this.sendingPreview = "failed";
})
.finally(() => {
setTimeout(() => {
this.sendingPreview = undefined;
}, 1500);
});
},
createNewsletterPrintout() {
this.subscribePdfPrintingProgress();
2024-12-28 18:03:20 +01:00
this.printing = "loading";
const newsletterId = useNewsletterStore().activeNewsletter;
if (newsletterId == null) return;
return http
.post(`/admin/newsletter/${newsletterId}/printout`)
.then((res) => {
this.fetchNewsletterPrintout();
this.printing = "success";
})
.catch((err) => {
this.printing = "failed";
})
.finally(() => {
setTimeout(() => {
this.printing = undefined;
this.pdfPrintingAbort?.abort();
2024-12-28 18:03:20 +01:00
}, 1500);
});
},
createNewsletterSend() {
this.subscribeMailSendingProgress();
2024-12-28 18:03:20 +01:00
this.sending = "loading";
const newsletterId = useNewsletterStore().activeNewsletter;
if (newsletterId == null) return;
return http
.post(`/admin/newsletter/${newsletterId}/send`)
.then((res) => {
this.sending = "success";
})
.catch((err) => {
this.sending = "failed";
})
.finally(() => {
setTimeout(() => {
this.sending = undefined;
this.mailSendingAbort?.abort();
2024-12-28 18:03:20 +01:00
}, 1500);
});
},
async subscribePdfPrintingProgress() {
this.pdfSourceMessages = [];
const newsletterId = useNewsletterStore().activeNewsletter;
const notificationStore = useNotificationStore();
this.pdfPrintingAbort = new AbortController();
for await (let chunk of streamingFetch(
`/admin/newsletter/${newsletterId}/printoutprogress`,
this.pdfPrintingAbort
)) {
chunk.split("//").forEach((r) => {
if (r.trim() != "") {
let data = JSON.parse(r);
this.pdfSourceMessages.push(data);
let type: NotificationType = "info";
2025-01-12 14:05:08 +01:00
let timeout = undefined;
if (data.factor == "failed") {
type = "error";
timeout = 0;
}
notificationStore.push(`Druck: ${data.iteration}/${data.total}`, `${data.msg}`, type, timeout);
}
});
this.fetchNewsletterPrintout();
}
2024-12-31 14:23:42 +01:00
},
async subscribeMailSendingProgress() {
this.mailSourceMessages = [];
const newsletterId = useNewsletterStore().activeNewsletter;
const notificationStore = useNotificationStore();
this.mailSendingAbort = new AbortController();
for await (let chunk of streamingFetch(`/admin/newsletter/${newsletterId}/sendprogress`, this.mailSendingAbort)) {
chunk.split("//").forEach((r) => {
if (r.trim() != "") {
let data = JSON.parse(r);
this.mailSourceMessages.push(data);
let type: NotificationType = "info";
2025-01-12 14:05:08 +01:00
let timeout = undefined;
if (data.factor == "failed") {
type = "error";
timeout = 0;
}
notificationStore.push(`Mailversand: ${data.iteration}/${data.total}`, `${data.msg}`, type, timeout);
}
});
}
2024-12-31 14:23:42 +01:00
},
2024-12-28 18:03:20 +01:00
},
});