import React, { useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { FormGroup, TextInput, TextArea } from '@patternfly/react-core';
import PropTypes from 'prop-types';
import SearchBar from 'foremanReact/components/SearchBar';
import { getControllerSearchProps } from 'foremanReact/constants';
import { TRIGGERS } from 'foremanReact/components/AutoComplete/AutoCompleteConstants';
import { getResults } from 'foremanReact/components/AutoComplete/AutoCompleteActions';
import { helpLabel } from './FormHelpers';
import { SelectField } from './SelectField';
import { ResourceSelectAPI } from './ResourceSelect';
import { noop } from '../../../helpers';
const TemplateSearchField = ({
name,
controller,
url,
labelText,
required,
defaultValue,
setValue,
values,
}) => {
const searchQuery = useSelector(
state => state.autocomplete?.[name]?.searchQuery
);
const dispatch = useDispatch();
useEffect(() => {
setValue({ ...values, [name]: searchQuery });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [searchQuery]);
const id = name.replace(/ /g, '-');
const props = getControllerSearchProps(controller.replace('/', '_'), name);
const setSearch = newSearchQuery => {
dispatch(
getResults({
url,
searchQuery: newSearchQuery,
controller,
trigger: TRIGGERS.INPUT_CHANGE,
id: name,
})
);
};
return (
setSearch(search)}
/>
);
};
export const formatter = (input, values, setValue) => {
const isSelectType = !!input?.options;
const inputType = input.value_type;
const isTextType = inputType === 'plain' || !inputType; // null defaults to plain
const {
name,
required,
hidden_value: hidden,
resource_type: resourceType,
value_type: valueType,
} = input;
const labelText = input.description;
const value = values[name];
const id = name.replace(/ /g, '-');
if (valueType === 'resource') {
return (
setValue({ ...values, [name]: newValue })}
/>
);
}
if (isSelectType) {
const options = input.options.split(/\r?\n/).map(option => option.trim());
return (
setValue({ ...values, [name]: newValue })}
/>
);
}
if (isTextType) {
return (
);
}
if (inputType === 'date') {
return (
setValue({ ...values, [name]: newValue })}
/>
);
}
if (inputType === 'search') {
const controller = input.resource_type_tableize;
return (
);
}
return null;
};
TemplateSearchField.propTypes = {
name: PropTypes.string.isRequired,
controller: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
labelText: PropTypes.string,
required: PropTypes.bool.isRequired,
defaultValue: PropTypes.string,
setValue: PropTypes.func.isRequired,
values: PropTypes.object.isRequired,
};
TemplateSearchField.defaultProps = {
labelText: null,
defaultValue: '',
};