-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.NewsItem { | ||
padding: 2px 8px; | ||
} | ||
.NewsItem__NumCol { | ||
width: 80px; | ||
text-align: right; | ||
} | ||
@media screen and (min-width: 768px) { | ||
.NewsItem { | ||
padding: 4px 12px; | ||
} | ||
.NewsItem__NumCol { | ||
width: 100px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import TimeAgo from 'javascript-time-ago'; | ||
import en from 'javascript-time-ago/locale/en'; | ||
|
||
import styles from './utility.module.css'; | ||
import mstyle from './NewsItem.module.css'; | ||
|
||
TimeAgo.addLocale(en); | ||
const timeAgo = new TimeAgo('en-US'); | ||
|
||
type Props = { | ||
// | ||
id: number; | ||
title: string; | ||
points: number; | ||
commentCount: number; | ||
source: string; | ||
author: string; // username of the poster | ||
createdAt: string; // timestamp | ||
|
||
onUpvote: (id: number, points: number) => void; | ||
onHide: (id: number) => void; | ||
|
||
visible?: boolean; | ||
alt?: boolean; | ||
}; | ||
|
||
const NewsItem: React.FC<Props> = ({ | ||
commentCount, | ||
points: pointsProp, | ||
title = '', | ||
source = '', | ||
author = '', | ||
createdAt, | ||
id, | ||
onHide = () => {}, | ||
onUpvote = () => {}, | ||
visible: visibleProp = true, | ||
alt = false, | ||
}) => { | ||
const [visible, setVisibility] = React.useState(true); | ||
const [points, setPoints] = React.useState(0); | ||
|
||
React.useEffect(() => { | ||
setPoints(pointsProp); | ||
setVisibility(visibleProp); | ||
return () => {}; | ||
}, [visibleProp, pointsProp]); | ||
|
||
const handleVisibility = () => { | ||
setVisibility(!visible); | ||
onHide(id); | ||
}; | ||
|
||
const handleUpvote = () => { | ||
setPoints((prevPoints) => prevPoints + 1); | ||
onUpvote(id, points); | ||
}; | ||
|
||
return ( | ||
visible && ( | ||
<div className={cx(mstyle.NewsItem, styles.flexR, alt ? styles.bgCararra : styles.bgSpringWood)}> | ||
<div className={styles.flex}> | ||
{/* NewsItem: Comments Count */} | ||
<div className={mstyle.NewsItem__NumCol}>{commentCount > 0 ? commentCount : '-'}</div> | ||
{/* NewsItem: Points & Upvote button */} | ||
<div className={mstyle.NewsItem__NumCol}> | ||
<NewsItemPoint points={points} /> | ||
<button | ||
className={cx(styles.unstyled, styles.fs12, styles.tcDustyGray)} | ||
type="button" | ||
onClick={handleUpvote} | ||
> | ||
▲ | ||
</button> | ||
</div> | ||
{/* NewsItem: Title */} | ||
<div className="NewsItem__title">{title}</div> | ||
</div> | ||
|
||
<div className={cx(styles.flex, styles.fw600, styles.fs14)}> | ||
{/* NewsItem: Source */} | ||
<div className="NewsItem__source"> | ||
<span className={styles.tcSilverLight}>(</span> | ||
<span className={styles.tcGray}> | ||
{source.indexOf('://') !== -1 ? source.split('://')[1].split('/')[0] : source} | ||
</span> | ||
<span className={styles.tcSilverLight}>)</span> | ||
</div> | ||
|
||
{/* NewsItem: Author */} | ||
<div className={styles.NewsItem__author}> | ||
<span className={styles.tcSilverDark}>by</span> {author} | ||
</div> | ||
|
||
{/* NewsItem: CreatedAt */} | ||
<div className={styles.tcGray}>{timeAgo.format(new Date(createdAt))}</div> | ||
|
||
{/* NewsItem: Hide Button */} | ||
<button className={cx(styles.unstyled)} type="button" onClick={handleVisibility}> | ||
<span className={styles.tcStack}> | ||
[<span className={cx(styles.px4, styles.tcBlack)}>hide</span>] | ||
</span> | ||
</button> | ||
</div> | ||
</div> | ||
) | ||
); | ||
}; | ||
|
||
const NewsItemPoint = ({ points }) => ( | ||
<span | ||
className={cx( | ||
points >= 100 && styles.tcBlazeOrange, | ||
points < 100 && points >= 80 && styles.tcFire, | ||
points < 80 && styles.white | ||
)} | ||
> | ||
{points} | ||
</span> | ||
); | ||
|
||
export default NewsItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** Utility classes **/ | ||
.flex { display: flex;} | ||
.flexR { display: flex; flex-direction: column;} | ||
@media screen and (min-width: 768px) { | ||
.flexR { | ||
flex-direction: row; | ||
} | ||
} | ||
|
||
.unstyled { outline: 0; border: 0; background: none;} | ||
|
||
.fs12 { font-size: 12px;} | ||
.fs14 { font-size: 14px;} | ||
|
||
.px4 { padding-left: 4px; padding-right: 4px;} | ||
|
||
.fw500 { font-weight: 500 } | ||
.fw600 { font-weight: 600 } | ||
|
||
.bgCararra { background-color: #e6e6df;} | ||
.bgSpringWood { background-color: #f6f6ef;} | ||
.bgBlazeOrange { background-color: #ff6602;} | ||
|
||
.tcBlazeOrange { color: #ff6602; } | ||
.tcFire { color: #aa4400; } | ||
.tcBlack { color: #000000; } | ||
.tcGray { color: #828282; } | ||
.tcDustyGray { color: #999999; } | ||
.tcStack { color: #8d8f8c } | ||
.tcSilverLight { color: #c9c9c9; } | ||
.tcSilverDark { color: #b8b8b8; } | ||
|
||
/** | ||
- Primary: #ff6602 // blaze-orange | ||
- White: #ffffff | ||
- Black: #000000 | ||
- Bg, row-normal: #f6f6ef // spring-wood | ||
- news row-alt: #e6e6df // cararra | ||
- news upvote: #999999 // dusty-gray | ||
- news source: #828282 // gray | ||
- news source bracket: #c9c9c9 // silver-light | ||
- news source by: #b8b8b8 // silver-dark | ||
- news source time ago: #828282 // gray | ||
- news hide bracket: #8d8f8c // stack | ||
- #aa4400 (above 80) **/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters