import fs from 'fs' import electron from 'electron' import React, {Component, PropTypes} from 'react' import {bindActionCreators} from 'redux' import {connect} from 'react-redux' import {MODAL_SETTINGS} from '../lib/constants' import Dropdown from '../components/base/Dropdown' import DropdownDivider from '../components/base/DropdownDivider' import * as RequestGroupActions from '../redux/modules/requestGroups' import * as WorkspaceActions from '../redux/modules/workspaces' import * as ModalActions from '../redux/modules/modals' import * as db from '../database' import importData from '../lib/import' class WorkspaceDropdown extends Component { _importDialog () { const options = { properties: ['openFile'], filters: [{ name: 'Insomnia Imports', extensions: ['json'] }] }; // TODO: Factor this out into a selector const {entities, workspaces} = this.props; let workspace = entities.workspaces[workspaces.activeId]; if (!workspace) { workspace = entities.workspaces[Object.keys(entities.workspaces)[0]]; } electron.remote.dialog.showOpenDialog(options, paths => { paths.map(path => { fs.readFile(path, 'utf8', (err, data) => { err || importData(workspace, data); }) }) }); } _workspaceCreate () { db.workspaceCreate({name: 'New Workspace'}).then(workspace => { this.props.actions.workspaces.activate(workspace); }); } render () { const {actions, loading, workspaces, entities, ...other} = this.props; const allWorkspaces = Object.keys(entities.workspaces).map(id => entities.workspaces[id]); // TODO: Factor this out into a selector let workspace = entities.workspaces[workspaces.activeId]; if (!workspace) { workspace = entities.workspaces[Object.keys(entities.workspaces)[0]]; } return ( ) } } WorkspaceDropdown.propTypes = { loading: PropTypes.bool.isRequired, workspaces: PropTypes.shape({ activeId: PropTypes.string }), entities: PropTypes.shape({ workspaces: PropTypes.object.isRequired }).isRequired, actions: PropTypes.shape({ modals: PropTypes.shape({ show: PropTypes.func.isRequired }), requestGroups: PropTypes.shape({ showEnvironmentEditModal: PropTypes.func.isRequired }), workspaces: PropTypes.shape({ activate: PropTypes.func.isRequired, showUpdateNamePrompt: PropTypes.func.isRequired }) }) }; function mapStateToProps (state) { return { workspaces: state.workspaces, entities: state.entities, actions: state.actions, loading: state.global.loading }; } function mapDispatchToProps (dispatch) { return { actions: { requestGroups: bindActionCreators(RequestGroupActions, dispatch), workspaces: bindActionCreators(WorkspaceActions, dispatch), modals: bindActionCreators(ModalActions, dispatch) } } } export default connect( mapStateToProps, mapDispatchToProps )(WorkspaceDropdown);