dfghgfhplkjbv

src/components/InterviewsListt/InterviewsList.js

Feb 14th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import PropTypes from 'prop-types'
  3. import classNames from 'classnames'
  4. import styles from './InterviewsList.module.scss'
  5. import SortPanel from 'src/components/SortPanel'
  6. import { formatMessage } from 'src/translations'
  7. import ItemWithoutPreview from 'src/components/ReadMore/ItemWithoutPreview'
  8.  
  9. class InterviewsList extends Component {
  10. state = { interviews: this.props.interviews, activeOption: null }
  11.  
  12. handleSortByOldDate = () => {
  13. this.setState({
  14. interviews: this.state.interviews.edges.sort(
  15. (a, b) => (a.node.publishDate > b.node.publishDate ? 1 : b.node.publishDate > a.node.publishDate ? -1 : 0),
  16. ),
  17. })
  18. this.setState({
  19. interviews: this.state.interviews
  20. })
  21. }
  22.  
  23. handleSortByNewDate = () => {
  24. this.setState({
  25. interviews: this.state.interviews.edges.sort(
  26. (a, b) => (a.node.publishDate > b.node.publishDate ? -1 : b.node.publishDate > a.node.publishDate ? 1 : 0),
  27. ),
  28. })
  29. this.setState({
  30. interviews: this.state.interviews
  31. })
  32. }
  33.  
  34. handleSort = (ev) => {
  35. this.setState({ activeOption: ev.target.value }, () => {
  36. if (this.state.activeOption === 'old') {
  37. this.handleSortByOldDate()
  38. } else if (this.state.activeOption === 'new') {
  39. this.handleSortByNewDate()
  40. }
  41. })
  42. }
  43.  
  44. render() {
  45. console.log(this.state.interviews.edges)
  46. const {
  47. className,
  48. interviews: { edges },
  49. locale,
  50. } = this.props
  51.  
  52. const sortOptions = [
  53. { type: 'new', text: formatMessage(locale, 'fromNewToOld') },
  54. { type: 'old', text: formatMessage(locale, 'fromOldToNew') },
  55. ]
  56. const articles = [...edges]
  57.  
  58. return (
  59. <>
  60. <SortPanel
  61. title={formatMessage(locale, 'AllInterviews')}
  62. options={sortOptions}
  63. sortTitle={formatMessage(locale, 'Sort')}
  64. onGetOption={(ev) => this.handleSort(ev)}
  65. />
  66.  
  67. <div>
  68. <section className={classNames(className, styles.root)}>
  69. <div className={styles.articles}>
  70. {articles.map((item) => <ItemWithoutPreview key={item.node.slug} {...item} />)}
  71. </div>
  72. </section>
  73. </div>
  74. </>
  75. )
  76. }
  77. }
  78.  
  79. InterviewsList.propTypes = {
  80. className: PropTypes.string,
  81. edges: PropTypes.array,
  82. locale: PropTypes.string.isRequired,
  83. }
  84.  
  85. export default InterviewsList
Advertisement
Add Comment
Please, Sign In to add comment