import React, { PropTypes } from 'react' import CSSModules from 'browser/lib/CSSModules' import styles from './NewRepositoryModal.styl' import Repository from 'browser/lib/Repository' import store from 'browser/main/store' const electron = require('electron') const remote = electron.remote function browseFolder () { let dialog = remote.dialog let defaultPath = remote.app.getPath('home') return new Promise((resolve, reject) => { dialog.showOpenDialog({ title: 'Select Directory', defaultPath, properties: ['openDirectory', 'createDirectory'] }, function (targetPaths) { if (targetPaths == null) return resolve('') resolve(targetPaths[0]) }) }) } class NewRepositoryModal extends React.Component { constructor (props) { super(props) this.state = { name: '', path: '', isPathSectionFocused: false, error: null, isBrowsingPath: false } } componentDidMount () { this.refs.nameInput.focus() } handleCloseButtonClick (e) { this.props.close() } handlePathFocus (e) { this.setState({ isPathSectionFocused: true }) } handlePathBlur (e) { if (e.relatedTarget !== this.refs.pathInput && e.relatedTarget !== this.refs.browseButton) { this.setState({ isPathSectionFocused: false }) } } handleBrowseButtonClick (e) { this.setState({ isBrowsingPath: true }, () => { browseFolder() .then((targetPath) => { this.setState({ path: targetPath, isBrowsingPath: false }) }) .catch((err) => { console.error('BrowseFAILED') console.error(err) this.setState({ isBrowsingPath: false }) }) }) } handleConfirmButtonClick (e) { let targetPath = this.state.path let name = this.state.name let repository = new Repository({ name: name, path: targetPath }) repository .mount() .then(() => repository.load()) .then((data) => { store.dispatch({ type: 'ADD_REPOSITORY', repository: data }) this.props.close() }) .catch((err) => { console.error(err) this.setState({ error: err.message }) }) } handleChange (e) { let name = this.refs.nameInput.value let path = this.refs.pathInput.value this.setState({ name, path }) } render () { return (