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,
|
||||
} from "@odit/lfk-client-js";
|
||||
import toast from 'svelte-french-toast'
|
||||
import DocumentServer from "./DocumentServer.ts"
|
||||
|
||||
import { init } from "@paralleldrive/cuid2";
|
||||
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 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) {
|
||||
cards_dropdown_open = false;
|
||||
|
||||
@ -41,34 +55,9 @@
|
||||
|
||||
function generateCards(locale) {
|
||||
toast.loading($_("generating-pdf"));
|
||||
fetch(
|
||||
`${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();
|
||||
}
|
||||
})
|
||||
documentServer.generateCards(generate_cards, locale)
|
||||
.then((blob) => {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
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"));
|
||||
download(blob, `${$_("runnercards")}-${locale}-${createId()}.pdf`);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
@ -88,40 +77,15 @@
|
||||
}
|
||||
cards.push(card);
|
||||
}
|
||||
fetch(
|
||||
`${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();
|
||||
}
|
||||
})
|
||||
documentServer.generateCards(cards, locale)
|
||||
.then((blob) => {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
let a = document.createElement("a");
|
||||
a.href = url;
|
||||
let fileName = `${$_("runnercards")}-${locale}-${createId()}.pdf`;
|
||||
if (generate_runners.length == 1) {
|
||||
a.download = `${$_("runnercards")}_${generate_runners[0].firstname}_${
|
||||
fileName = `${$_("runnercards")}_${generate_runners[0].firstname}_${
|
||||
generate_runners[0].lastname
|
||||
}-${locale}-${createId()}.pdf`;
|
||||
} else {
|
||||
a.download = `${$_("runnercards")}-${locale}-${createId()}.pdf`;
|
||||
}
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
toast.dismiss();
|
||||
toast($_("pdf-successfully-generated"));
|
||||
download(blob, fileName);
|
||||
})
|
||||
.catch((err) => {});
|
||||
}
|
||||
@ -144,39 +108,11 @@
|
||||
}
|
||||
cards.push(card);
|
||||
}
|
||||
fetch(
|
||||
`${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();
|
||||
}
|
||||
})
|
||||
documentServer.generateCards(cards, locale)
|
||||
.then((blob) => {
|
||||
count++;
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
let a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${$_("runnercards")}_${
|
||||
download(blob, `${$_("runnercards")}_${
|
||||
t.name
|
||||
}-${locale}-${createId()}.pdf`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
if (count === generate_teams.length) {
|
||||
toast.dismiss();
|
||||
toast.success($_("pdfs-successfully-generated"));
|
||||
}
|
||||
}-${locale}-${createId()}.pdf`)
|
||||
})
|
||||
.catch((err) => {});
|
||||
}
|
||||
@ -205,38 +141,11 @@
|
||||
}
|
||||
cards.push(card);
|
||||
}
|
||||
await fetch(
|
||||
`${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();
|
||||
}
|
||||
})
|
||||
await documentServer.generateCards(cards, locale)
|
||||
.then((blob) => {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
let a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${$_("runnercards")}_${
|
||||
download(blob, `${$_("runnercards")}_${
|
||||
o.name
|
||||
}_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"));
|
||||
}
|
||||
}_direct-${locale}-${createId()}.pdf`)
|
||||
})
|
||||
.catch((err) => {});
|
||||
for (const t of o.teams) {
|
||||
@ -254,41 +163,11 @@
|
||||
}
|
||||
cards.push(card);
|
||||
}
|
||||
await fetch(
|
||||
`${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();
|
||||
}
|
||||
})
|
||||
await documentServer.generateCards(cards, locale)
|
||||
.then((blob) => {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
let a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${$_("runnercards")}_${o.name}_${
|
||||
download(blob, `${$_("runnercards")}_${o.name}_${
|
||||
t.name
|
||||
}-${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"));
|
||||
}
|
||||
}-${locale}-${createId()}.pdf`)
|
||||
})
|
||||
.catch((err) => {});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user