insomnia/app/containers/Prompts.js

101 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-04-10 02:58:48 +00:00
import React, {Component, PropTypes} from 'react'
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
2016-04-23 06:16:23 +00:00
import * as ModalActions from '../redux/modules/modals'
import * as RequestGroupActions from '../redux/modules/requestGroups'
import * as RequestActions from '../redux/modules/requests'
import PromptModal from '../components/base/PromptModal'
2016-04-10 02:58:48 +00:00
2016-04-23 06:16:23 +00:00
import * as db from '../database'
import {MODAL_REQUEST_RENAME, MODAL_REQUEST_GROUP_RENAME} from '../lib/constants';
2016-04-16 23:24:57 +00:00
2016-04-15 05:23:54 +00:00
class Prompts extends Component {
2016-04-10 02:58:48 +00:00
constructor (props) {
super(props);
this._prompts = {};
2016-04-23 06:08:52 +00:00
this._prompts[MODAL_REQUEST_RENAME] = {
2016-04-10 02:58:48 +00:00
header: 'Rename Request',
submit: 'Rename',
onSubmit: (modal, name) => {
2016-04-16 23:24:57 +00:00
db.update(modal.data.request, {name})
2016-04-10 02:58:48 +00:00
}
};
2016-04-23 06:08:52 +00:00
this._prompts[MODAL_REQUEST_GROUP_RENAME] = {
2016-04-10 02:58:48 +00:00
header: 'Rename Request Group',
submit: 'Rename',
onSubmit: (modal, name) => {
2016-04-16 23:24:57 +00:00
db.update(modal.data.requestGroup, {name})
2016-04-10 02:58:48 +00:00
}
};
}
render () {
const {modals, actions} = this.props;
return (
2016-04-15 05:23:54 +00:00
<div ref="prompts">
{Object.keys(this._prompts).map(id => {
const promptDef = this._prompts[id];
const modal = modals.find(m => m.id === id);
2016-04-10 02:58:48 +00:00
2016-04-15 05:23:54 +00:00
if (!modal) {
return null;
}
2016-04-10 02:58:48 +00:00
2016-04-15 05:23:54 +00:00
return (
<PromptModal
key={id}
id={id}
headerName={promptDef.header}
submitName={promptDef.submit}
defaultValue={modal.data.defaultValue}
2016-04-16 23:24:57 +00:00
onClose={() => actions.modals.hide(modal.id)}
2016-04-15 05:23:54 +00:00
onSubmit={value => promptDef.onSubmit(modal, value)}
/>
)
})}
2016-04-10 02:58:48 +00:00
</div>
);
}
}
2016-04-15 05:23:54 +00:00
Prompts.propTypes = {
2016-04-10 02:58:48 +00:00
actions: PropTypes.shape({
2016-04-16 23:24:57 +00:00
modals: PropTypes.shape({
hide: PropTypes.func.isRequired
}),
requestGroups: PropTypes.shape({
update: PropTypes.func.isRequired
}),
requests: PropTypes.shape({
update: PropTypes.func.isRequired
})
2016-04-10 02:58:48 +00:00
}),
modals: PropTypes.array.isRequired
};
function mapStateToProps (state) {
return {
actions: state.actions,
modals: state.modals
};
}
function mapDispatchToProps (dispatch) {
return {
2016-04-16 23:24:57 +00:00
actions: {
requests: bindActionCreators(RequestActions, dispatch),
modals: bindActionCreators(ModalActions, dispatch),
requestGroups: bindActionCreators(RequestGroupActions, dispatch)
}
2016-04-10 02:58:48 +00:00
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
2016-04-15 05:23:54 +00:00
)(Prompts);
2016-04-10 02:58:48 +00:00