import React, { Component, PropTypes } from 'react'; import radium from 'radium'; import componentStyles from './styles'; import Button from '../../../buttons/Button'; import Dropdown from '../../fields/Dropdown'; import InputField from '../../fields/InputField'; import isMacOS from '../../../../utilities/isMacOS'; import validatePresence from '../../validators/validate_presence'; const RUN_TYPES = { RUN: 'RUN', RUN_AND_SAVE: 'RUN_AND_SAVE', SAVE: 'SAVE', }; class SaveQueryForm extends Component { static propTypes = { onSubmit: PropTypes.func, saveQuery: PropTypes.bool, }; constructor (props) { super(props); this.state = { errors: { name: null, description: null, duration: null, platforms: null, hosts: null, hostsPercentage: null, scanInterval: null, }, formData: { name: null, description: null, duration: 'short', platforms: 'all', hosts: 'all', hostsPercentage: null, scanInterval: 0, }, showMoreOptions: false, }; } componentWillMount () { global.window.addEventListener('keydown', this.handleKeydown); } componentWillUnmount () { global.window.removeEventListener('keydown', this.handleKeydown); } onFieldChange = (fieldName) => { return ({ target }) => { const { errors, formData } = this.state; this.setState({ errors: { ...errors, [fieldName]: null, }, formData: { ...formData, [fieldName]: target.value, }, }); }; } onFormSubmit = (runType) => { return (evt) => { if (evt) { evt.preventDefault(); } const { formData } = this.state; const { onSubmit } = this.props; const { validate } = this; if (validate(runType)) { return onSubmit({ ...formData, runType }); } return false; }; } handleKeydown = (evt) => { const { metaKey, code } = evt; const { onFormSubmit } = this; const { RUN_AND_SAVE, RUN } = RUN_TYPES; const { saveQuery } = this.props; const runType = saveQuery ? RUN_AND_SAVE : RUN; if (metaKey && code === 'Enter') { return onFormSubmit(runType)(); } return false; }; validate = (runType) => { const { errors, formData: { name }, } = this.state; const { RUN } = RUN_TYPES; if (runType === RUN) { return true; } if (!validatePresence(name)) { this.setState({ errors: { ...errors, name: 'Query Name field must be completed', }, }); return false; } return true; } toggleShowMoreOptions = () => { const { showMoreOptions } = this.state; this.setState({ showMoreOptions: !showMoreOptions, }); return false; }; renderMoreOptionsCtaSection = () => { const { moreOptionsIconStyles, moreOptionsCtaSectionStyles, moreOptionsTextStyles } = componentStyles; const { showMoreOptions } = this.state; const { toggleShowMoreOptions } = this; if (showMoreOptions) { return (