import React, { useEffect } from 'react'; import { Form, Input, Button, Switch, Radio, Upload, Typography } from 'antd'; import { DeleteOutlined, UploadOutlined } from '@ant-design/icons'; import { uploadFile, getFolderFromFileType } from '../../../../api/file-uploads'; import { NotifAlert, NotifOk } from '../../../../components/Global/ToastNotif'; const { Text } = Typography; const SolutionField = ({ fieldId, index, solutionStatus, isReadOnly, fileList, onRemove, onSolutionTypeChange, onSolutionStatusChange, onFileUpload, currentSolutionData, onFileView, errorCodeForm, }) => { // Watch the solution status from the form const watchedStatus = Form.useWatch(`solution_status_${fieldId}`, errorCodeForm); useEffect(() => { if (currentSolutionData && errorCodeForm) { if (currentSolutionData.solution_name) { errorCodeForm.setFieldValue( `solution_name_${fieldId}`, currentSolutionData.solution_name ); } if (currentSolutionData.type_solution === 'text' && currentSolutionData.text_solution) { errorCodeForm.setFieldValue( `text_solution_${fieldId}`, currentSolutionData.text_solution ); } if (currentSolutionData.type_solution) { const formValue = currentSolutionData.type_solution === 'image' || currentSolutionData.type_solution === 'pdf' ? 'file' : currentSolutionData.type_solution; errorCodeForm.setFieldValue(`solution_type_${fieldId}`, formValue); } // Only set status if it's not already set to prevent overwriting user changes const currentStatus = errorCodeForm.getFieldValue(`solution_status_${fieldId}`); if (currentSolutionData.is_active !== undefined && currentStatus === undefined) { errorCodeForm.setFieldValue( `solution_status_${fieldId}`, currentSolutionData.is_active ); } } }, [currentSolutionData, fieldId, errorCodeForm]); const handleBeforeUpload = async (file) => { const isAllowedType = ['application/pdf', 'image/jpeg', 'image/png', 'image/gif'].includes( file.type ); if (!isAllowedType) { NotifAlert({ icon: 'error', title: 'Error', message: `${file.name} bukan file PDF atau gambar yang diizinkan.`, }); return Upload.LIST_IGNORE; } try { // Upload file immediately to get path const fileExtension = file.name.split('.').pop().toLowerCase(); const isImage = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp'].includes(fileExtension); const fileType = isImage ? 'image' : 'pdf'; const folder = getFolderFromFileType(fileType); const uploadResponse = await uploadFile(file, folder); const actualPath = uploadResponse.data?.path_solution || ''; if (actualPath) { file.uploadPath = actualPath; file.solution_name = file.name; file.solutionId = fieldId; file.type_solution = fileType; onFileUpload(file); NotifOk({ icon: 'success', title: 'Berhasil', message: `${file.name} berhasil diupload!`, }); } else { NotifAlert({ icon: 'error', title: 'Gagal', message: `Gagal mengupload ${file.name}`, }); } } catch (error) { console.error('Error uploading file:', error); NotifAlert({ icon: 'error', title: 'Error', message: `Gagal mengupload ${file.name}. Silakan coba lagi.`, }); } return false; }; return (
Solution {index + 1}
{ onSolutionStatusChange(fieldId, checked); }} style={{ backgroundColor: (watchedStatus ?? true) ? '#23A55A' : '#bfbfbf', }} /> {(watchedStatus ?? true) ? 'Active' : 'Non Active'}
{ onSolutionTypeChange(fieldId, e.target.value); }} disabled={isReadOnly} > Text Solution File Upload prevValues[`solution_type_${fieldId}`] !== currentValues[`solution_type_${fieldId}`] } noStyle > {({ getFieldValue }) => { const currentType = getFieldValue(`solution_type_${fieldId}`) || 'text'; const displayType = currentType === 'file' && currentSolutionData ? currentSolutionData.type_solution === 'image' ? 'image' : currentSolutionData.type_solution === 'pdf' ? 'pdf' : 'file' : currentType; return displayType === 'text' ? ( ) : ( <> {/* Show existing file info for both preview and edit mode */} {currentSolutionData && currentSolutionData.type_solution !== 'text' && currentSolutionData.path_solution && ( {(() => { const solution = currentSolutionData; const fileName = solution.file_upload_name || solution.path_solution?.split('/')[1] || 'File'; const fileType = solution.type_solution; if (fileType !== 'text' && solution.path_solution) { return (
{fileType === 'image' ? '[Image]' : '[Document]'}{' '} {fileName}
); } return null; })()}
)} file.solutionId === fieldId), // Add existing file to fileList if it exists ...(currentSolutionData && currentSolutionData.type_solution !== 'text' && currentSolutionData.path_solution ? [ { uid: `existing-${fieldId}`, name: currentSolutionData.file_upload_name || currentSolutionData.path_solution?.split( '/' )[1] || 'File', status: 'done', url: null, // We'll use the path_solution for viewing solutionId: fieldId, type_solution: currentSolutionData.type_solution, uploadPath: currentSolutionData.path_solution, existingFile: true, }, ] : []), ]} onRemove={(file) => {}} beforeUpload={handleBeforeUpload} > ); }}
); }; export default SolutionField;