<commit_before>import React from 'react';
import { Button, Alert, Input } from 'react-bootstrap';
import { reduxForm, change, focus } from 'redux-form';
import { connect } from 'react-redux';
import moment from 'moment';


import { fetchContainers } from '../../actions/containers';
import { add, remove, edit } from '../../actions/items';
import { hideItemForm } from '../../actions/itemForm';
import { ModalTypes } from '../../constants/ActionTypes';
import { NoOp } from '../common/common';
import FormModal from '../common/FormModal';
import Datepicker from '../common/Datepicker';
import ValidatedInput from '../common/ValidatedInput';

//// utils
import { getDisplayFormat, isValidDate, momentFormats } from '../../util/DateUtils';

const fields = ['category', 'name', 'measurement', 'quantity', 'date', 'container', 'id', 'keepOpen'];

const formName = 'item';

const validate = values => {
	const errors = {};
  	//// required fields	
	['category', 'name'].map(field => {
		if (!values[field] || values[field] === '') {
			errors[field] = `${field} is required`;
		} 
	});
	if (values.date !== '' && !isValidDate(values.date)) {
		errors.date = `Invalid date: valid formats are ${momentFormats.join(', ')}`;
	}

	return errors;
};

const submit = (submitAction) => (values, dispatch) => {
  return new Promise((resolve, reject) => {
	dispatch(submitAction(values));	
	dispatch(fetchContainers());
	resolve();
  });
};


