2020-08-17 21:07:16 +01:00
import React from 'react'
2020-08-17 21:59:29 +01:00
import PropTypes from 'prop-types'
2020-08-17 21:39:25 +01:00
import { connect } from 'react-redux'
2020-05-10 04:26:58 +01:00
import { FormattedMessage } from 'react-intl'
2020-02-24 21:56:07 +00:00
import ImmutablePropTypes from 'react-immutable-proptypes'
import ImmutablePureComponent from 'react-immutable-pure-component'
2020-02-28 15:20:47 +00:00
import { fetchAccountByUsername } from '../actions/accounts'
import { makeGetAccount } from '../selectors'
2020-03-04 22:26:01 +00:00
import { me } from '../initial_state'
2020-04-11 23:29:19 +01:00
import PageTitle from '../features/ui/util/page_title'
2020-03-04 22:26:01 +00:00
import ColumnIndicator from '../components/column_indicator'
2020-05-10 04:26:58 +01:00
import Block from '../components/block'
2020-02-24 21:56:07 +00:00
import ProfileLayout from '../layouts/profile_layout'
2019-07-02 08:10:25 +01:00
class ProfilePage extends ImmutablePureComponent {
2020-04-08 02:06:59 +01:00
2020-03-12 16:09:15 +00:00
componentDidMount ( ) {
2020-11-15 18:48:32 +00:00
if ( ! this . props . account ) {
this . props . dispatch ( fetchAccountByUsername ( this . props . params . username ) )
}
2020-02-28 15:20:47 +00:00
}
2020-12-31 22:16:15 +00:00
componentDidUpdate ( ) {
if ( ! this . props . account ) {
this . props . dispatch ( fetchAccountByUsername ( this . props . params . username ) )
}
}
2020-01-27 19:46:42 +00:00
render ( ) {
2020-03-04 22:26:01 +00:00
const {
account ,
children ,
2020-04-08 02:06:59 +01:00
unavailable ,
2020-06-09 00:39:35 +01:00
noSidebar ,
2020-09-01 01:58:14 +01:00
isBlocked ,
2020-10-30 21:25:33 +00:00
isMe ,
2020-04-11 23:29:19 +01:00
params : { username } ,
2020-03-04 22:26:01 +00:00
} = this . props
2019-07-02 08:10:25 +01:00
2020-05-27 01:33:53 +01:00
const nameHTML = ! ! account ? account . get ( 'display_name_html' ) : ''
const name = ! ! account ? account . get ( 'display_name_plain' ) : ''
2020-09-01 01:58:14 +01:00
const unavailableMessage = ( unavailable && isBlocked ) ? < FormattedMessage id = 'empty_column.account_unavailable' defaultMessage = 'Profile unavailable' / > : < FormattedMessage id = 'empty_column.account_private' defaultMessage = 'This account is private. You must request to follow in order to view their page.' / >
2019-07-02 08:10:25 +01:00
return (
2020-02-24 21:56:07 +00:00
< ProfileLayout
account = { account }
2020-05-27 01:33:53 +01:00
titleHTML = { nameHTML }
2020-05-10 04:26:58 +01:00
unavailable = { unavailable }
2020-06-09 00:39:35 +01:00
noSidebar = { noSidebar }
2020-02-24 21:56:07 +00:00
>
2020-04-17 06:35:46 +01:00
< PageTitle path = { ` ${ name } (@ ${ username } ) ` } / >
2020-03-04 22:26:01 +00:00
{
2020-07-29 21:40:47 +01:00
! unavailable &&
2020-03-04 22:26:01 +00:00
React . cloneElement ( children , {
account ,
2020-10-30 21:25:33 +00:00
isMe ,
2020-03-04 22:26:01 +00:00
} )
}
2020-05-10 04:26:58 +01:00
{
unavailable &&
2020-08-07 23:30:12 +01:00
< Block >
2020-09-01 01:58:14 +01:00
< ColumnIndicator type = 'error' message = { unavailableMessage } / >
2020-08-07 23:30:12 +01:00
< / B l o c k >
2020-05-10 04:26:58 +01:00
}
2020-02-17 17:50:29 +00:00
< / P r o f i l e L a y o u t >
2019-07-02 08:10:25 +01:00
)
}
2020-04-08 02:06:59 +01:00
2020-08-14 18:45:59 +01:00
}
const mapStateToProps = ( state , { params : { username } } ) => {
const accounts = state . getIn ( [ 'accounts' ] )
const account = accounts . find ( acct => username . toLowerCase ( ) === acct . getIn ( [ 'acct' ] , '' ) . toLowerCase ( ) )
const accountId = ! ! account ? account . get ( 'id' ) : - 1
const isBlocked = state . getIn ( [ 'relationships' , accountId , 'blocked_by' ] , false )
const isLocked = state . getIn ( [ 'accounts' , accountId , 'locked' ] , false )
const isFollowing = state . getIn ( [ 'relationships' , accountId , 'following' ] , false )
const unavailable = ( me === accountId ) ? false : ( isBlocked || ( isLocked && ! isFollowing ) )
2020-10-30 21:25:33 +00:00
const isMe = me === accountId
2020-08-14 18:45:59 +01:00
const getAccount = makeGetAccount ( )
return {
2020-10-30 21:25:33 +00:00
isMe ,
2020-09-01 01:58:14 +01:00
isBlocked ,
2020-08-14 18:45:59 +01:00
unavailable ,
account : accountId !== - 1 ? getAccount ( state , accountId ) : null ,
}
}
ProfilePage . propTypes = {
account : ImmutablePropTypes . map ,
children : PropTypes . node ,
dispatch : PropTypes . func . isRequired ,
noSidebar : PropTypes . bool ,
params : PropTypes . object . isRequired ,
unavailable : PropTypes . bool . isRequired ,
2020-09-01 01:58:14 +01:00
isBlocked : PropTypes . bool . isRequired ,
2020-08-14 18:45:59 +01:00
}
export default connect ( mapStateToProps ) ( ProfilePage )