/* eslint-disable import/extensions */ import { t } from 'i18next'; import DefaultRadioGroup from '@/components/button/DefaultRadioGroup'; import SimpleInput from '@/components/input/SimpleInput'; import RichText from '@/components/text/RichText'; import { CheckboxButtonCustom, DayPickerCustom, MultiSelectDropdownCustom, SelectDropdownCustom, } from '@/routes/data/CustomData/custom-fields/customComponents'; type TransformedField = { description: string; display: string; displaySub: string; fieldType: string; label: string; offset: number; text: { locale: string; value: string; }[]; options: { locale: string; value: string; }[]; required: boolean; size: number; _id: string; }; type ListItem = { id: number; value: string; label?: string; locale?: string; }; type AuditCustomFieldsProps = { fields: TransformedField[]; currentLanguage: ListItem; // es necesaria una refactorización de los componentes // se hará un issue al respecto // eslint-disable-next-line @typescript-eslint/no-explicit-any setCurrentCustomFields: (fields: any) => void; onSave: () => void; }; const AuditCustomFields: React.FC = ({ fields, currentLanguage, setCurrentCustomFields, onSave, }) => { const handlerInputChangeText = (id: string, value: string) => { setCurrentCustomFields((prevFields: TransformedField[]) => { return prevFields.map((field: TransformedField) => field._id === id ? { ...field, text: field.text.map(item => item.locale === currentLanguage.value ? { locale: item.locale, value } : item, ), } : field, ); }); }; const handlerTextString = ( text: { locale: string; value: string | string[] }[], ) => { return Array.isArray(text[0].value) ? text[0].value[0] : text[0].value; }; const handlerTextArray = ( text: { locale: string; value: string | string[]; }[], ) => { return text .filter(item => item.locale === currentLanguage.value) .flatMap(item => (Array.isArray(item.value) ? item.value : [item.value])); }; return (
{fields.map(customField => { const { _id, fieldType, size, offset, options, label, text } = customField; const validSize = Math.min(12, Math.max(0, size)); const sizeStyle = validSize > 0 ? Math.round((validSize / 12) * 100) : Math.round((1 / 12) * 100); return (
{label} {t('offset')}: {offset}
{(() => { switch (fieldType) { case 'checkbox': return ( option.value)} setCurrentCustomFields={setCurrentCustomFields} text={handlerTextArray(text)} /> ); case 'space': return (
); case 'text': return ( handlerInputChangeText(_id, value) } placeholder="" value={handlerTextString(text)} /> ); case 'input': return ( handlerInputChangeText(_id, value) } placeholder="" type="text" value={handlerTextString(text)} /> ); case 'radio': return ( handlerInputChangeText(_id, value) } options={options.map((option, index) => ({ id: index.toString(), label: option.value, value: option.value, }))} value={handlerTextString(text)} /> ); case 'select': return ( ({ id: index, value: option.value, label: option.value, }))} placeholder="Select" setCurrentCustomFields={setCurrentCustomFields} text={handlerTextString(text)} title="" /> ); case 'select-multiple': return ( ({ id: index, value: option.value, label: option.value, }))} placeholder="Select" setCurrentCustomFields={setCurrentCustomFields} text={handlerTextArray(text)} title="" /> ); case 'date': return ( ); default: return null; } })()}
); })}
); }; export default AuditCustomFields;