Timeline.jsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.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={props.timelineData.filter(c => c.timelineType === 'revision' && c.revision_id <= content.revision_id).length}
  43. key={`revision_${content.revision_id}`}
  44. />
  45. }
  46. })}
  47. <li style={{visibility: 'hidden'}} ref={el => { this.timelineBottom = el }} />
  48. </ul>
  49. <form className={classnames(`${props.customClass}__texteditor`, 'timeline__texteditor d-flex align-items-center justify-content-between flex-wrap')}>
  50. <div className={classnames(`${props.customClass}__texteditor__textinput`, 'timeline__texteditor__textinput')}>
  51. <textarea
  52. id='wysiwygTimelineComment'
  53. placeholder='Taper votre message ici'
  54. value={props.newComment}
  55. onChange={props.onChangeNewComment}
  56. />
  57. </div>
  58. <div className={classnames(`${props.customClass}__texteditor__wrapper`, 'timeline__texteditor__wrapper')}>
  59. <div className={classnames(`${props.customClass}__texteditor__advancedtext`, 'timeline__texteditor__advancedtext')}>
  60. <button
  61. type='button'
  62. className={classnames(
  63. `${props.customClass}__texteditor__advancedtext__btn timeline__texteditor__advancedtext__btn btn btn-outline-primary`
  64. )}
  65. onClick={props.onClickWysiwygBtn}
  66. >
  67. {props.wysiwyg ? 'Text Simple' : 'Texte Avancé'}
  68. </button>
  69. </div>
  70. <div className={classnames(`${props.customClass}__texteditor__submit`, 'timeline__texteditor__submit mb-2')}>
  71. <button
  72. type='button'
  73. className={classnames(`${props.customClass}__texteditor__submit__btn`, 'timeline__texteditor__submit__btn btn')}
  74. onClick={props.onClickValidateNewCommentBtn}
  75. >
  76. Envoyer
  77. <div
  78. className={classnames(`${props.customClass}__texteditor__submit__btn__icon`, 'timeline__texteditor__submit__btn__icon')}>
  79. <i className='fa fa-paper-plane-o' />
  80. </div>
  81. </button>
  82. </div>
  83. </div>
  84. </form>
  85. </div>
  86. )
  87. }
  88. }
  89. export default Timeline
  90. Timeline.propTypes = {
  91. timelineData: PropTypes.array.isRequired,
  92. newComment: PropTypes.string.isRequired,
  93. onChangeNewComment: PropTypes.func.isRequired,
  94. onClickValidateNewCommentBtn: PropTypes.func.isRequired,
  95. customClass: PropTypes.string,
  96. loggedUser: PropTypes.object,
  97. wysiwyg: PropTypes.bool,
  98. onClickWysiwygBtn: PropTypes.func
  99. }
  100. Timeline.defaultProps = {
  101. customClass: '',
  102. loggedUser: {
  103. id: '',
  104. name: '',
  105. avatar: ''
  106. },
  107. timelineData: [],
  108. wysiwyg: false,
  109. onClickWysiwygBtn: () => {}
  110. }