focalboard/webapp/src/components/commentsList.tsx

110 lines
3.5 KiB
TypeScript
Raw Normal View History

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react'
import {injectIntl, IntlShape, FormattedMessage} from 'react-intl'
import {MutableCommentBlock} from '../blocks/commentBlock'
import {IBlock} from '../blocks/block'
import {Utils} from '../utils'
import mutator from '../mutator'
import Comment from './comment'
import './commentsList.scss'
2020-10-28 00:59:40 +01:00
import { MarkdownEditor } from './markdownEditor'
type Props = {
comments: readonly IBlock[]
cardId: string
intl: IntlShape
}
2020-10-26 17:55:05 +01:00
type State = {
newComment: string
inputFocused: boolean
}
class CommentsList extends React.Component<Props, State> {
public constructor(props: Props) {
super(props)
this.state = {
newComment: '',
inputFocused: false,
}
}
public shouldComponentUpdate() {
return true
}
2020-10-26 17:55:05 +01:00
private sendComment = () => {
const {cardId} = this.props
Utils.assertValue(cardId)
2020-10-26 17:55:05 +01:00
const block = new MutableCommentBlock({parentId: cardId, title: this.state.newComment})
mutator.insertBlock(block, 'add comment')
2020-10-26 17:55:05 +01:00
this.setState({newComment: ''})
}
public render(): JSX.Element {
const {comments, intl} = this.props
2020-10-25 19:06:24 +01:00
// TODO: Replace this placeholder
const username = 'John Smith'
const userImageUrl = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" style="fill: rgb(192, 192, 192);"><rect width="100" height="100" /></svg>'
return (
<div className='CommentsList'>
{comments.map((comment) => (
<Comment
key={comment.id}
comment={comment}
userImageUrl={userImageUrl}
username={username}
/>
))}
{/* New comment */}
<div className='commentrow'>
<img
className='comment-avatar'
src={userImageUrl}
/>
2020-10-28 00:59:40 +01:00
<MarkdownEditor
className='newcomment'
2020-10-28 00:59:40 +01:00
text={this.state.newComment}
placeholderText={intl.formatMessage({id: 'CardDetail.new-comment-placeholder', defaultMessage: 'Add a comment...'})}
2020-10-28 00:59:40 +01:00
onChange={(value: string) => {
if (this.state.newComment != value) {
this.setState({newComment: value})
}
}}
/>
2020-10-26 20:30:32 +01:00
{this.state.newComment &&
2020-10-26 17:55:05 +01:00
<div
className='octo-button filled'
onClick={() => {
2020-10-28 00:59:40 +01:00
if (this.state.newComment) {
Utils.log(`Send comment: ${this.state.newComment}`)
this.sendComment()
this.setState({inputFocused: false, newComment: ''})
}
2020-10-26 17:55:05 +01:00
}}
>
<FormattedMessage
id='CommentsList.send'
defaultMessage='Send'
/>
2020-10-28 00:59:40 +01:00
</div>
}
</div>
</div>
)
}
}
export default injectIntl(CommentsList)