refactor(pdfgeneration): Switch cards over to new service
This commit is contained in:
parent
04494d2a2a
commit
e23098410c
49
src/components/pdf_generation/DocumentServer.ts
Normal file
49
src/components/pdf_generation/DocumentServer.ts
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
class DocumentServer {
|
||||||
|
baseUrl: string;
|
||||||
|
apiKey: string;
|
||||||
|
|
||||||
|
constructor(baseUrl: string, apiKey: string){
|
||||||
|
this.baseUrl = baseUrl;
|
||||||
|
this.apiKey = apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
async generateCards(cards: any[], locale: string, filename: string) {
|
||||||
|
const generateCards = new Array<any>();
|
||||||
|
|
||||||
|
for (let i = 0; i < cards.length; i++) {
|
||||||
|
const card = {
|
||||||
|
id: cards[i].id,
|
||||||
|
enabled: cards[i].enabled,
|
||||||
|
code: cards[i].code,
|
||||||
|
runner: {
|
||||||
|
id: cards[i].runner.id,
|
||||||
|
first_name: cards[i].runner.firstname,
|
||||||
|
middle_name: cards[i].runner.middlename,
|
||||||
|
last_name: cards[i].runner.lastname,
|
||||||
|
group: {
|
||||||
|
id: cards[i].runner.group.id,
|
||||||
|
name: cards[i].runner.group.name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
generateCards.push(card)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(`${this.baseUrl}/v1/pdfs/cards?key=${this.apiKey}`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
locale,
|
||||||
|
cards: generateCards,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const blob = await response.blob();
|
||||||
|
return blob;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default DocumentServer;
|
@ -6,9 +6,11 @@
|
|||||||
RunnerTeamService,
|
RunnerTeamService,
|
||||||
} from "@odit/lfk-client-js";
|
} from "@odit/lfk-client-js";
|
||||||
import toast from 'svelte-french-toast'
|
import toast from 'svelte-french-toast'
|
||||||
|
import DocumentServer from "./DocumentServer.ts"
|
||||||
|
|
||||||
import { init } from "@paralleldrive/cuid2";
|
import { init } from "@paralleldrive/cuid2";
|
||||||
const createId = init({ length: 10, fingerprint: "lfk-frontend" });
|
const createId = init({ length: 10, fingerprint: "lfk-frontend" });
|
||||||
|
const documentServer = new DocumentServer(config.baseurl_documentserver,config.documentserver_key);
|
||||||
|
|
||||||
export let cards_show = false;
|
export let cards_show = false;
|
||||||
export let generate_cards = [];
|
export let generate_cards = [];
|
||||||
@ -25,6 +27,18 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function download (blob, fileName){
|
||||||
|
const url = window.URL.createObjectURL(blob);
|
||||||
|
let a = document.createElement("a");
|
||||||
|
a.href = url;
|
||||||
|
a.download = fileName;
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
a.remove();
|
||||||
|
toast.dismiss();
|
||||||
|
toast($_("pdf-successfully-generated"));
|
||||||
|
}
|
||||||
|
|
||||||
function generateRunnerCards(locale) {
|
function generateRunnerCards(locale) {
|
||||||
cards_dropdown_open = false;
|
cards_dropdown_open = false;
|
||||||
|
|
||||||
@ -41,34 +55,9 @@
|
|||||||
|
|
||||||
function generateCards(locale) {
|
function generateCards(locale) {
|
||||||
toast.loading($_("generating-pdf"));
|
toast.loading($_("generating-pdf"));
|
||||||
fetch(
|
documentServer.generateCards(generate_cards, locale)
|
||||||
`${config.baseurl_documentserver}/cards?locale=${locale}&download=true&key=${config.documentserver_key}`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(generate_cards),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.then((response) => {
|
|
||||||
if (response.status != "200") {
|
|
||||||
toast.dismiss();
|
|
||||||
toast.error($_("pdf-generation-failed"));
|
|
||||||
} else {
|
|
||||||
return response.blob();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then((blob) => {
|
.then((blob) => {
|
||||||
const url = window.URL.createObjectURL(blob);
|
download(blob, `${$_("runnercards")}-${locale}-${createId()}.pdf`);
|
||||||
let a = document.createElement("a");
|
|
||||||
a.href = url;
|
|
||||||
a.download = `${$_("runnercards")}-${locale}-${createId()}.pdf`;
|
|
||||||
document.body.appendChild(a);
|
|
||||||
a.click();
|
|
||||||
a.remove();
|
|
||||||
toast.dismiss();
|
|
||||||
toast($_("pdf-successfully-generated"));
|
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
@ -88,40 +77,15 @@
|
|||||||
}
|
}
|
||||||
cards.push(card);
|
cards.push(card);
|
||||||
}
|
}
|
||||||
fetch(
|
documentServer.generateCards(cards, locale)
|
||||||
`${config.baseurl_documentserver}/cards?locale=${locale}&download=true&key=${config.documentserver_key}`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(cards),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.then((response) => {
|
|
||||||
if (response.status != "200") {
|
|
||||||
toast.dismiss();
|
|
||||||
toast.error($_("pdf-generation-failed"));
|
|
||||||
} else {
|
|
||||||
return response.blob();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then((blob) => {
|
.then((blob) => {
|
||||||
const url = window.URL.createObjectURL(blob);
|
let fileName = `${$_("runnercards")}-${locale}-${createId()}.pdf`;
|
||||||
let a = document.createElement("a");
|
|
||||||
a.href = url;
|
|
||||||
if (generate_runners.length == 1) {
|
if (generate_runners.length == 1) {
|
||||||
a.download = `${$_("runnercards")}_${generate_runners[0].firstname}_${
|
fileName = `${$_("runnercards")}_${generate_runners[0].firstname}_${
|
||||||
generate_runners[0].lastname
|
generate_runners[0].lastname
|
||||||
}-${locale}-${createId()}.pdf`;
|
}-${locale}-${createId()}.pdf`;
|
||||||
} else {
|
|
||||||
a.download = `${$_("runnercards")}-${locale}-${createId()}.pdf`;
|
|
||||||
}
|
}
|
||||||
document.body.appendChild(a);
|
download(blob, fileName);
|
||||||
a.click();
|
|
||||||
a.remove();
|
|
||||||
toast.dismiss();
|
|
||||||
toast($_("pdf-successfully-generated"));
|
|
||||||
})
|
})
|
||||||
.catch((err) => {});
|
.catch((err) => {});
|
||||||
}
|
}
|
||||||
@ -144,39 +108,11 @@
|
|||||||
}
|
}
|
||||||
cards.push(card);
|
cards.push(card);
|
||||||
}
|
}
|
||||||
fetch(
|
documentServer.generateCards(cards, locale)
|
||||||
`${config.baseurl_documentserver}/cards?locale=${locale}&download=true&key=${config.documentserver_key}`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(cards),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.then((response) => {
|
|
||||||
if (response.status != "200") {
|
|
||||||
toast.dismiss();
|
|
||||||
toast.error($_("pdf-generation-failed"));
|
|
||||||
} else {
|
|
||||||
return response.blob();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then((blob) => {
|
.then((blob) => {
|
||||||
count++;
|
download(blob, `${$_("runnercards")}_${
|
||||||
const url = window.URL.createObjectURL(blob);
|
|
||||||
let a = document.createElement("a");
|
|
||||||
a.href = url;
|
|
||||||
a.download = `${$_("runnercards")}_${
|
|
||||||
t.name
|
t.name
|
||||||
}-${locale}-${createId()}.pdf`;
|
}-${locale}-${createId()}.pdf`)
|
||||||
document.body.appendChild(a);
|
|
||||||
a.click();
|
|
||||||
a.remove();
|
|
||||||
if (count === generate_teams.length) {
|
|
||||||
toast.dismiss();
|
|
||||||
toast.success($_("pdfs-successfully-generated"));
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch((err) => {});
|
.catch((err) => {});
|
||||||
}
|
}
|
||||||
@ -205,38 +141,11 @@
|
|||||||
}
|
}
|
||||||
cards.push(card);
|
cards.push(card);
|
||||||
}
|
}
|
||||||
await fetch(
|
await documentServer.generateCards(cards, locale)
|
||||||
`${config.baseurl_documentserver}/cards?locale=${locale}&download=true&key=${config.documentserver_key}`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(cards),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.then((response) => {
|
|
||||||
if (response.status != "200") {
|
|
||||||
toast.dismiss();
|
|
||||||
toast.error($_("pdf-generation-failed"));
|
|
||||||
} else {
|
|
||||||
return response.blob();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then((blob) => {
|
.then((blob) => {
|
||||||
const url = window.URL.createObjectURL(blob);
|
download(blob, `${$_("runnercards")}_${
|
||||||
let a = document.createElement("a");
|
|
||||||
a.href = url;
|
|
||||||
a.download = `${$_("runnercards")}_${
|
|
||||||
o.name
|
o.name
|
||||||
}_direct-${locale}-${createId()}.pdf`;
|
}_direct-${locale}-${createId()}.pdf`)
|
||||||
document.body.appendChild(a);
|
|
||||||
a.click();
|
|
||||||
a.remove();
|
|
||||||
if (count === o.teams.length && count_orgs === generate_orgs.length) {
|
|
||||||
toast.dismiss();
|
|
||||||
toast.success($_("pdfs-successfully-generated"));
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch((err) => {});
|
.catch((err) => {});
|
||||||
for (const t of o.teams) {
|
for (const t of o.teams) {
|
||||||
@ -254,41 +163,11 @@
|
|||||||
}
|
}
|
||||||
cards.push(card);
|
cards.push(card);
|
||||||
}
|
}
|
||||||
await fetch(
|
await documentServer.generateCards(cards, locale)
|
||||||
`${config.baseurl_documentserver}/cards?locale=${locale}&download=true&key=${config.documentserver_key}`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(cards),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.then((response) => {
|
|
||||||
if (response.status != "200") {
|
|
||||||
toast.dismiss();
|
|
||||||
toast.error($_("pdf-generation-failed"));
|
|
||||||
} else {
|
|
||||||
return response.blob();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then((blob) => {
|
.then((blob) => {
|
||||||
const url = window.URL.createObjectURL(blob);
|
download(blob, `${$_("runnercards")}_${o.name}_${
|
||||||
let a = document.createElement("a");
|
|
||||||
a.href = url;
|
|
||||||
a.download = `${$_("runnercards")}_${o.name}_${
|
|
||||||
t.name
|
t.name
|
||||||
}-${locale}-${createId()}.pdf`;
|
}-${locale}-${createId()}.pdf`)
|
||||||
document.body.appendChild(a);
|
|
||||||
a.click();
|
|
||||||
a.remove();
|
|
||||||
if (
|
|
||||||
count === o.teams.length &&
|
|
||||||
count_orgs === generate_orgs.length
|
|
||||||
) {
|
|
||||||
toast.dismiss();
|
|
||||||
toast($_("pdfs-successfully-generated"));
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch((err) => {});
|
.catch((err) => {});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user