const ItemForm = ({ containerId, serverErrors, showModal, onHide, readOnly, submitAction, title,
			type, submitButtonBsStyle,
			fields: { category, name, measurement, quantity, date, container, id, keepOpen },
	      handleSubmit,
	      resetForm,
	      submitting }) => {
	const submit  = (submitAction) => (values, dispatch) => {
	  return new Promise((resolve, reject) => {
		dispatch(submitAction(values));	

		resetForm();
		if (values.keepOpen) {
			dispatch(change(formName, 'keepOpen', true));
			const category = document.getElementById('category');
			category.focus();
		} else {
			onHide();	
		}
		resolve();
	  });		
	};
	return (<FormModal title={title} valid={true} show={showModal} errors={serverErrors} submitButtonBsStyle={submitButtonBsStyle}
		onSubmit={handleSubmit(submit(submitAction))} onHide={() => {
			resetForm(); 
			onHide();
		}}>
		<ValidatedInput type='text' autoFocus id='category' readOnly={readOnly} label='Category' {...category} />
		<ValidatedInput type='text' id='name' label='Name' readOnly={readOnly} {...name} />
		<ValidatedInput type='text' id='quantity' label='Quantity' readOnly={readOnly} {...quantity} />
		<ValidatedInput type='text' id='measurement' label='Measurement' readOnly={readOnly} {...measurement} />
		<Datepicker id='date' label='Date' readOnly={readOnly} {...date} /> 
		<input type='hidden' id='container' value={containerId} {...container} />
		{ ModalTypes.CREATE === type && <Input type='checkbox' id='keepOpen' label='Keep Open' {...keepOpen} /> }

		{ [ModalTypes.EDIT, ModalTypes.DELETE].indexOf(type) > 0 && <input type='hidden' id='id' {...id} /> }

	</FormModal>)
};
//, addForm: { show : showAddForm }
const mapStateToProps = ({ containers: { selected }, 
		itemForm: { errors, show, selected: selectedItem } }) => {
	const containerId = selected && selected.id ? selected.id : -1;
	let submitAction, title, submitButtonBsStyle;
	switch (show) {
		case ModalTypes.DELETE:
			submitAction = remove;
			title = 'Delete';
			submitButtonBsStyle='danger';
			break;
		case ModalTypes.EDIT:
			submitAction = edit;
			title = 'Edit';
			submitButtonBsStyle='primary';
			break;
		case ModalTypes.CREATE:
			submitAction = add;
			title = 'Add';
			submitButtonBsStyle='success';
			break;			
		case ModalTypes.NONE:
			//// do nothing<commit_msg>fix formatting of keep open checkbox<commit_after>import React from 'react';
import { Button, Alert, Input } from 'react-bootstrap';
import { reduxForm, change, focus } from 'redux-form';
import { connect } from 'react-redux';
import moment from 'moment';


import { fetchContainers } from '../../actions/containers';
import { add, remove, edit } from '../../actions/items';
import { hideItemForm } from '../../actions/itemForm';
import { ModalTypes } from '../../constants/ActionTypes';
import { NoOp } from '../common/common';
import FormModal from '../common/FormModal';
import Datepicker from '../common/Datepicker';
import ValidatedInput from '../common/ValidatedInput';

//// utils
import { getDisplayFormat, isValidDate, momentFormats } from '../../util/DateUtils';

const fields = ['category', 'name', 'measurement', 'quantity', 'date', 'container', 'id', 'keepOpen'];

const formName = 'item';

const validate = values => {
	const errors = {};
  	//// required fields	
	['category', 'name'].map(field => {
		if (!values[field] || values[field] === '') {
			errors[field] = `${field} is required`;
		} 
	});
	if (values.date !== '' && !isValidDate(values.date)) {
		errors.date = `Invalid date: valid formats are ${momentFormats.join(', ')}`;
	}

	return errors;
};

const submit = (submitAction) => (values, dispatch) => {
  return new Promise((resolve, reject) => {
	dispatch(submitAction(values));	
	dispatch(fetchContainers());
	resolve();
  });
};


const ItemForm = ({ containerId, serverErrors, showModal, onHide, readOnly, submitAction, title,
			type, submitButtonBsStyle,
			fields: { category, name, measurement, quantity, date, container, id, keepOpen },
	      handleSubmit,
	      resetForm,
	      submitting }) => {
	const submit  = (submitAction) => (values, dispatch) => {
	  return new Promise((resolve, reject) => {
		dispatch(submitAction(values));	

		resetForm();
		if (values.keepOpen) {
			dispatch(change(formName, 'keepOpen', true));
			const category = document.getElementById('category');
			category.focus();
		} else {
			onHide();	
		}
		resolve();
	  });		
	};
	return (<FormModal title={title} valid={true} show={showModal} errors={serverErrors} submitButtonBsStyle={submitButtonBsStyle}
		onSubmit={handleSubmit(submit(submitAction))} onHide={() => {
			resetForm(); 
			onHide();
		}}>
		<ValidatedInput type='text' autoFocus id='category' readOnly={readOnly} label='Category' {...category} />
		<ValidatedInput type='text' id='name' label='Name' readOnly={readOnly} {...name} />
		<ValidatedInput type='text' id='quantity' label='Quantity' readOnly={readOnly} {...quantity} />
		<ValidatedInput type='text' id='measurement' label='Measurement' readOnly={readOnly} {...measurement} />
		<Datepicker id='date' label='Date' readOnly={readOnly} {...date} /> 
		<input type='hidden' id='container' value={containerId} {...container} />
		{ ModalTypes.CREATE === type && <Input type='checkbox' wrapperClassName='col-xs-offset-1' id='keepOpen' label='Keep Open' {...keepOpen} /> }

		{ [ModalTypes.EDIT, ModalTypes.DELETE].indexOf(type) > 0 && <input type='hidden' id='id' {...id} /> }

	</FormModal>)
};
//, addForm: { show : showAddForm }
const mapStateToProps = ({ containers: { selected }, 
		itemForm: { errors, show, selected: selectedItem } }) => {
	const containerId = selected && selected.id ? selected.id : -1;
	let submitAction, title, submitButtonBsStyle;
	switch (show) {
		case ModalTypes.DELETE:
			submitAction = remove;
			title = 'Delete';
			submitButtonBsStyle='danger';
			break;
		case ModalTypes.EDIT:
			submitAction = edit;
			title = 'Edit';
			submitButtonBsStyle='primary';
			break;
		case ModalTypes.CREATE:
			submitAction = add;
			title = 'Add';
			submitButtonBsStyle='success';
			break;			
		case ModalTypes.NONE:
			//// do nothing
{'fraction_non_alphanumeric': 0.17897802488747683, 'fraction_numerical': 0.0006619009796134498, 'mean_word_length': 4.320422535211268, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1877541', 'n_tokens_mistral': 2469, 'n_tokens_neox': 2286, 'n_words': 561}