Spaces:
Runtime error
Runtime error
File size: 5,432 Bytes
56b6519 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
/* eslint-disable import/extensions */
import { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useParams } from 'react-router-dom';
import { toast } from 'sonner';
import {
AuditSectionById,
getAuditById,
getAuditSectionById,
getLanguages,
updateAuditSectionById,
} from '@/services/audits';
import AuditCustomFields from './AuditCustomFields';
type LanguageData = {
language: string;
locale: string;
};
type ListItem = {
id: number;
value: string;
label?: string;
icon?: string;
};
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;
};
export const Sections = () => {
const { t } = useTranslation();
const [currentCustomFields, setCurrentCustomFields] = useState<
TransformedField[]
>([]);
const [languagesList, setLanguagesList] = useState<ListItem[]>([]);
const [auditLanguage, setAuditLanguage] = useState<string | null>(null);
const [currentLanguage, setCurrentLanguage] = useState<ListItem | null>(null);
const [dataAuditSection, setDataAuditSection] = useState<AuditSectionById>();
const auditId = useParams().auditId;
const sectionId = useParams().sectionId;
const fetchLanguages = async () => {
try {
const dataLanguage = await getLanguages();
const languageNames = dataLanguage.datas.map(
(item: LanguageData, index: number) => ({
id: index,
value: item.locale,
label: item.language,
}),
);
setLanguagesList(languageNames);
} catch (error) {
console.error('Error:', error);
}
};
const fetchAudit = useCallback(async () => {
try {
const audit = await getAuditById(auditId);
setAuditLanguage(audit.datas.language);
} catch (error) {
console.error('Error:', error);
}
}, [auditId]);
const fetchAuditSections = useCallback(async () => {
try {
const auditSectionResponse = await getAuditSectionById(
auditId,
sectionId,
);
setDataAuditSection(auditSectionResponse.datas);
} catch (error) {
console.error('Error:', error);
}
}, [auditId, sectionId]);
useEffect(() => {
if (dataAuditSection && currentLanguage) {
const transformedFields = dataAuditSection.customFields.map(field => ({
description: field.customField.description,
display: field.customField.display,
displaySub: field.customField.displaySub,
fieldType: field.customField.fieldType,
label: field.customField.label,
offset: field.customField.offset,
text: Array.isArray(field.text)
? field.text.map(textItem => ({
locale: currentLanguage.value,
value: textItem,
}))
: [{ locale: currentLanguage.value, value: field.text }],
options: field.customField.options.map(optionItem => ({
locale: optionItem.locale,
value: optionItem.value,
})),
required: field.customField.required,
size: field.customField.size,
_id: field.customField._id,
}));
setCurrentCustomFields(transformedFields);
}
}, [dataAuditSection, currentLanguage]);
useEffect(() => {
void fetchLanguages();
}, []);
useEffect(() => {
void fetchAudit();
}, [fetchAudit]);
useEffect(() => {
if (currentLanguage) {
void fetchAuditSections();
}
}, [currentLanguage, fetchAuditSections]);
useEffect(() => {
if (auditLanguage && languagesList.length > 0) {
const matchedLanguage = languagesList.find(
language => language.value === auditLanguage,
);
if (matchedLanguage) {
setCurrentLanguage(matchedLanguage);
}
}
}, [auditLanguage, languagesList]);
const handleSave = async () => {
if (!dataAuditSection) {
return;
}
const transformedFields = currentCustomFields.map(field => {
const { text, ...customField } = field;
const textValues = text.map(item => item.value).flat();
let formattedText;
if (
customField.fieldType === 'checkbox' ||
customField.fieldType === 'select-multiple'
) {
formattedText = textValues;
} else if (textValues.length === 1) {
formattedText = textValues[0];
} else {
formattedText = textValues;
}
return {
customField,
text: formattedText,
};
});
const payload = {
customFields: transformedFields,
field: dataAuditSection.field,
name: dataAuditSection.name,
_id: dataAuditSection._id,
};
try {
await updateAuditSectionById(auditId, sectionId, payload);
toast.success(t('msg.auditCustomSectionsSaveSuccess'));
} catch (error) {
console.error(error);
toast.error(t('err.errorSavingAuditCustomSections'));
}
};
return (
<div className="p-4">
{currentLanguage ? (
<AuditCustomFields
currentLanguage={currentLanguage}
fields={currentCustomFields}
onSave={handleSave}
setCurrentCustomFields={setCurrentCustomFields}
/>
) : null}
</div>
);
};
|