Updated TrendsItem component code structure

• Updated:
- TrendsItem component code structure
This commit is contained in:
mgabdev 2020-11-06 23:21:29 -06:00
parent 7b19a4b7c9
commit 83deb5e9e1

@ -1,92 +1,42 @@
import React from 'react'
import PropTypes from 'prop-types'
import ImmutablePureComponent from 'react-immutable-pure-component'
import ImmutablePropTypes from 'react-immutable-proptypes'
import { urlRegex } from '../features/ui/util/url_regex'
import {
CX,
DEFAULT_REL,
} from '../constants'
import { DEFAULT_REL } from '../constants'
import Button from './button'
import DotTextSeperator from './dot_text_seperator'
import RelativeTimestamp from './relative_timestamp'
import Text from './text'
class TrendingItem extends React.PureComponent {
state = {
hovering: false,
}
handleOnMouseEnter = () => {
this.setState({ hovering: true })
}
handleOnMouseLeave = () => {
this.setState({ hovering: false })
}
class TrendsItem extends ImmutablePureComponent {
render() {
const {
index,
isLast,
isHidden,
title,
description,
author,
url,
date,
} = this.props
const { hovering } = this.state
const { trend } = this.props
if (!trend) return null
const title = trend.get('title')
const url = trend.get('trends_url')
if (!title || !url) return null
const correctedTitle = `${title}`.trim()
let correctedDescription = description || ''
const correctedAuthor = `${author}`.replace('www.', '')
let correctedDescription = trend.get('description')
correctedDescription = correctedDescription.length >= 120 ? `${correctedDescription.substring(0, 120).trim()}...` : correctedDescription
const descriptionHasLink = correctedDescription.match(urlRegex)
if (isHidden) {
return (
<React.Fragment>
{correctedTitle}
{!descriptionHasLink && correctedDescription}
{correctedAuthor}
</React.Fragment>
)
}
const containerClasses = CX({
d: 1,
noUnderline: 1,
px15: 1,
pt10: 1,
pb5: 1,
borderColorSecondary: !isLast,
borderBottom1PX: !isLast,
backgroundColorSubtle_onHover: 1,
})
const subtitleClasses = CX({
ml5: 1,
underline: hovering,
})
return (
<Button
noClasses
href={url}
target='_blank'
rel={DEFAULT_REL}
className={containerClasses}
onMouseEnter={() => this.handleOnMouseEnter()}
onMouseLeave={() => this.handleOnMouseLeave()}
className={[_s.d, _s.noUnderline, _s.px15, _s.pt10, _s.pb5, _s.borderBottom1PX, _s.borderColorSecondary, _s.bgSubtle_onHover].join(' ')}
>
<div className={[_s.d, _s.flexNormal, _s.pb5].join(' ')}>
<div className={_s.d}>
<Text size='medium' color='primary' weight='bold'>
{correctedTitle}
{title}
</Text>
</div>
@ -98,25 +48,20 @@ class TrendingItem extends React.PureComponent {
</Text>
</div>
}
<div className={[_s.d, _s.flexRow].join(' ')}>
<Text color='secondary' size='small'>
{index}
{trend.get('feed_base_url')}
</Text>
<DotTextSeperator />
<Text color='secondary' size='small' className={_s.ml5}>
{correctedAuthor}
trends.gab.com
</Text>
<DotTextSeperator />
<Text color='secondary' size='small' className={subtitleClasses}>
<RelativeTimestamp timestamp={date} />
<Text color='secondary' size='small' className={_s.ml5}>
<RelativeTimestamp timestamp={trend.get('publish_date')} />
</Text>
{
hovering &&
<Text color='secondary' size='small' className={_s.ml10}></Text>
}
</div>
</div>
</Button>
)
@ -124,23 +69,8 @@ class TrendingItem extends React.PureComponent {
}
TrendingItem.propTypes = {
index: PropTypes.number,
isLast: PropTypes.bool,
isHidden: PropTypes.bool,
title: PropTypes.string,
description: PropTypes.string,
author: PropTypes.string,
url: PropTypes.string,
date: PropTypes.string,
TrendsItem.propTypes = {
trend: ImmutablePropTypes.map,
}
TrendingItem.defaultProps = {
title: '',
description: '',
author: '',
url: '',
date: '',
}
export default TrendingItem
export default TrendsItem