import React, { PropTypes } from 'react' import linkState from 'boost/linkState' import FolderMark from 'boost/components/FolderMark' import store from 'boost/store' import { updateFolder, destroyFolder, replaceFolder } from 'boost/actions' const IDLE = 'IDLE' const EDIT = 'EDIT' const DELETE = 'DELETE' export default class FolderRow extends React.Component { constructor (props) { super(props) this.state = { mode: IDLE } } handleUpClick (e) { let { index } = this.props if (index > 0) { store.dispatch(replaceFolder(index, index - 1)) } } handleDownClick (e) { let { index, count } = this.props if (index < count - 1) { store.dispatch(replaceFolder(index, index + 1)) } } handleCancelButtonClick (e) { this.setState({ mode: IDLE }) } handleEditButtonClick (e) { this.setState({ mode: EDIT, name: this.props.folder.name, color: this.props.folder.color, isColorEditing: false }) } handleDeleteButtonClick (e) { this.setState({mode: DELETE}) } handleColorSelectClick (e) { this.setState({ isColorEditing: true }) } handleColorButtonClick (index) { return e => { this.setState({ color: index, isColorEditing: false }) } } handleSaveButtonClick (e) { let { folder, setAlert } = this.props setAlert(null, () => { let input = { name: this.state.name, color: this.state.color } folder = Object.assign({}, folder, input) try { store.dispatch(updateFolder(folder)) this.setState({ mode: IDLE }) } catch (e) { console.error(e) setAlert({ type: 'error', message: e.message }) } }) } handleDeleteConfirmButtonClick (e) { let { folder } = this.props store.dispatch(destroyFolder(folder.key)) } render () { let folder = this.props.folder switch (this.state.mode) { case EDIT: let colorIndexes = [] for (let i = 0; i < 8; i++) { colorIndexes.push(i) } let colorOptions = colorIndexes.map(index => { let className = this.state.color === index ? 'active' : null return ( ) }) return (
{this.state.isColorEditing ? (
Color select
{colorOptions}
) : null }
) case DELETE: return (
Are you sure to delete {folder.name} folder?
) case IDLE: default: return (
{folder.name}
) } } } FolderRow.propTypes = { folder: PropTypes.shape(), index: PropTypes.number, count: PropTypes.number, setAlert: PropTypes.func } FolderRow.prototype.linkState = linkState