-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
85 lines (75 loc) · 2.72 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Generate Quote</title>
<script src="https://unpkg.com/@jsreport/browser-client/dist/jsreport.umd.js"></script>
<script src="https://docs.getgrist.com/grist-plugin-api.js"></script>
</head>
<body>
<div id="app">
<button id="generate-quote-btn" style="height: 30px; background-color: orange;color: black; font-size: 18px;">Generate Quote</button>
<div id="quotename" style="font-size: 12px;"></div>
<p>Requires Issued Date and Validity to be set!!</p>
</div>
<script>
let baseURL;
let template;
let data;
let authUser;
let authPassword;
let fileName;
grist.ready({
columns: ["QuoteName", "Template", "QuoteID"],
requiredAccess: "read table",
});
grist.onRecord(function (record, mappings) {
const mapped = grist.mapColumnNames(record);
// First check if all columns were mapped.
if (mapped) {
console.log(
`Using ${mappings.QuoteName} and ${mappings.QuoteID} and ${mappings.Template} columns`
);
const recordTemplate = JSON.parse(mapped.Template);
baseURL = recordTemplate.url;
template = recordTemplate.body.template;
data = recordTemplate.body.data;
authUser = recordTemplate.user;
authPassword = recordTemplate.pw;
fileName = mapped.QuoteName;
data.quoteID = mapped.QuoteID;
} else {
// Helper returned a null value. It means that not all
// required columns were mapped.
console.error("Please map all columns");
}
jsreport.headers["Authorization"] =
"Basic " + btoa(`${authUser}:${authPassword}`);
jsreport.serverUrl = baseURL;
async function getFile() {
try {
const report = await jsreport.render({
template,
data,
});
// download the output to the file
report.download(`${fileName}.pdf`);
} catch (error) {
console.error("Error generating report:", error);
}
}
document.getElementById("quotename").innerHTML = fileName;
const button = document.getElementById("generate-quote-btn");
if (!button.hasEventListener) {
button.addEventListener("click", async function () {
console.log("Click!!");
event.stopPropagation();
await getFile();
});
button.hasEventListener = true; // Custom flag to indicate listener has been added
}
});
</script>
</body>
</html>