diff --git a/app/Views/shared/dialog_preview.php b/app/Views/shared/dialog_preview.php index 8d21190..9bb9846 100644 --- a/app/Views/shared/dialog_preview.php +++ b/app/Views/shared/dialog_preview.php @@ -17,16 +17,19 @@ -

+

+ + You have already validated this request +

diff --git a/app/Views/shared/script_requests.php b/app/Views/shared/script_requests.php index 87380ec..d0bffb6 100644 --- a/app/Views/shared/script_requests.php +++ b/app/Views/shared/script_requests.php @@ -359,7 +359,7 @@ document.addEventListener('alpine:init', () => { const eng = this.generateLang === 1 ? '?eng=1' : ''; try { - const res = await fetch(`${BASEURL}/report/${this.generateAccessnumber}/pdf${eng}`); + const res = await fetch(`${BASEURL}report/${this.generateAccessnumber}/pdf${eng}`); const data = await res.json(); if (data.success) { @@ -436,24 +436,44 @@ document.addEventListener('alpine:init', () => { this.isPreviewIframeLoaded = false; }, getPreviewUrl() { - return `${BASEURL}/report/${this.previewAccessnumber}`; + if(this.previewAccessnumber != null) { + return `${BASEURL}report/${this.previewAccessnumber}`; + } }, onPreviewIframeLoad() { this.isPreviewIframeLoaded = true; }, - validateFromPreview(accessnumber, userid) { + async validateFromPreview(accessnumber, userid) { if (!this.isPreviewIframeLoaded || this.isPreviewValidating) return; this.isPreviewValidating = true; - fetch(`${BASEURL}/api/requests/validate/${accessnumber}`, { - method: 'POST', - headers: { 'Content-Type': 'application/json' } - }) - .then(res => res.json()) - .then(data => { + try { + const res = await fetch(`${BASEURL}/api/requests/validate/${accessnumber}`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' } + }); + const data = await res.json(); this.isPreviewValidating = false; + if (data.val) { this.showToast(`Validated (val${data.val}): ${accessnumber}`, 'success'); + + // Generate PDF if second validation (val2) succeeded + if (data.val === 2) { + try { + const pdfRes = await fetch(`${BASEURL}report/${accessnumber}/pdf`); + const pdfData = await pdfRes.json(); + + if (pdfData.success) { + this.showToast(`${pdfData.lang} PDF queued for download`, 'success'); + } else { + this.showToast('PDF generation failed', 'error'); + } + } catch (e) { + this.showToast('PDF generation failed', 'error'); + } + } + this.fetchList(); this.closePreviewDialog(); } else if (data.message && data.message.includes('already validate')) { @@ -461,11 +481,10 @@ document.addEventListener('alpine:init', () => { } else { this.showToast(data.message || 'Validation failed', 'error'); } - }) - .catch(() => { + } catch (e) { this.isPreviewValidating = false; this.showToast('Validation failed', 'error'); - }); + } }, showToast(message, type = 'success') { diff --git a/app/Views/shared/script_validation.php b/app/Views/shared/script_validation.php index 93a7cd9..a05bc07 100644 --- a/app/Views/shared/script_validation.php +++ b/app/Views/shared/script_validation.php @@ -210,12 +210,11 @@ document.addEventListener('alpine:init', () => { headers: { "Content-Type": "application/json" }, body: JSON.stringify({ userid: `${userid}` }) }).then(response => response.json()).then(data => { - // Capture accessnumber before async operations - const currentAccessnumber = accessnumber; + console.log(data); if (data.val === 2) { - this.showToast(`Validated (val2): ${currentAccessnumber} - PDF queued`); + this.showToast(`Validated (val2): ${data.accessnumber} - PDF queued`); // Trigger PDF auto-generation after val2 - fetch(`${BASEURL}/report/${currentAccessnumber}/pdf`).then(res => res.json()).then(pdfData => { + fetch(`${BASEURL}/report/${data.accessnumber}/pdf`).then(res => res.json()).then(pdfData => { if (pdfData.success) { console.log('PDF generation queued:', pdfData.jobId); } else {