dfghgfhplkjbv

src/components/AudiosList/AudiosList.js

Feb 13th, 2019
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. /* eslint-disable */
  2. import React, { Component } from 'react'
  3. import PropTypes from 'prop-types'
  4. import Preview from './Preview'
  5. import SortPanel from 'src/components/SortPanel'
  6. import { formatMessage } from 'src/translations'
  7. import MissingContent from 'src/components/MissingContent'
  8. import styles from './AudiosList.module.scss'
  9.  
  10. class AudiosList extends Component {
  11. state = {
  12. audios: null,
  13. }
  14.  
  15. componentDidMount() {
  16. this.setState({ audios: this.props.audios.filter((item) => item.title !== '_') }, () => {
  17. this.state.audios.map((item) => {
  18. let secondsDuration = item.node.duration.split(':').map((item, index) => (index === 0 ? item * 60 : +item)).reduce((a, b) => a + b, 0)
  19. item.secondsDuration = secondsDuration
  20. })
  21. })
  22. }
  23.  
  24. handleSortByDuration = () => {
  25. this.setState({
  26. audios: []
  27. .concat(this.state.audios)
  28. .sort((a, b) => (a.secondsDuration < b.secondsDuration ? 1 : b.secondsDuration < a.secondsDuration ? -1 : 0)),
  29. })
  30. }
  31.  
  32. handleSortByDate = () => {
  33. this.setState({
  34. audios: []
  35. .concat(this.state.audios)
  36. .sort((a, b) => (a.publishdate > b.publishdate ? 1 : b.publishdate > a.publishdate ? -1 : 0)),
  37. })
  38. }
  39.  
  40. handleSort = (ev) => {
  41. this.setState({ activeOption: ev.target.value }, () => {
  42. if (this.state.activeOption === 'duration') {
  43. this.handleSortByDuration()
  44. } else if (this.state.activeOption === 'date') {
  45. this.handleSortByDate()
  46. }
  47. })
  48. }
  49.  
  50. render() {
  51. console.log(this.state.audios)
  52. const { style, locale } = this.props
  53. const options = [
  54. { type: 'duration', text: formatMessage(locale, 'duration') },
  55. { type: 'date', text: formatMessage(locale, 'date') },
  56. ]
  57.  
  58. const content = (
  59. <>
  60. <SortPanel
  61. title={formatMessage(locale, 'Allpodcasts')}
  62. options={options}
  63. sortTitle={formatMessage(locale, 'Sortby')}
  64. onGetOption={(ev) => this.handleSort(ev)}
  65. />
  66. <section className={styles.root} style={style}>
  67. <div className={styles.inner}>
  68. <div className={styles.wrap}>
  69. {this.state.audios &&
  70. this.state.audios.map((item) => <Preview key={item.node.title} {...item} locale={locale} />)}
  71. </div>
  72. </div>
  73. </section>
  74. </>
  75. )
  76.  
  77. return this.state.audios && locale !== 'en' ? content : <MissingContent locale={locale} title={'missingPodcasts'} />
  78. }
  79. }
  80.  
  81. AudiosList.propTypes = {
  82. className: PropTypes.string,
  83. locale: PropTypes.string.isRequired,
  84. }
  85.  
  86. export default AudiosList
Add Comment
Please, Sign In to add comment