sk-hasil-belajar can only be printed once, prevented to be printed multiple times, error texts will be removed on retry

This commit is contained in:
Gregorio Chiko Putra
2019-05-16 12:07:18 +07:00
parent f8d4d1df98
commit c4b05cb525
5 changed files with 114 additions and 72 deletions

View File

@@ -34,6 +34,10 @@ var component = {
usbn: [],
unbk: [],
},
// This prop is to prevent double dialog when printing,
// caused by the print button's confirm()
// and the beforeprint's alert()
confirmPrint: 0,
view: () => {
component.rataRata = {
raport: [],
@@ -93,6 +97,7 @@ var component = {
: m('button.print-button', {
onclick: () => {
if (confirm('Surat Keterangan ini hanya dapat dicetak satu kali. Lanjutkan?')) {
component.confirmPrint = 1;
AccessLog.create({siswaId: Siswa.current.id, src: 'sk-hasil-belajar'}).then(() => {
AccessLog.fetch('sk-hasil-belajar');
window.print();
@@ -265,7 +270,7 @@ var component = {
]);
})),
m('tr', [
m('th', 'A.'),
m('th', 'B.'),
m('th[colspan=5].text-left', 'Adaptif'),
]),
m('tbody.counter',
@@ -282,7 +287,7 @@ var component = {
]);
}),
m('tr', [
m('th', 'A.'),
m('th', 'C.'),
m('th[colspan=5].text-left', 'Produktif'),
])),
m('tbody.counter',
@@ -299,7 +304,7 @@ var component = {
]);
})),
m('tr', [
m('th', 'A.'),
m('th', 'D.'),
m('th[colspan=5].text-left', 'Mulok'),
]),
m('tbody.counter',
@@ -345,4 +350,53 @@ var component = {
},
};
// Below is to handle Ctrl+P or MouseR->Print
window.addEventListener("beforeprint", function () {
// If the print button did not exists, show nothing when printing
if (document.body.querySelector('button.print-button') === null)
document.body.style.display = 'none';
else {
if (!component.confirmPrint) {
alert('Surat Keterangan ini hanya dapat dicetak satu kali. Lanjutkan?');
component.confirmPrint = 1;
AccessLog.create({siswaId: Siswa.current.id, src: 'sk-hasil-belajar'}).then(() => {
AccessLog.fetch('sk-hasil-belajar');
})
.catch(e => {
if (e.code === 0) {
AccessLog.error = {
message: e.message,
errors: {
create: ['Terjadi kesalahan saat menghubungkan ke server.'],
},
};
}
else {
AccessLog.error = JSON.parse(e.message);
}
});
}
}
});
window.addEventListener("afterprint", function () {
// Give back the whole document.body
if (document.body.querySelector('button.print-button') === null)
document.body.style.display = 'block';
// Hide the print button
if (document.body.querySelector('button.print-button') !== null)
document.body.querySelector('button.print-button').style.display = 'none';
// To redraw the content to prevent printing
let nisn = document.body.querySelector('input[name=nisn]').value,
tanggalLahir = document.body.querySelector('input[name=tanggalLahir]').value;
Siswa.cariData({
nisn,
tanggalLahir,
src: 'sk-hasil-belajar',
with: ['hasilBelajar', 'accessLog'],
});
});
export default component;

View File

@@ -5,6 +5,7 @@ var model = {
error: {},
fetch: src => {
model.current = {};
model.error = {};
return m.request({
method: 'get',
url: '/api/access_log',
@@ -27,6 +28,7 @@ var model = {
});
},
create: data => {
model.error = {};
return m.request({
method: 'post',
url: '/api/access_log',
@@ -36,28 +38,6 @@ var model = {
model.current = response;
});
},
search: nisn => {
model.current = {};
m.request({
method: 'get',
url: '/api/access_log/' + nisn,
})
.then(response => {
mode.current = response;
})
.catch(e => {
if (e.code === 0) {
model.error = {
message: e.message,
errors: {
accessLog: ['Terjadi kesalahan saat menghubungkan ke server.']
},
};
}
else
model.error = JSON.parse(e.message);
});
},
};
export default model;

View File

@@ -1,24 +0,0 @@
import m from "mithril"
import AccessLog from "./AccessLog"
var model = {
current: {},
error: {},
cariData: data => {
model.current = {};
m.request({
method: 'post',
url: '/api/hasil_belajar',
data,
})
.then(response => {
model.current = response;
AccessLog.fetch('sk-hasil-belajar');
})
.catch(e => {
model.error = JSON.parse(e.message);
});
},
};
export default model;

View File

@@ -6,6 +6,7 @@ var model = {
error: {},
cariData: data => {
model.current = {};
model.error = {},
m.request({
method: 'post',
url: '/api/siswa',