Timeline.jsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. <div className={classnames(`${props.customClass}__header`, 'timeline__header')}>
  24. Timeline
  25. </div>
  26. <ul className={classnames(`${props.customClass}__messagelist`, 'timeline__messagelist')}>
  27. {props.timelineData.map(content => {
  28. switch (content.timelineType) {
  29. case 'comment':
  30. return <Comment
  31. customClass={props.customClass}
  32. avatar={content.author.avatar_url}
  33. createdAt={content.created}
  34. text={content.raw_content}
  35. fromMe={props.loggedUser.user_id === content.author.user_id}
  36. key={`comment_${content.content_id}`}
  37. />
  38. case 'revision':
  39. return <Revision
  40. customClass={props.customClass}
  41. createdAt={content.created}
  42. number={content.number}
  43. key={`revision_${content.revision_id}`}
  44. onClickRevision={() => props.onClickRevisionBtn(content)}
  45. />
  46. }
  47. })}
  48. <li style={{visibility: 'hidden'}} ref={el => { this.timelineBottom = el }} />
  49. </ul>
  50. <form className={classnames(`${props.customClass}__texteditor`, 'timeline__texteditor')}>
  51. <div className={classnames(`${props.customClass}__texteditor__textinput`, 'timeline__texteditor__textinput')}>
  52. <textarea
  53. id='wysiwygTimelineComment'
  54. placeholder='Taper votre message ici'
  55. value={props.newComment}
  56. onChange={props.onChangeNewComment}
  57. disabled={props.disableComment}
  58. />
  59. </div>
  60. <div className={classnames(`${props.customClass}__texteditor__wrapper`, 'timeline__texteditor__wrapper')}>
  61. <div className={classnames(`${props.customClass}__texteditor__advancedtext`, 'timeline__texteditor__advancedtext')}>
  62. <button
  63. type='button'
  64. className={classnames(
  65. `${props.customClass}__texteditor__advancedtext__btn timeline__texteditor__advancedtext__btn btn btn-outline-primary`
  66. )}
  67. onClick={props.onClickWysiwygBtn}
  68. disabled={props.disableComment}
  69. >
  70. {props.wysiwyg ? 'Text Simple' : 'Texte Avancé'}
  71. </button>
  72. </div>
  73. <div className={classnames(`${props.customClass}__texteditor__submit`, 'timeline__texteditor__submit')}>
  74. <button
  75. type='button'
  76. className={classnames(`${props.customClass}__texteditor__submit__btn`, 'timeline__texteditor__submit__btn btn')}
  77. onClick={props.onClickValidateNewCommentBtn}
  78. disabled={props.disableComment}
  79. >
  80. Envoyer
  81. <div
  82. className={classnames(`${props.customClass}__texteditor__submit__btn__icon`, 'timeline__texteditor__submit__btn__icon')}>
  83. <i className='fa fa-paper-plane-o' />
  84. </div>
  85. </button>
  86. </div>
  87. </div>
  88. </form>
  89. </div>
  90. )
  91. }
  92. }
  93. export default Timeline
  94. Timeline.propTypes = {
  95. timelineData: PropTypes.array.isRequired,
  96. newComment: PropTypes.string.isRequired,
  97. onChangeNewComment: PropTypes.func.isRequired,
  98. onClickValidateNewCommentBtn: PropTypes.func.isRequired,
  99. disableComment: PropTypes.bool,
  100. customClass: PropTypes.string,
  101. loggedUser: PropTypes.object,
  102. wysiwyg: PropTypes.bool,
  103. onClickWysiwygBtn: PropTypes.func,
  104. onClickRevisionBtn: PropTypes.func,
  105. shouldScrollToBottom: PropTypes.bool
  106. }
  107. Timeline.defaultProps = {
  108. disableComment: false,
  109. customClass: '',
  110. loggedUser: {
  111. id: '',
  112. name: '',
  113. avatar: ''
  114. },
  115. timelineData: [],
  116. wysiwyg: false,
  117. onClickWysiwygBtn: () => {},
  118. shouldScrollToBottom: true
  119. }