From 6167d59501eb1169d594e5aff2a9295b06fe3a7a Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Thu, 5 Jun 2025 15:38:06 +0200 Subject: [PATCH] document validation takes json certificates --- src/pages/account/document-validation.ts | 40 ++++++++++++++++-------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/pages/account/document-validation.ts b/src/pages/account/document-validation.ts index 25cc3c0..6b68eb1 100644 --- a/src/pages/account/document-validation.ts +++ b/src/pages/account/document-validation.ts @@ -10,7 +10,7 @@ export function getDocumentValidation(container: HTMLElement) { const state = { documentFile: null as File | null, - certificateFile: null as File | null + certificateJson: null as Record | null }; const wrapper = document.createElement('div'); @@ -38,19 +38,34 @@ export function getDocumentValidation(container: HTMLElement) { cursor: pointer; background: #fafafa; `; - box.textContent = `Drop ${label} PDF here`; + box.textContent = `Drop ${label} here`; box.ondragover = e => { e.preventDefault(); box.style.background = '#eee'; }; box.ondragleave = () => { box.style.background = '#fafafa'; }; - box.ondrop = e => { + box.ondrop = async e => { e.preventDefault(); const file = e.dataTransfer?.files?.[0]; - if (file && file.type === 'application/pdf') { - onDrop(file); - box.textContent = `${label} uploaded: ${file.name}`; - box.style.borderColor = 'green'; - box.style.background = '#e6ffed'; - } else { - alert('Please drop a valid PDF file.'); + if (!file) return; + if (label === 'Document') { + if (file.type === 'application/pdf') { + onDrop(file); + box.textContent = `${label} uploaded: ${file.name}`; + box.style.borderColor = 'green'; + box.style.background = '#e6ffed'; + } else { + alert('Please drop a valid PDF file.'); + } + } else if (label === 'Certificate') { + try { + const text = await file.text(); + const json = JSON.parse(text); + onDrop(file); + state.certificateJson = json; + box.textContent = `Certificate loaded: ${file.name}`; + box.style.borderColor = 'green'; + box.style.background = '#e6ffed'; + } catch (err) { + alert('Invalid certificate file. Must be valid JSON.'); + } } }; return box; @@ -62,7 +77,6 @@ export function getDocumentValidation(container: HTMLElement) { }); const certBox = createDropBox('Certificate', file => { - state.certificateFile = file; checkSuccess(); }); @@ -73,7 +87,7 @@ export function getDocumentValidation(container: HTMLElement) { container.appendChild(wrapper); function checkSuccess() { - if (state.documentFile && state.certificateFile) { + if (state.documentFile && state.certificateJson) { showSuccessScreen(); } } @@ -108,7 +122,7 @@ export function getDocumentValidation(container: HTMLElement) { successWrapper.appendChild(docLabel); const certLabel = document.createElement('div'); - certLabel.textContent = `Certificate: ${state.certificateFile!.name}`; + certLabel.textContent = `Certificate: ${state.certificateJson!.name}`; successWrapper.appendChild(certLabel); // === Mocked verification data ===