dfghgfhplkjbv

src/components/News/News.js

Feb 28th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import PropTypes from 'prop-types'
  3. import classNames from 'classnames'
  4. import SocialShare from 'src/components/SocialShare'
  5. import { getBaseUrl } from 'src/utils'
  6. import Ad from '../Ad'
  7. import { formatMessage } from 'src/translations'
  8. import styles from './News.module.scss'
  9. import { isOnline, removeListeners } from 'src/utils'
  10.  
  11. class News extends Component {
  12. state = { isModalOpen: false, isOnline: true }
  13.  
  14. componentDidMount() {
  15. if (typeof window !== 'undefined') {
  16. window.document.body.classList.add('white-bg')
  17. }
  18. isOnline(this)
  19. }
  20.  
  21. componentWillUnmount() {
  22. if (typeof window !== 'undefined') {
  23. window.document.body.classList.remove('white-bg')
  24. }
  25. removeListeners()
  26. }
  27.  
  28. toggleModal = () => {
  29. this.setState({ isModalOpen: !this.state.isModalOpen })
  30. }
  31.  
  32. render() {
  33. const {
  34. className,
  35. news: {
  36. bodyNode: {
  37. childMarkdownRemark: { html },
  38. },
  39. photographer,
  40. author,
  41. publishdate,
  42. title,
  43. },
  44. locale,
  45. pathname,
  46. } = this.props
  47.  
  48. const buttonContainer = (
  49. <button className={styles.button} onClick={this.toggleModal}>
  50. <span className={styles.buttonText}>{formatMessage(locale, 'share')}</span>
  51. <SocialShare
  52. className={styles.share}
  53. isWhite={true}
  54. title={title}
  55. url={`${getBaseUrl(locale)}${pathname.slice(1)}`}
  56. isModalOpen={this.state.isModalOpen}
  57. toggleModal={this.toggleModal}
  58. />
  59. </button>
  60. )
  61. return (
  62. <div className={classNames(className, styles.root)}>
  63. <div className={styles.inner}>
  64. <article className={styles.article} dangerouslySetInnerHTML={{ __html: html }} />
  65. <div className={classNames(styles.ad, { [styles.sticky]: html.length <= 600 })}>
  66. <Ad locale={locale} isLong />
  67. </div>
  68. </div>
  69. <div className={styles.meta}>
  70. <div className={styles.publishDate}>{publishdate}</div>
  71. {author && (
  72. <div className={styles.author}>
  73. <strong>{formatMessage(locale, 'author')}</strong>
  74. <span>{author}</span>
  75. </div>
  76. )}
  77. {photographer && (
  78. <div className={styles.photographer}>
  79. <strong>{formatMessage(locale, 'photo')}</strong>
  80. <span>{photographer}</span>
  81. </div>
  82. )}
  83. </div>
  84. <div className={styles.buttonContainer}>
  85. {this.state.isOnline ? buttonContainer : <div className={styles.offlineMode}>{buttonContainer}</div>}
  86. </div>
  87. </div>
  88. )
  89. }
  90. }
  91.  
  92. News.propTypes = {
  93. className: PropTypes.string,
  94. news: PropTypes.shape({
  95. html: PropTypes.string,
  96. photographer: PropTypes.string,
  97. author: PropTypes.string,
  98. publishdate: PropTypes.string,
  99. title: PropTypes.string,
  100. }),
  101. locale: PropTypes.string.isRequired,
  102. }
  103.  
  104. export default News
Advertisement
Add Comment
Please, Sign In to add comment