2016-11-05 15:20:05 +01:00
import { connect } from 'react-redux' ;
2017-04-21 20:05:35 +02:00
import PropTypes from 'prop-types' ;
2016-11-05 15:20:05 +01:00
import StatusListContainer from '../ui/containers/status_list_container' ;
import Column from '../ui/components/column' ;
import {
refreshTimeline ,
2016-11-10 00:47:47 +01:00
updateTimeline ,
deleteFromTimelines
2016-11-05 15:20:05 +01:00
} from '../../actions/timelines' ;
2017-01-30 15:22:04 +01:00
import ColumnBackButtonSlim from '../../components/column_back_button_slim' ;
2017-02-18 02:37:59 +01:00
import { FormattedMessage } from 'react-intl' ;
2017-02-04 00:34:31 +01:00
import createStream from '../../stream' ;
const mapStateToProps = state => ( {
2017-02-23 02:14:35 +01:00
hasUnread : state . getIn ( [ 'timelines' , 'tag' , 'unread' ] ) > 0 ,
2017-04-15 02:32:42 +02:00
streamingAPIBaseURL : state . getIn ( [ 'meta' , 'streaming_api_base_url' ] ) ,
2017-02-04 00:34:31 +01:00
accessToken : state . getIn ( [ 'meta' , 'access_token' ] )
} ) ;
2016-11-05 15:20:05 +01:00
2017-04-21 20:05:35 +02:00
class HashtagTimeline extends React . PureComponent {
2016-11-05 15:20:05 +01:00
_subscribe ( dispatch , id ) {
2017-04-15 02:32:42 +02:00
const { streamingAPIBaseURL , accessToken } = this . props ;
2017-02-04 00:34:31 +01:00
2017-04-15 02:32:42 +02:00
this . subscription = createStream ( streamingAPIBaseURL , accessToken , ` hashtag&tag= ${ id } ` , {
2017-02-04 00:34:31 +01:00
received ( data ) {
switch ( data . event ) {
case 'update' :
dispatch ( updateTimeline ( 'tag' , JSON . parse ( data . payload ) ) ) ;
break ;
case 'delete' :
dispatch ( deleteFromTimelines ( data . payload ) ) ;
break ;
2016-11-05 15:20:05 +01:00
}
2017-02-04 00:34:31 +01:00
}
2016-11-05 15:20:05 +01:00
2017-02-04 00:34:31 +01:00
} ) ;
2017-04-21 20:05:35 +02:00
}
2016-11-05 15:20:05 +01:00
_unsubscribe ( ) {
if ( typeof this . subscription !== 'undefined' ) {
2017-02-04 00:34:31 +01:00
this . subscription . close ( ) ;
this . subscription = null ;
2016-11-05 15:20:05 +01:00
}
2017-04-21 20:05:35 +02:00
}
2016-11-05 15:20:05 +01:00
2017-02-04 00:34:31 +01:00
componentDidMount ( ) {
2016-11-05 15:20:05 +01:00
const { dispatch } = this . props ;
const { id } = this . props . params ;
2016-12-03 21:04:57 +01:00
dispatch ( refreshTimeline ( 'tag' , id ) ) ;
2016-11-05 15:20:05 +01:00
this . _subscribe ( dispatch , id ) ;
2017-04-21 20:05:35 +02:00
}
2016-11-05 15:20:05 +01:00
componentWillReceiveProps ( nextProps ) {
if ( nextProps . params . id !== this . props . params . id ) {
2016-12-03 21:04:57 +01:00
this . props . dispatch ( refreshTimeline ( 'tag' , nextProps . params . id ) ) ;
2016-11-05 15:20:05 +01:00
this . _unsubscribe ( ) ;
this . _subscribe ( this . props . dispatch , nextProps . params . id ) ;
}
2017-04-21 20:05:35 +02:00
}
2016-11-05 15:20:05 +01:00
componentWillUnmount ( ) {
this . _unsubscribe ( ) ;
2017-04-21 20:05:35 +02:00
}
2016-11-05 15:20:05 +01:00
render ( ) {
2017-02-23 02:14:35 +01:00
const { id , hasUnread } = this . props . params ;
2016-11-05 15:20:05 +01:00
return (
2017-02-23 02:14:35 +01:00
< Column icon = 'hashtag' active = { hasUnread } heading = { id } >
2017-01-30 15:22:04 +01:00
< ColumnBackButtonSlim / >
2017-04-24 04:49:08 +02:00
< StatusListContainer scrollKey = 'hashtag_timeline' type = 'tag' id = { id } emptyMessage = { < FormattedMessage id = 'empty_column.hashtag' defaultMessage = 'There is nothing in this hashtag yet.' / > } / >
2016-11-05 15:20:05 +01:00
< / Column >
) ;
2017-04-21 20:05:35 +02:00
}
2016-11-05 15:20:05 +01:00
2017-04-21 20:05:35 +02:00
}
HashtagTimeline . propTypes = {
params : PropTypes . object . isRequired ,
dispatch : PropTypes . func . isRequired ,
streamingAPIBaseURL : PropTypes . string . isRequired ,
accessToken : PropTypes . string . isRequired ,
hasUnread : PropTypes . bool
} ;
2016-11-05 15:20:05 +01:00
2017-02-04 00:34:31 +01:00
export default connect ( mapStateToProps ) ( HashtagTimeline ) ;