Timeline.jsx 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React from 'react'
  2. import PropTypes from 'prop-types'
  3. import classnames from 'classnames'
  4. import Comment from './Comment.jsx'
  5. import Revision from './Revision.jsx'
  6. require('./Timeline.styl')
  7. class Timeline extends React.Component {
  8. componentDidMount () {
  9. this.scrollToBottom()
  10. }
  11. componentDidUpdate () {
  12. this.props.shouldScrollToBottom && this.scrollToBottom()
  13. }
  14. scrollToBottom = () => this.timelineBottom.scrollIntoView({behavior: 'instant'})
  15. render () {
  16. const { props } = this
  17. if (!Array.isArray(props.timelineData)) {
  18. console.log('Error in Timeline.jsx, props.timelineData is not an array. timelineData: ', props.timelineData)
  19. return null
  20. }
  21. return (
  22. <div className='timeline'>
  23. {props.showHeader &&
  24. <div className={classnames(`${props.customClass}__header`, 'timeline__header')}>
  25. Timeline
  26. </div>
  27. }
  28. <ul className={classnames(`${props.customClass}__messagelist`, 'timeline__messagelist')}>
  29. {props.timelineData.map(content => {
  30. switch (content.timelineType) {
  31. case 'comment':
  32. return <Comment
  33. customClass={props.customClass}
  34. avatar={content.author.avatar_url}
  35. createdAt={content.created}
  36. text={content.raw_content}
  37. fromMe={props.loggedUser.user_id === content.author.user_id}
  38. key={`comment_${content.content_id}`}
  39. />
  40. case 'revision':
  41. return <Revision
  42. customClass={props.customClass}
  43. createdAt={content.created}
  44. number={content.number}
  45. key={`revision_${content.revision_id}`}
  46. onClickRevision={() => props.onClickRevisionBtn(content)}
  47. />
  48. }
  49. })}
  50. <li style={{visibility: 'hidden'}} ref={el => { this.timelineBottom = el }} />
  51. </ul>
  52. <form className={classnames(`${props.customClass}__texteditor`, 'timeline__texteditor')}>
  53. <div className={classnames(`${props.customClass}__texteditor__textinput`, 'timeline__texteditor__textinput')}>
  54. <textarea
  55. id='wysiwygTimelineComment'
  56. placeholder='Taper votre message ici'
  57. value={props.newComment}
  58. onChange={props.onChangeNewComment}
  59. disabled={props.disableComment}
  60. />
  61. </div>
  62. <div className={classnames(`${props.customClass}__texteditor__wrapper`, 'timeline__texteditor__wrapper')}>
  63. <div className={classnames(`${props.customClass}__texteditor__advancedtext`, 'timeline__texteditor__advancedtext')}>
  64. <button
  65. type='button'
  66. className={classnames(
  67. `${props.customClass}__texteditor__advancedtext__btn timeline__texteditor__advancedtext__btn btn btn-outline-primary`
  68. )}
  69. onClick={props.onClickWysiwygBtn}
  70. disabled={props.disableComment}
  71. >
  72. {props.wysiwyg ? 'Text Simple' : 'Texte Avancé'}
  73. </button>
  74. </div>
  75. <div className={classnames(`${props.customClass}__texteditor__submit`, 'timeline__texteditor__submit')}>
  76. <button
  77. type='button'
  78. className={classnames(`${props.customClass}__texteditor__submit__btn`, 'timeline__texteditor__submit__btn btn')}
  79. onClick={props.onClickValidateNewCommentBtn}
  80. disabled={props.disableComment}
  81. >
  82. Envoyer
  83. <div
  84. className={classnames(`${props.customClass}__texteditor__submit__btn__icon`, 'timeline__texteditor__submit__btn__icon')}>
  85. <i className='fa fa-paper-plane-o' />
  86. </div>
  87. </button>
  88. </div>
  89. </div>
  90. </form>
  91. </div>
  92. )
  93. }
  94. }
  95. export default Timeline
  96. Timeline.propTypes = {
  97. timelineData: PropTypes.array.isRequired,
  98. newComment: PropTypes.string.isRequired,
  99. onChangeNewComment: PropTypes.func.isRequired,
  100. onClickValidateNewCommentBtn: PropTypes.func.isRequired,
  101. disableComment: PropTypes.bool,
  102. customClass: PropTypes.string,
  103. loggedUser: PropTypes.object,
  104. wysiwyg: PropTypes.bool,
  105. onClickWysiwygBtn: PropTypes.func,
  106. onClickRevisionBtn: PropTypes.func,
  107. shouldScrollToBottom: PropTypes.bool,
  108. showHeader: PropTypes.bool
  109. }
  110. Timeline.defaultProps = {
  111. disableComment: false,
  112. customClass: '',
  113. loggedUser: {
  114. id: '',
  115. name: '',
  116. avatar: ''
  117. },
  118. timelineData: [],
  119. wysiwyg: false,
  120. onClickWysiwygBtn: () => {},
  121. shouldScrollToBottom: true,
  122. showHeader: true
  123. }