diff --git a/src/pages/master/brandDevice/component/SolutionField.jsx b/src/pages/master/brandDevice/component/SolutionField.jsx index 9e2da06..0fc6a35 100644 --- a/src/pages/master/brandDevice/component/SolutionField.jsx +++ b/src/pages/master/brandDevice/component/SolutionField.jsx @@ -1,8 +1,9 @@ import React, { useState } from 'react'; -import { Form, Input, Button, Switch, Radio, Typography, Space } from 'antd'; -import { DeleteOutlined } from '@ant-design/icons'; +import { Form, Input, Button, Switch, Radio, Typography, Space, Card } from 'antd'; +import { DeleteOutlined, EyeOutlined, FileOutlined } from '@ant-design/icons'; import FileUploadHandler from './FileUploadHandler'; import { NotifAlert } from '../../../../components/Global/ToastNotif'; +import { getFileUrl, getFolderFromFileType } from '../../../../api/file-uploads'; const { Text } = Typography; const { TextArea } = Input; @@ -20,50 +21,89 @@ const SolutionFieldNew = ({ onRemove, onFileUpload, onFileView, - fileList = [] + fileList = [], + originalSolutionData = null }) => { const form = Form.useFormInstance(); - const existingFile = Form.useWatch([`solution_items,${fieldKey}`, 'fileUpload'], form) || - Form.useWatch([`solution_items,${fieldKey}`, 'file'], form); + const [currentFile, setCurrentFile] = useState(null); + const [isDeleted, setIsDeleted] = useState(false); - // Get form values for debugging and file data extraction - const allFormValues = form.getFieldsValue(true); - const solutionData = allFormValues[`solution_items,${fieldKey}`] || {}; + const fileUpload = Form.useWatch(['solution_items', fieldKey, 'fileUpload'], form); + const file = Form.useWatch(['solution_items', fieldKey, 'file'], form); - // Extract file data from form values for preview - const getFileFromFormValues = () => { - if (solutionData.fileUpload && typeof solutionData.fileUpload === 'object' && Object.keys(solutionData.fileUpload).length > 0) { - return solutionData.fileUpload; + const getSolutionData = () => { + try { + return form.getFieldValue(['solution_items', fieldKey]) || {}; + } catch (error) { + return {}; } - if (solutionData.file && typeof solutionData.file === 'object' && Object.keys(solutionData.file).length > 0) { - return solutionData.file; - } - return null; }; - const fileFromForm = getFileFromFormValues(); - const displayFile = existingFile || fileFromForm; + const nameValue = Form.useWatch(['solution_items', fieldKey, 'name'], form); + const typeValue = Form.useWatch(['solution_items', fieldKey, 'type'], form); + const textValue = Form.useWatch(['solution_items', fieldKey, 'text'], form); + const fileNameValue = Form.useWatch(['solution_items', fieldKey, 'fileName'], form); + + const pathSolution = Form.useWatch(['solution_items', fieldKey, 'path_solution'], form); + + const [deleteCounter, setDeleteCounter] = useState(0); + + React.useEffect(() => { + const getFileFromFormValues = () => { + const hasValidFileUpload = fileUpload && typeof fileUpload === 'object' && Object.keys(fileUpload).length > 0; + const hasValidFile = file && typeof file === 'object' && Object.keys(file).length > 0; + const hasValidPath = pathSolution && pathSolution.trim() !== ''; + + const wasExplicitlyDeleted = + (fileUpload === null || file === null || pathSolution === null) && + !hasValidFileUpload && + !hasValidFile && + !hasValidPath; + + if (wasExplicitlyDeleted) { + return null; + } + + if (solutionType === 'text') { + return null; + } + + if (hasValidFileUpload) { + return fileUpload; + } + if (hasValidFile) { + return file; + } + if (hasValidPath) { + return { + name: fileNameValue || pathSolution.split('/').pop() || 'File', + uploadPath: pathSolution, + url: pathSolution, + path: pathSolution + }; + } + + return null; + }; + + const fileFromForm = getFileFromFormValues(); + + if (JSON.stringify(currentFile) !== JSON.stringify(fileFromForm)) { + setCurrentFile(fileFromForm); + } + }, [fileUpload, file, pathSolution, solutionType, deleteCounter, fileNameValue, fieldKey]); - console.log(`🔍 SolutionField ${fieldKey}:`, { - solutionType, - hasPathSolution: !!solutionData.path_solution, - pathSolution: solutionData.path_solution, - fileFromForm, - existingFile, - displayFile, - shouldRenderPreview: !!displayFile - }); const renderSolutionContent = () => { if (solutionType === 'text') { return (