import React, { PropTypes } from 'react' import moment from 'moment' import { findWhere, uniq } from 'lodash' import ModeIcon from 'boost/components/ModeIcon' import MarkdownPreview from 'boost/components/MarkdownPreview' import CodeEditor from 'boost/components/CodeEditor' import { UNSYNCED, IDLE_MODE, CREATE_MODE, EDIT_MODE, switchMode, switchArticle, updateArticle, destroyArticle } from 'boost/actions' import aceModes from 'boost/ace-modes' import Select from 'react-select' import linkState from 'boost/linkState' import api from 'boost/api' import FolderMark from 'boost/components/FolderMark' var modeOptions = aceModes.map(function (mode) { return { label: mode, value: mode } }) function makeInstantArticle (article) { let instantArticle = Object.assign({}, article) instantArticle.Tags = Array.isArray(instantArticle.Tags) ? instantArticle.Tags.map(tag => tag.name) : [] return instantArticle } export default class ArticleDetail extends React.Component { constructor (props) { super(props) this.state = { article: makeInstantArticle(props.activeArticle) } } componentWillReceiveProps (nextProps) { if (nextProps.activeArticle != null && nextProps.activeArticle.id !== this.state.article.id) { this.setState({article: makeInstantArticle(nextProps.activeArticle)}, function () { console.log('receive props') }) } } renderEmpty () { return (
Empty article
) } handleEditButtonClick (e) { let { dispatch } = this.props dispatch(switchMode(EDIT_MODE)) } handleDeleteButtonClick (e) { this.setState({openDeleteConfirmMenu: true}) } handleDeleteConfirmButtonClick (e) { let { dispatch, activeUser, activeArticle } = this.props api.destroyArticle(activeArticle.id) .then(res => { console.log(res.body) }) .catch(err => { // connect failed need to queue data if (err.code === 'ECONNREFUSED') { return } if (err.status != null) throw err else console.log(err) }) dispatch(destroyArticle(activeUser.id, activeArticle.id)) this.setState({openDeleteConfirmMenu: false}) } handleDeleteCancleButtonClick (e) { this.setState({openDeleteConfirmMenu: false}) } renderIdle () { let { activeArticle, activeUser } = this.props let tags = activeArticle.Tags.length > 0 ? activeArticle.Tags.map(tag => { return ( {tag.name} ) }) : ( Not tagged yet ) let folder = findWhere(activeUser.Folders, {id: activeArticle.FolderId}) let folderName = folder != null ? folder.name : '(unknown)' return (
{this.state.openDeleteConfirmMenu ? (
Are you sure to delete this article?
) : (
{folderName}  by {activeArticle.User.profileName}  Created {moment(activeArticle.createdAt).format('YYYY/MM/DD')}  Updated {moment(activeArticle.updatedAt).format('YYYY/MM/DD')}
{tags}
) }
{activeArticle.title}
{activeArticle.mode === 'markdown' ? : }
) } handleCancelButtonClick (e) { this.props.dispatch(switchMode(IDLE_MODE)) } handleSaveButtonClick (e) { let { activeArticle } = this.props if (activeArticle.id == null) this.saveAsNew() else this.save() } saveAsNew () { let { dispatch, activeUser } = this.props let article = this.state.article let newArticle = Object.assign({}, article) article.tags = article.Tags api.createArticle(article) .then(res => { console.log(res.body) }) .catch(err => { // connect failed need to queue data if (err.code === 'ECONNREFUSED') { return } if (err.status != null) throw err else console.log(err) }) newArticle.status = UNSYNCED newArticle.Tags = newArticle.Tags.map(tag => { return {name: tag} }) dispatch(updateArticle(activeUser.id, newArticle)) dispatch(switchMode(IDLE_MODE)) dispatch(switchArticle(article.id)) } save () { let { dispatch, activeUser } = this.props let article = this.state.article let newArticle = Object.assign({}, article) article.tags = article.Tags api.saveArticle(article) .then(res => { console.log(res.body) }) .catch(err => { // connect failed need to queue data if (err.code === 'ECONNREFUSED') { return } if (err.status != null) throw err else console.log(err) }) newArticle.status = UNSYNCED newArticle.Tags = newArticle.Tags.map(tag => { return {name: tag} }) dispatch(updateArticle(activeUser.id, newArticle)) dispatch(switchMode(IDLE_MODE)) dispatch(switchArticle(article.id)) } handleFolderIdChange (value) { let article = this.state.article article.FolderId = value this.setState({article: article}) } handleTagsChange (tag, tags) { tags = uniq(tags, function (tag) { return tag.value }) var article = this.state.article article.Tags = tags.map(function (tag) { return tag.value }) this.setState({article: article}) } handleModeChange (value) { let article = this.state.article article.mode = value this.setState({article: article}) } handleContentChange (e, value) { let article = this.state.article article.content = value this.setState({article: article}) } renderEdit () { let { activeUser } = this.props let folderOptions = activeUser.Folders.map(folder => { return { label: folder.name, value: folder.id } }) return (
this.handleTagsChange(tag, tags)} clearable={false} multi placeholder='add some tags...' allowCreate value={this.state.article.Tags} className='tags'/>