From fae5b282440283fdceff3d58d16d809e3e6610a6 Mon Sep 17 00:00:00 2001 From: jaya Date: Tue, 16 Sep 2025 14:40:01 +0700 Subject: [PATCH] ok --- src/components/AddDocumentModal.js | 65 ++++++++++++++++++++++++------ src/pages/InputDataPage.js | 7 +++- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/components/AddDocumentModal.js b/src/components/AddDocumentModal.js index 028650e..650ecbb 100644 --- a/src/components/AddDocumentModal.js +++ b/src/components/AddDocumentModal.js @@ -41,19 +41,58 @@ export default function AddDocumentModal({ isOpen, onClose }) { const handleAddOption = (fieldId) => {setFields(fields.map(field =>field.id === fieldId? { ...field, options: [...field.options, { id: Date.now(), value: '' }] }: field));}; const handleOptionChange = (fieldId, optionId, event) => {const { value } = event.target;setFields(fields.map(field =>field.id === fieldId? { ...field, options: field.options.map(opt => opt.id === optionId ? { ...opt, value } : opt) }: field));}; const handleRemoveOption = (fieldId, optionId) => {setFields(fields.map(field =>field.id === fieldId? { ...field, options: field.options.filter(opt => opt.id !== optionId) }: field));}; - const handleSave = () => { - const newDocumentType = { - template: selectedTemplate, - name: docTypeName, - fields: fields.map(({ id, name, type, options }) => ({ - name, - type, - ...(type === 'Selection' && { options: options.map(opt => opt.value) }) - })) - }; - console.log("Menyimpan data JSON:", JSON.stringify(newDocumentType, null, 2)); - onClose(); - setSelectedTemplate(null); + const handleSave = async () => { + // Validasi nama tipe dokumen + if (!docTypeName.trim()) { + alert("Nama tipe dokumen wajib diisi"); + return; + } + // Siapkan expectation sesuai format backend + const expectation = {}; + fields.forEach(field => { + if (!field.name.trim()) return; + if (field.type === "Selection") { + expectation[field.name] = { + type: "selection", + options: field.options.map(opt => opt.value).filter(Boolean) + }; + } else if (field.type === "Number") { + expectation[field.name] = "number"; + } else if (field.type === "Date") { + expectation[field.name] = "date"; + } else { + expectation[field.name] = "text"; + } + }); + + try { + const org = localStorage.getItem('selected_organization'); + if (!org) throw new Error("Organisasi belum dipilih"); + const { organization_id } = JSON.parse(org); + + const res = await fetch("https://bot.kediritechnopark.com/webhook/create-data-type", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + organization_id, + nama_tipe: docTypeName, + expectation, + }), + }); + if (!res.ok) { + const errData = await res.json().catch(() => ({})); + throw new Error(errData.message || "Gagal menambah jenis dokumen"); + } + // Reset state dan tutup modal + setSelectedTemplate(null); + setDocTypeName(''); + setFields([]); + onClose(); + // Jika DashboardPage Anda memakai onSuccess, panggil juga di sini: + // if (typeof onSuccess === "function") onSuccess(); + } catch (err) { + alert(err.message); + } }; if (!isOpen) return null; diff --git a/src/pages/InputDataPage.js b/src/pages/InputDataPage.js index 3f6b125..653b21a 100644 --- a/src/pages/InputDataPage.js +++ b/src/pages/InputDataPage.js @@ -19,7 +19,7 @@ export default function InputDataPage() { const fileInputRef = useRef(null); const cameraInputRef = useRef(null); const [isDragging, setIsDragging] = useState(false); - + console.log(expectation) // --- PERUBAHAN: State baru untuk progress upload --- const [uploadProgress, setUploadProgress] = useState(0); @@ -47,6 +47,7 @@ export default function InputDataPage() { const totalFiles = filesToUpload.length; const successfulUploads = []; const failedUploads = []; + const allResults = []; // <-- Variabel untuk mengumpulkan semua hasil for (let i = 0; i < totalFiles; i++) { const file = filesToUpload[i]; @@ -71,6 +72,7 @@ export default function InputDataPage() { const resultJson = await response.json(); successfulUploads.push({ file: file.name, result: resultJson }); + allResults.push(resultJson); // <-- Kumpulkan hasil response di sini console.log(`Sukses mengupload ${file.name}:`, resultJson); } catch (error) { @@ -82,6 +84,9 @@ export default function InputDataPage() { setIsUploading(false); setUploadProgress(0); + // allResults sekarang berisi array hasil response dari semua file + console.log("Semua hasil response:", allResults); + // Beri ringkasan hasil upload alert(`Selesai! Berhasil: ${successfulUploads.length}, Gagal: ${failedUploads.length}`);