repair: delete error code
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useState } from 'react';
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
export const useSolutionLogic = (solutionForm) => {
|
||||
const [solutionFields, setSolutionFields] = useState([0]);
|
||||
@@ -6,19 +6,68 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
const [solutionStatuses, setSolutionStatuses] = useState({ 0: true });
|
||||
const [solutionsToDelete, setSolutionsToDelete] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
setTimeout(() => {
|
||||
if (solutionForm) {
|
||||
solutionForm.setFieldsValue({
|
||||
solution_items: {
|
||||
0: {
|
||||
name: 'Solution 1',
|
||||
status: true,
|
||||
type: 'text',
|
||||
text: 'Deskripsi untuk Solution 1',
|
||||
file: null,
|
||||
fileUpload: null
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 100);
|
||||
}, [solutionForm]);
|
||||
|
||||
const handleAddSolutionField = () => {
|
||||
const newKey = Date.now();
|
||||
const newKey = Date.now();
|
||||
|
||||
setSolutionFields(prev => [...prev, newKey]);
|
||||
setSolutionTypes(prev => ({ ...prev, [newKey]: 'text' }));
|
||||
setSolutionStatuses(prev => ({ ...prev, [newKey]: true }));
|
||||
|
||||
setTimeout(() => {
|
||||
solutionForm.setFieldValue(['solution_items', newKey, 'name'], '');
|
||||
const currentFormValues = solutionForm.getFieldsValue(true);
|
||||
const existingNames = [];
|
||||
|
||||
Object.keys(currentFormValues).forEach(key => {
|
||||
if (key.startsWith('solution_items,') || key.startsWith('solution_items.')) {
|
||||
const solutionData = currentFormValues[key];
|
||||
if (solutionData && solutionData.name) {
|
||||
existingNames.push(solutionData.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (currentFormValues.solution_items) {
|
||||
Object.values(currentFormValues.solution_items).forEach(solution => {
|
||||
if (solution && solution.name) {
|
||||
existingNames.push(solution.name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let solutionNumber = solutionFields.length + 1;
|
||||
let defaultName = `Solution ${solutionNumber}`;
|
||||
|
||||
while (existingNames.includes(defaultName)) {
|
||||
solutionNumber++;
|
||||
defaultName = `Solution ${solutionNumber}`;
|
||||
}
|
||||
|
||||
solutionForm.setFieldValue(['solution_items', newKey, 'name'], defaultName);
|
||||
solutionForm.setFieldValue(['solution_items', newKey, 'type'], 'text');
|
||||
solutionForm.setFieldValue(['solution_items', newKey, 'text'], '');
|
||||
solutionForm.setFieldValue(['solution_items', newKey, 'text'], `Deskripsi untuk ${defaultName}`);
|
||||
solutionForm.setFieldValue(['solution_items', newKey, 'status'], true);
|
||||
}, 0);
|
||||
solutionForm.setFieldValue(['solution_items', newKey, 'file'], null);
|
||||
solutionForm.setFieldValue(['solution_items', newKey, 'fileUpload'], null);
|
||||
}, 100);
|
||||
};
|
||||
|
||||
const handleRemoveSolutionField = (key) => {
|
||||
@@ -28,7 +77,6 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
|
||||
setSolutionFields(prev => prev.filter(field => field !== key));
|
||||
|
||||
// Clean up type and status
|
||||
const newTypes = { ...solutionTypes };
|
||||
const newStatuses = { ...solutionStatuses };
|
||||
delete newTypes[key];
|
||||
@@ -36,30 +84,52 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
|
||||
setSolutionTypes(newTypes);
|
||||
setSolutionStatuses(newStatuses);
|
||||
|
||||
setTimeout(() => {
|
||||
try {
|
||||
solutionForm.setFieldValue(['solution_items', key], undefined);
|
||||
solutionForm.setFieldValue(['solution_items', key, 'name'], undefined);
|
||||
solutionForm.setFieldValue(['solution_items', key, 'type'], undefined);
|
||||
solutionForm.setFieldValue(['solution_items', key, 'text'], undefined);
|
||||
solutionForm.setFieldValue(['solution_items', key, 'status'], undefined);
|
||||
solutionForm.setFieldValue(['solution_items', key, 'file'], undefined);
|
||||
solutionForm.setFieldValue(['solution_items', key, 'fileUpload'], undefined);
|
||||
} catch (error) {
|
||||
}
|
||||
}, 50);
|
||||
};
|
||||
|
||||
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}`] || {};
|
||||
setTimeout(() => {
|
||||
const fieldName = ['solution_items', key];
|
||||
const currentSolutionData = solutionForm.getFieldsValue([fieldName]) || {};
|
||||
const solutionData = currentSolutionData[`solution_items,${key}`] || currentSolutionData[`solution_items.${key}`] || {};
|
||||
|
||||
if (value === 'text') {
|
||||
const updatedSolutionData = {
|
||||
...solutionData,
|
||||
fileUpload: null,
|
||||
file: null
|
||||
file: null,
|
||||
text: solutionData.text || `Deskripsi untuk ${solutionData.name || 'Solution'}`
|
||||
};
|
||||
|
||||
// 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);
|
||||
}
|
||||
solutionForm.setFieldValue([...fieldName, 'text'], updatedSolutionData.text);
|
||||
} else if (value === 'file') {
|
||||
const updatedSolutionData = {
|
||||
...solutionData,
|
||||
text: '',
|
||||
fileUpload: null,
|
||||
file: null
|
||||
};
|
||||
solutionForm.setFieldValue([...fieldName, 'text'], '');
|
||||
solutionForm.setFieldValue([...fieldName, 'fileUpload'], null);
|
||||
solutionForm.setFieldValue([...fieldName, 'file'], null);
|
||||
}
|
||||
}, 0);
|
||||
};
|
||||
|
||||
const handleSolutionStatusChange = (key, value) => {
|
||||
@@ -71,14 +141,31 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
setSolutionTypes({ 0: 'text' });
|
||||
setSolutionStatuses({ 0: true });
|
||||
|
||||
// Reset form values
|
||||
solutionForm.resetFields();
|
||||
solutionForm.setFieldsValue({
|
||||
solution_items: [{
|
||||
status: true,
|
||||
type: 'text',
|
||||
}]
|
||||
});
|
||||
setTimeout(() => {
|
||||
solutionForm.setFieldsValue({
|
||||
solution_items: {
|
||||
0: {
|
||||
name: 'Solution 1',
|
||||
status: true,
|
||||
type: 'text',
|
||||
text: '',
|
||||
file: null,
|
||||
fileUpload: null
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
solutionForm.setFieldValue(['solution_items', 0, 'name'], 'Solution 1');
|
||||
solutionForm.setFieldValue(['solution_items', 0, 'type'], 'text');
|
||||
solutionForm.setFieldValue(['solution_items', 0, 'text'], 'Deskripsi untuk Solution 1');
|
||||
solutionForm.setFieldValue(['solution_items', 0, 'status'], true);
|
||||
solutionForm.setFieldValue(['solution_items', 0, 'file'], null);
|
||||
solutionForm.setFieldValue(['solution_items', 0, 'fileUpload'], null);
|
||||
|
||||
console.log('✅ Reset solution fields with proper structure');
|
||||
console.log('Form values after reset:', solutionForm.getFieldsValue(true));
|
||||
}, 100);
|
||||
};
|
||||
|
||||
const checkFirstSolutionValid = () => {
|
||||
@@ -107,76 +194,121 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
};
|
||||
|
||||
const getSolutionData = () => {
|
||||
const values = solutionForm.getFieldsValue(true);
|
||||
try {
|
||||
const values = solutionForm.getFieldsValue(true);
|
||||
const result = [];
|
||||
|
||||
const result = solutionFields.map(key => {
|
||||
let solution = null;
|
||||
solutionFields.forEach(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];
|
||||
try {
|
||||
solution = solutionForm.getFieldValue(['solution_items', key]);
|
||||
} catch (error) {
|
||||
// Silently handle errors
|
||||
}
|
||||
}
|
||||
|
||||
if (!solution) return null;
|
||||
|
||||
const validSolution = solution.name && solution.name.trim() !== '';
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
let typeSolution = solutionTypes[key] || solution.type || 'text';
|
||||
|
||||
if (typeSolution === 'file') {
|
||||
if (fileObject && fileObject.type_solution) {
|
||||
typeSolution = fileObject.type_solution;
|
||||
} else {
|
||||
typeSolution = 'image';
|
||||
if (!solution && values.solution_items && values.solution_items[key]) {
|
||||
solution = values.solution_items[key];
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
if (!solution) {
|
||||
const commaKey = `solution_items,${key}`;
|
||||
solution = values[commaKey];
|
||||
}
|
||||
|
||||
return result;
|
||||
if (!solution) {
|
||||
const dotKey = `solution_items.${key}`;
|
||||
solution = values[dotKey];
|
||||
}
|
||||
|
||||
if (!solution) {
|
||||
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) {
|
||||
// Try to find the solution in the raw form structure
|
||||
const rawValues = solutionForm.getFieldsValue();
|
||||
|
||||
if (rawValues.solution_items && rawValues.solution_items[key]) {
|
||||
solution = rawValues.solution_items[key];
|
||||
}
|
||||
}
|
||||
|
||||
if (!solution) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const hasName = solution.name && solution.name.trim() !== '';
|
||||
|
||||
if (!hasName) {
|
||||
return;
|
||||
}
|
||||
|
||||
const solutionType = solutionTypes[key] || solution.type || 'text';
|
||||
let isValidType = true;
|
||||
|
||||
if (solutionType === 'text') {
|
||||
isValidType = solution.text && solution.text.trim() !== '';
|
||||
if (!isValidType) {
|
||||
return;
|
||||
}
|
||||
} else if (solutionType === 'file') {
|
||||
const hasPathSolution = solution.path_solution && solution.path_solution.trim() !== '';
|
||||
const hasFileUpload = (solution.fileUpload && typeof solution.fileUpload === 'object' && Object.keys(solution.fileUpload).length > 0);
|
||||
const hasFile = (solution.file && typeof solution.file === 'object' && Object.keys(solution.file).length > 0);
|
||||
|
||||
isValidType = hasPathSolution || hasFileUpload || hasFile;
|
||||
if (!isValidType) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
let typeSolution = solutionTypes[key] || solution.type || 'text';
|
||||
|
||||
if (typeSolution === 'file') {
|
||||
if (fileObject && fileObject.type_solution) {
|
||||
typeSolution = fileObject.type_solution;
|
||||
} else {
|
||||
typeSolution = 'image';
|
||||
}
|
||||
}
|
||||
|
||||
const finalSolution = {
|
||||
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),
|
||||
};
|
||||
|
||||
result.push(finalSolution);
|
||||
});
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const setSolutionsForExistingRecord = (solutions, form) => {
|
||||
@@ -193,8 +325,6 @@ 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}`;
|
||||
@@ -207,10 +337,8 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
isExisting: true,
|
||||
size: 0,
|
||||
type: solution.type_solution === 'pdf' ? 'application/pdf' : 'image/jpeg',
|
||||
fileExtension: solution.type_solution === 'pdf' ? 'pdf' :
|
||||
(fileName.split('.').pop().toLowerCase() || 'jpg')
|
||||
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;
|
||||
@@ -227,13 +355,13 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
newStatuses[key] = solution.is_active !== false;
|
||||
});
|
||||
|
||||
console.log('🔧 Final solutions values:', solutionsValues);
|
||||
console.log('🔧 Final solution types:', newTypes);
|
||||
const nestedFormValues = {
|
||||
solution_items: {}
|
||||
};
|
||||
|
||||
const formValues = {};
|
||||
Object.keys(solutionsValues).forEach(key => {
|
||||
const solution = solutionsValues[key];
|
||||
formValues[`solution_items,${key}`] = {
|
||||
nestedFormValues.solution_items[key] = {
|
||||
name: solution.name,
|
||||
type: solution.type,
|
||||
text: solution.text,
|
||||
@@ -243,8 +371,40 @@ export const useSolutionLogic = (solutionForm) => {
|
||||
};
|
||||
});
|
||||
|
||||
console.log('🔧 Setting form values:', formValues);
|
||||
form.setFieldsValue(formValues);
|
||||
form.setFieldsValue(nestedFormValues);
|
||||
|
||||
const fallbackFormValues = {};
|
||||
Object.keys(solutionsValues).forEach(key => {
|
||||
const solution = solutionsValues[key];
|
||||
fallbackFormValues[`solution_items,${key}`] = {
|
||||
name: solution.name,
|
||||
type: solution.type,
|
||||
text: solution.text,
|
||||
file: solution.file,
|
||||
fileUpload: solution.fileUpload,
|
||||
status: solution.status
|
||||
};
|
||||
});
|
||||
|
||||
form.setFieldsValue(fallbackFormValues);
|
||||
|
||||
Object.keys(solutionsValues).forEach(key => {
|
||||
const solution = solutionsValues[key];
|
||||
form.setFieldValue([`solution_items,${key}`, 'name'], solution.name);
|
||||
form.setFieldValue([`solution_items,${key}`, 'type'], solution.type);
|
||||
form.setFieldValue([`solution_items,${key}`, 'text'], solution.text);
|
||||
form.setFieldValue([`solution_items,${key}`, 'file'], solution.file);
|
||||
form.setFieldValue([`solution_items,${key}`, 'fileUpload'], solution.fileUpload);
|
||||
form.setFieldValue([`solution_items,${key}`, 'status'], solution.status);
|
||||
|
||||
form.setFieldValue(['solution_items', key, 'name'], solution.name);
|
||||
form.setFieldValue(['solution_items', key, 'type'], solution.type);
|
||||
form.setFieldValue(['solution_items', key, 'text'], solution.text);
|
||||
form.setFieldValue(['solution_items', key, 'file'], solution.file);
|
||||
form.setFieldValue(['solution_items', key, 'fileUpload'], solution.fileUpload);
|
||||
form.setFieldValue(['solution_items', key, 'status'], solution.status);
|
||||
});
|
||||
|
||||
setSolutionTypes(newTypes);
|
||||
setSolutionStatuses(newStatuses);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user