repair: add edit brand device
This commit is contained in:
@@ -13,7 +13,6 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
setSolutionTypes(prev => ({ ...prev, [newKey]: 'text' }));
|
||||
setSolutionStatuses(prev => ({ ...prev, [newKey]: true }));
|
||||
|
||||
// Set default values for the new field
|
||||
setTimeout(() => {
|
||||
solutionForm.setFieldValue(['solution_items', newKey, 'name'], '');
|
||||
solutionForm.setFieldValue(['solution_items', newKey, 'type'], 'text');
|
||||
@@ -24,10 +23,10 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
|
||||
const handleRemoveSolutionField = (key) => {
|
||||
if (solutionFields.length <= 1) {
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
setSolutionFields(prev => prev.filter(field => field.key !== key));
|
||||
setSolutionFields(prev => prev.filter(field => field !== key));
|
||||
|
||||
// Clean up type and status
|
||||
const newTypes = { ...solutionTypes };
|
||||
@@ -41,6 +40,26 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
|
||||
const handleSolutionTypeChange = (key, value) => {
|
||||
setSolutionTypes(prev => ({ ...prev, [key]: value }));
|
||||
|
||||
if (value === 'text') {
|
||||
setTimeout(() => {
|
||||
const fieldName = ['solution_items', key];
|
||||
const currentSolutionData = solutionForm.getFieldsValue([fieldName]) || {};
|
||||
const solutionData = currentSolutionData[`solution_items,${key}`] || currentSolutionData[`solution_items.${key}`] || {};
|
||||
|
||||
const updatedSolutionData = {
|
||||
...solutionData,
|
||||
fileUpload: null,
|
||||
file: null
|
||||
};
|
||||
|
||||
// Update form with cleared file data
|
||||
const commaPath = fieldName.join(',');
|
||||
solutionForm.setFieldValue(commaPath, updatedSolutionData);
|
||||
solutionForm.setFieldValue([...fieldName, 'fileUpload'], null);
|
||||
solutionForm.setFieldValue([...fieldName, 'file'], null);
|
||||
}, 0);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSolutionStatusChange = (key, value) => {
|
||||
@@ -48,7 +67,7 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
};
|
||||
|
||||
const resetSolutionFields = () => {
|
||||
setSolutionFields([{ name: ['solution_items', 0], key: 0 }]);
|
||||
setSolutionFields([0]);
|
||||
setSolutionTypes({ 0: 'text' });
|
||||
setSolutionStatuses({ 0: true });
|
||||
|
||||
@@ -71,8 +90,10 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
}
|
||||
|
||||
const solutionKey = firstField.key || firstField;
|
||||
const solutionPath = `solution_items,${solutionKey}`;
|
||||
const firstSolution = values[solutionPath];
|
||||
// Try both notations for compatibility
|
||||
const commaPath = `solution_items,${solutionKey}`;
|
||||
const dotPath = `solution_items.${solutionKey}`;
|
||||
const firstSolution = values[commaPath] || values[dotPath];
|
||||
|
||||
if (!firstSolution || !firstSolution.name || firstSolution.name.trim() === '') {
|
||||
return false;
|
||||
@@ -86,25 +107,73 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
};
|
||||
|
||||
const getSolutionData = () => {
|
||||
const values = solutionForm.getFieldsValue();
|
||||
const values = solutionForm.getFieldsValue(true);
|
||||
|
||||
const result = solutionFields.map(key => {
|
||||
const solution = values[`solution_items,${key}`];
|
||||
let solution = null;
|
||||
|
||||
// Try nested format first (preferred)
|
||||
if (values.solution_items && values.solution_items[key]) {
|
||||
solution = values.solution_items[key];
|
||||
}
|
||||
|
||||
// Fallback to comma notation
|
||||
if (!solution) {
|
||||
const commaKey = `solution_items,${key}`;
|
||||
solution = values[commaKey];
|
||||
}
|
||||
|
||||
// Fallback to dot notation
|
||||
if (!solution) {
|
||||
const dotKey = `solution_items.${key}`;
|
||||
solution = values[dotKey];
|
||||
}
|
||||
|
||||
if (!solution) {
|
||||
// Last resort: search in all keys
|
||||
const allKeys = Object.keys(values);
|
||||
const foundKey = allKeys.find(k => k.includes(key.toString()) && k.includes('solution_items'));
|
||||
if (foundKey) {
|
||||
solution = values[foundKey];
|
||||
}
|
||||
}
|
||||
|
||||
if (!solution) return null;
|
||||
|
||||
const validSolution = solution.name && solution.name.trim() !== '';
|
||||
|
||||
if (validSolution) {
|
||||
return {
|
||||
solution_name: solution.name || 'Default Solution',
|
||||
type_solution: solutionTypes[key] || 'text',
|
||||
text_solution: solution.text || '',
|
||||
path_solution: solution.file || '',
|
||||
is_active: solution.status !== false,
|
||||
};
|
||||
if (!validSolution) return null;
|
||||
|
||||
let pathSolution = '';
|
||||
let fileObject = null;
|
||||
|
||||
if (solution.fileUpload && typeof solution.fileUpload === 'object' && Object.keys(solution.fileUpload).length > 0) {
|
||||
pathSolution = solution.fileUpload.path_solution || solution.fileUpload.uploadPath || '';
|
||||
fileObject = solution.fileUpload;
|
||||
} else if (solution.file && typeof solution.file === 'object' && Object.keys(solution.file).length > 0) {
|
||||
pathSolution = solution.file.path_solution || solution.file.uploadPath || '';
|
||||
fileObject = solution.file;
|
||||
} else if (solution.file && typeof solution.file === 'string' && solution.file.trim() !== '') {
|
||||
pathSolution = solution.file;
|
||||
}
|
||||
return null;
|
||||
|
||||
let typeSolution = solutionTypes[key] || solution.type || 'text';
|
||||
|
||||
if (typeSolution === 'file') {
|
||||
if (fileObject && fileObject.type_solution) {
|
||||
typeSolution = fileObject.type_solution;
|
||||
} else {
|
||||
typeSolution = 'image';
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
solution_name: solution.name,
|
||||
type_solution: typeSolution,
|
||||
text_solution: solution.text || '',
|
||||
path_solution: pathSolution,
|
||||
is_active: solution.status !== false && solution.status !== undefined ? solution.status : (solutionStatuses[key] !== false),
|
||||
};
|
||||
}).filter(Boolean);
|
||||
|
||||
return result;
|
||||
@@ -123,17 +192,44 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
|
||||
solutions.forEach((solution, index) => {
|
||||
const key = solution.id || index;
|
||||
|
||||
console.log(`🔧 Processing solution ${key}:`, solution);
|
||||
|
||||
let fileObject = null;
|
||||
if (solution.path_solution && solution.path_solution.trim() !== '') {
|
||||
const fileName = solution.file_upload_name || solution.path_solution.split('/').pop() || `file_${index}`;
|
||||
|
||||
fileObject = {
|
||||
uploadPath: solution.path_solution,
|
||||
path_solution: solution.path_solution,
|
||||
name: fileName,
|
||||
type_solution: solution.type_solution || 'image',
|
||||
isExisting: true,
|
||||
size: 0,
|
||||
type: solution.type_solution === 'pdf' ? 'application/pdf' : 'image/jpeg',
|
||||
fileExtension: solution.type_solution === 'pdf' ? 'pdf' :
|
||||
(fileName.split('.').pop().toLowerCase() || 'jpg')
|
||||
};
|
||||
console.log(`✅ Created file object for ${key}:`, fileObject);
|
||||
}
|
||||
|
||||
const isFileType = solution.type_solution && solution.type_solution !== 'text' && fileObject;
|
||||
|
||||
solutionsValues[key] = {
|
||||
name: solution.solution_name || '',
|
||||
type: solution.type_solution || 'text',
|
||||
type: isFileType ? 'file' : 'text',
|
||||
text: solution.text_solution || '',
|
||||
file: solution.path_solution || '',
|
||||
file: fileObject,
|
||||
fileUpload: fileObject,
|
||||
status: solution.is_active !== false
|
||||
};
|
||||
newTypes[key] = solution.type_solution || 'text';
|
||||
newTypes[key] = isFileType ? 'file' : 'text';
|
||||
newStatuses[key] = solution.is_active !== false;
|
||||
});
|
||||
|
||||
// Set all form values at once
|
||||
console.log('🔧 Final solutions values:', solutionsValues);
|
||||
console.log('🔧 Final solution types:', newTypes);
|
||||
|
||||
const formValues = {};
|
||||
Object.keys(solutionsValues).forEach(key => {
|
||||
const solution = solutionsValues[key];
|
||||
@@ -142,10 +238,12 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
type: solution.type,
|
||||
text: solution.text,
|
||||
file: solution.file,
|
||||
status: solution.is_active !== false
|
||||
fileUpload: solution.fileUpload,
|
||||
status: solution.status
|
||||
};
|
||||
});
|
||||
|
||||
console.log('🔧 Setting form values:', formValues);
|
||||
form.setFieldsValue(formValues);
|
||||
setSolutionTypes(newTypes);
|
||||
setSolutionStatuses(newStatuses);
|
||||
|
||||
Reference in New Issue
Block a user