import React, { useState, useEffect } from 'react'; import PropTypes from 'prop-types'; import Select from 'react-select'; import { CForm, CInput, CLabel, CCol, CFormGroup, CInvalidFeedback, CFormText, CRow, CTextarea, } from '@coreui/react'; import { CopyToClipboardButton } from 'ucentral-libs'; const EditDefaultConfigurationForm = ({ t, disable, fields, updateField, updateFieldWithKey, deviceTypes, editing, }) => { const [typeOptions, setTypeOptions] = useState([]); const [chosenTypes, setChosenTypes] = useState([]); const parseOptions = () => { const options = [{ value: '*', label: 'All' }]; const newOptions = deviceTypes.map((option) => ({ value: option, label: option, })); options.push(...newOptions); setTypeOptions(options); setChosenTypes([]); const newChosenTypes = fields.modelIds.value.map((dType) => ({ value: dType, label: dType === '*' ? 'All' : dType, })); setChosenTypes(newChosenTypes); }; const typeOnChange = (chosenArray) => { const allIndex = chosenArray.findIndex((el) => el.value === '*'); // If the All option was chosen before, we take it out of the array if (allIndex === 0 && chosenTypes.length > 0) { const newResults = chosenArray.slice(1); setChosenTypes(newResults); updateFieldWithKey('modelIds', { value: newResults.map((el) => el.value), error: false, notEmpty: true, }); } else if (allIndex > 0) { setChosenTypes([{ value: '*', label: 'All' }]); updateFieldWithKey('modelIds', { value: ['*'], error: false, notEmpty: true }); } else if (chosenArray.length > 0) { setChosenTypes(chosenArray); updateFieldWithKey('modelIds', { value: chosenArray.map((el) => el.value), error: false, notEmpty: true, }); } else { setChosenTypes([]); updateFieldWithKey('modelIds', { value: [], error: false, notEmpty: true }); } }; useEffect(() => { parseOptions(); }, [deviceTypes, fields.name.value]); return ( {t('user.name')} {fields.name.value} {t('user.description')} {t('common.required')}
{t('configuration.supported_device_types')}: