Skip to content

Commit 4d8f6d6

Browse files
committed
Created Card Component
1 parent b8d3b63 commit 4d8f6d6

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

src/app/components/Card/Card.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Box } from '@mui/material'
2+
import React from 'react'
3+
import { Link } from 'react-router-dom'
4+
import { News } from '../../../types'
5+
import { StyledBoxCard, StyledTypography } from '../../styles/styledComponents'
6+
import { PublishDate } from './PublishDate'
7+
import Arrow from '../../assets/Arrow.svg'
8+
import { CardDescription } from './CardDescription'
9+
10+
export const Card: React.FC<Props> = ({ newsData }) => {
11+
return (
12+
<Link to={`/news/${newsData.title}`} style={{ textDecoration: 'none', color: '#363636' }}>
13+
<StyledBoxCard>
14+
<img
15+
src={newsData.urlToImage}
16+
style={{
17+
height: '217px',
18+
width: '400px',
19+
borderTopLeftRadius: '5px',
20+
borderTopRightRadius: '5px',
21+
}}
22+
/>
23+
<Box
24+
sx={{
25+
height: '270px',
26+
padding: '25px',
27+
display: 'flex',
28+
flexDirection: 'column',
29+
alignItems: 'start',
30+
justifyItems: 'start',
31+
}}
32+
>
33+
<PublishDate publishedAt={newsData.publishedAt} />
34+
<CardDescription newsData={newsData} />
35+
36+
<StyledTypography
37+
sx={{ marginTop: 'auto', alignSelf: 'start', fontWeight: 700, lineHeight: '24px' }}
38+
>
39+
Read More <img src={Arrow} alt='Arrow' />
40+
</StyledTypography>
41+
</Box>
42+
</StyledBoxCard>
43+
</Link>
44+
)
45+
}
46+
47+
type Props = {
48+
newsData: News
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Box } from '@mui/system'
2+
import React from 'react'
3+
import { News } from '../../../types'
4+
import { StyledTypography } from '../../styles/styledComponents'
5+
6+
export const CardDescription: React.FC<Props> = ({ newsData }) => {
7+
return (
8+
<Box>
9+
<StyledTypography
10+
sx={{ fontSize: '24px', fontWeight: '400', lineHeight: '29px', marginY: '20px' }}
11+
>
12+
{getTitle(newsData)}
13+
</StyledTypography>
14+
15+
<StyledTypography sx={{ marginBottom: '20px', fontWeight: 400, lineHeight: '24px' }}>
16+
{getDescription(newsData)}
17+
</StyledTypography>
18+
</Box>
19+
)
20+
}
21+
22+
const getTitle = (newsData: News) => {
23+
if (newsData.title.length > 37) {
24+
return `${newsData.title.split('-')[0].slice(0, 37)}...`
25+
}
26+
return newsData.title
27+
}
28+
29+
const getDescription = (newsData: News) => {
30+
if (newsData.description && newsData.description.length > 139) {
31+
return `${newsData.description.slice(0, 139)}...`
32+
}
33+
return newsData.description
34+
}
35+
36+
type Props = {
37+
newsData: News
38+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react'
2+
import { StyledTypography } from '../../styles/styledComponents'
3+
import CalendarIcon from '../../assets/CalendarIcon.svg'
4+
5+
export const PublishDate: React.FC<Props> = ({ publishedAt }) => {
6+
const fromatedPuglishDate = getFormatedDate(publishedAt)
7+
8+
return (
9+
<StyledTypography sx={{ fontSize: '14px', fontWeight: '400' }}>
10+
<img src={CalendarIcon} width='14px' /> {fromatedPuglishDate}
11+
</StyledTypography>
12+
)
13+
}
14+
15+
const getOrdinalNum = (n: number) => {
16+
return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : '')
17+
}
18+
19+
const getFormatedDate = (publishedAt: string) => {
20+
const monthNames = [
21+
'January',
22+
'February',
23+
'March',
24+
'April',
25+
'May',
26+
'June',
27+
'July',
28+
'August',
29+
'September',
30+
'October',
31+
'November',
32+
'December',
33+
]
34+
35+
const puglishDate = new Date(Date.parse(publishedAt))
36+
return `${monthNames[puglishDate.getMonth()]} ${getOrdinalNum(
37+
puglishDate.getDate(),
38+
)}, ${puglishDate.getFullYear()}`
39+
}
40+
41+
type Props = {
42+
publishedAt: string
43+
}

0 commit comments

Comments
 (0)