2022-10-08 18:55:09 -07:00
import { debounce } from 'lodash' ;
2017-04-21 11:05:35 -07:00
import PropTypes from 'prop-types' ;
2022-10-08 18:55:09 -07:00
import React from 'react' ;
import { Helmet } from 'react-helmet' ;
2017-06-24 03:24:02 -07:00
import ImmutablePropTypes from 'react-immutable-proptypes' ;
2022-10-08 18:55:09 -07:00
import ImmutablePureComponent from 'react-immutable-pure-component' ;
import { defineMessages , injectIntl , FormattedMessage } from 'react-intl' ;
import { connect } from 'react-redux' ;
import { addColumn , removeColumn , moveColumn } from 'flavours/glitch/actions/columns' ;
2017-12-03 23:26:40 -08:00
import { fetchFavouritedStatuses , expandFavouritedStatuses } from 'flavours/glitch/actions/favourites' ;
import ColumnHeader from 'flavours/glitch/components/column_header' ;
import StatusList from 'flavours/glitch/components/status_list' ;
2022-10-08 18:55:09 -07:00
import Column from 'flavours/glitch/features/ui/components/column' ;
2017-01-16 04:27:58 -08:00
const messages = defineMessages ( {
2017-05-20 08:31:47 -07:00
heading : { id : 'column.favourites' , defaultMessage : 'Favourites' } ,
2017-01-16 04:27:58 -08:00
} ) ;
const mapStateToProps = state => ( {
2017-06-24 03:24:02 -07:00
statusIds : state . getIn ( [ 'status_lists' , 'favourites' , 'items' ] ) ,
2018-01-09 06:46:35 -08:00
isLoading : state . getIn ( [ 'status_lists' , 'favourites' , 'isLoading' ] , true ) ,
2017-08-28 13:23:44 -07:00
hasMore : ! ! state . getIn ( [ 'status_lists' , 'favourites' , 'next' ] ) ,
2017-01-16 04:27:58 -08:00
} ) ;
2019-09-09 06:16:08 -07:00
class Favourites extends ImmutablePureComponent {
2017-01-16 04:27:58 -08:00
2017-05-12 05:44:10 -07:00
static propTypes = {
dispatch : PropTypes . func . isRequired ,
2017-06-24 03:24:02 -07:00
statusIds : ImmutablePropTypes . list . isRequired ,
2017-05-12 05:44:10 -07:00
intl : PropTypes . object . isRequired ,
2017-07-14 15:49:34 -07:00
columnId : PropTypes . string ,
multiColumn : PropTypes . bool ,
2017-08-28 13:23:44 -07:00
hasMore : PropTypes . bool ,
2018-01-09 06:46:35 -08:00
isLoading : PropTypes . bool ,
2017-05-12 05:44:10 -07:00
} ;
2017-01-16 04:27:58 -08:00
componentWillMount ( ) {
this . props . dispatch ( fetchFavouritedStatuses ( ) ) ;
2017-04-21 11:05:35 -07:00
}
2017-01-16 04:27:58 -08:00
2017-07-14 15:49:34 -07:00
handlePin = ( ) => {
const { columnId , dispatch } = this . props ;
if ( columnId ) {
dispatch ( removeColumn ( columnId ) ) ;
} else {
dispatch ( addColumn ( 'FAVOURITES' , { } ) ) ;
}
2023-02-03 11:52:07 -08:00
} ;
2017-07-14 15:49:34 -07:00
handleMove = ( dir ) => {
const { columnId , dispatch } = this . props ;
dispatch ( moveColumn ( columnId , dir ) ) ;
2023-02-03 11:52:07 -08:00
} ;
2017-07-14 15:49:34 -07:00
handleHeaderClick = ( ) => {
this . column . scrollTop ( ) ;
2023-02-03 11:52:07 -08:00
} ;
2017-07-14 15:49:34 -07:00
setRef = c => {
this . column = c ;
2023-02-03 11:52:07 -08:00
} ;
2017-07-14 15:49:34 -07:00
2018-05-27 08:46:48 -07:00
handleLoadMore = debounce ( ( ) => {
2017-01-16 04:27:58 -08:00
this . props . dispatch ( expandFavouritedStatuses ( ) ) ;
2023-02-03 11:52:07 -08:00
} , 300 , { leading : true } ) ;
2017-01-16 04:27:58 -08:00
render ( ) {
2018-01-09 06:46:35 -08:00
const { intl , statusIds , columnId , multiColumn , hasMore , isLoading } = this . props ;
2017-07-14 15:49:34 -07:00
const pinned = ! ! columnId ;
2017-01-16 04:27:58 -08:00
2022-05-03 01:59:23 -07:00
const emptyMessage = < FormattedMessage id = 'empty_column.favourited_statuses' defaultMessage = "You don't have any favourite posts yet. When you favourite one, it will show up here." / > ;
2018-08-26 07:39:37 -07:00
2017-01-16 04:27:58 -08:00
return (
2019-08-01 10:17:17 -07:00
< Column bindToDocument = { ! multiColumn } ref = { this . setRef } name = 'favourites' label = { intl . formatMessage ( messages . heading ) } >
2017-07-14 15:49:34 -07:00
< ColumnHeader
icon = 'star'
title = { intl . formatMessage ( messages . heading ) }
onPin = { this . handlePin }
onMove = { this . handleMove }
onClick = { this . handleHeaderClick }
pinned = { pinned }
multiColumn = { multiColumn }
2017-09-06 08:32:15 -07:00
showBackButton
2017-07-14 15:49:34 -07:00
/ >
< StatusList
trackScroll = { ! pinned }
statusIds = { statusIds }
scrollKey = { ` favourited_statuses- ${ columnId } ` }
2017-08-28 13:23:44 -07:00
hasMore = { hasMore }
2018-01-09 06:46:35 -08:00
isLoading = { isLoading }
2018-05-27 08:46:48 -07:00
onLoadMore = { this . handleLoadMore }
2018-08-26 07:39:37 -07:00
emptyMessage = { emptyMessage }
2019-07-19 00:25:22 -07:00
bindToDocument = { ! multiColumn }
2017-07-14 15:49:34 -07:00
/ >
2022-10-08 18:55:09 -07:00
< Helmet >
< title > { intl . formatMessage ( messages . heading ) } < / title >
2022-10-20 05:35:29 -07:00
< meta name = 'robots' content = 'noindex' / >
2022-10-08 18:55:09 -07:00
< / Helmet >
2017-01-16 04:27:58 -08:00
< / Column >
) ;
}
2017-04-21 11:05:35 -07:00
}
2023-03-24 15:15:25 -07:00
export default connect ( mapStateToProps ) ( injectIntl ( Favourites ) ) ;