import _ from 'lodash'; import PropTypes from 'prop-types'; import React from 'react'; import { AutoComplete, Button, Col, Form, Icon, Input, Row, Select } from 'antd'; import { defaultMaxRps, httpMethods, tapQueryPropType } from './util/TapUtils.jsx'; const colSpan = 5; const rowGutter = 16; const getResourceList = (resourcesByNs, ns) => { return resourcesByNs[ns] || _.uniq(_.flatten(_.values(resourcesByNs))); }; export default class TapQueryForm extends React.Component { static propTypes = { enableAdvancedForm: PropTypes.bool, handleTapStart: PropTypes.func.isRequired, handleTapStop: PropTypes.func.isRequired, query: tapQueryPropType.isRequired, tapRequestInProgress: PropTypes.bool.isRequired, updateQuery: PropTypes.func.isRequired } static defaultProps = { enableAdvancedForm: true } constructor(props) { super(props); this.state = { authoritiesByNs: {}, resourcesByNs: {}, showAdvancedForm: false, query: props.query, autocomplete: { namespace: [], resource: [], toNamespace: [], toResource: [], authority: [] }, }; } static getDerivedStateFromProps(props, state) { if (!_.isEqual(props.resourcesByNs, state.resourcesByNs)) { let resourcesByNs = props.resourcesByNs; let authoritiesByNs = props.authoritiesByNs; let namespaces = _.sortBy(_.keys(resourcesByNs)); let resourceNames = getResourceList(resourcesByNs, state.query.namespace); let toResourceNames = getResourceList(resourcesByNs, state.query.toNamespace); let authorities = getResourceList(authoritiesByNs, state.query.namespace); return _.merge(state, { resourcesByNs, authoritiesByNs, autocomplete: { namespace: namespaces, resource: resourceNames, toNamespace: namespaces, toResource: toResourceNames, authority: authorities } }); } else { return null; } } toggleAdvancedForm = show => { this.setState({ showAdvancedForm: show }); } handleFormChange = (name, scopeResource, shouldScopeAuthority) => { let state = { query: this.state.query, autocomplete: this.state.autocomplete }; return formVal => { state.query[name] = formVal; if (!_.isNil(scopeResource)) { // scope the available typeahead resources to the selected namespace state.autocomplete[scopeResource] = this.state.resourcesByNs[formVal]; } if (shouldScopeAuthority) { state.autocomplete.authority = this.state.authoritiesByNs[formVal]; } this.setState(state); this.props.updateQuery(state.query); }; } handleFormEvent = name => { let state = { query: this.state.query }; return event => { state.query[name] = event.target.value; this.setState(state); this.props.updateQuery(state.query); }; } autoCompleteData = name => { return _(this.state.autocomplete[name]) .filter(d => d.indexOf(this.state.query[name]) !== -1) .sortBy() .value(); } renderAdvancedTapForm = () => { return ( ); } render() { return (
{ this.props.tapRequestInProgress ? : } { !this.props.enableAdvancedForm ? null : { !this.state.showAdvancedForm ? null : this.renderAdvancedTapForm() } }
); } }