File tree 4 files changed +62
-6
lines changed
4 files changed +62
-6
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import { Link } from 'react-router-dom'
4
4
import Loading from './Loading'
5
5
import { UserContext } from '../../App'
6
6
import M from 'materialize-css'
7
+ import { relativeDate } from '../../Utils/date-util.js' ;
7
8
8
9
function Home ( ) {
9
10
@@ -207,7 +208,8 @@ function Home() {
207
208
< img src = { post . photo } alt = "post_img" />
208
209
</ div >
209
210
< div className = "card-content" >
210
- {
211
+ < div class = "post-info-stripe" >
212
+ {
211
213
miniLoading ?
212
214
< div className = "like-loader" >
213
215
</ div >
@@ -221,6 +223,9 @@ function Home() {
221
223
}
222
224
</ div >
223
225
}
226
+ < div class = "post-date" > { relativeDate ( post . createdAt ) } </ div >
227
+ </ div >
228
+
224
229
< p className = "likes" > { post . likes . length } likes</ p >
225
230
< p > { post . body } </ p >
226
231
@@ -272,4 +277,4 @@ function Home() {
272
277
)
273
278
}
274
279
}
275
- export default Home
280
+ export default Home
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import '../../Styles/Home.css'
6
6
import { Link } from 'react-router-dom'
7
7
import { UserContext } from '../../App'
8
8
import M from 'materialize-css'
9
+ import { relativeDate } from '../../Utils/date-util.js' ;
9
10
10
11
11
12
function SinglePost ( ) {
@@ -156,7 +157,8 @@ function SinglePost() {
156
157
< img src = { post . photo } alt = "post_img" />
157
158
</ div >
158
159
< div className = "card-content" >
159
- {
160
+ < div class = "post-info-stripe" >
161
+ {
160
162
miniLoading ?
161
163
< div className = "like-loader" >
162
164
</ div >
@@ -169,7 +171,10 @@ function SinglePost() {
169
171
< i onClick = { ( ) => likePost ( post . _id ) } className = "like material-icons" > thumb_up</ i >
170
172
}
171
173
</ div >
172
- }
174
+ }
175
+ < div class = "post-date" > { relativeDate ( post . createdAt ) } </ div >
176
+ </ div >
177
+
173
178
< p className = "likes" > { post . likes . length } likes</ p >
174
179
< p > { post . body } </ p >
175
180
@@ -217,4 +222,4 @@ function SinglePost() {
217
222
)
218
223
}
219
224
220
- export default SinglePost
225
+ export default SinglePost
Original file line number Diff line number Diff line change @@ -226,4 +226,19 @@ color: var(--sec-text) !important;
226
226
font-size : 18px ;
227
227
color : var (--primary-text );
228
228
font-weight : 600 ;
229
- }
229
+ }
230
+
231
+ .card
232
+ > .card-content
233
+ > .post-info-stripe {
234
+ display : flex;
235
+ justify-content : space-between;
236
+ }
237
+
238
+ .card
239
+ > .card-content
240
+ > .post-info-stripe
241
+ > .post-date {
242
+ color : var (--sec-text );
243
+ font-style : italic;
244
+ }
Original file line number Diff line number Diff line change
1
+ export const relativeDate = ( timestamp ) => {
2
+ const NB_DAYS_YEAR = 365 ; // nb days on a year
3
+ const NB_HOURS_DAY = 24 ; // nb hours on a day
4
+ const NB_MINUTES_HOUR = 60 ; // nb minutes on a hour
5
+ const now = new Date ( ) ;
6
+ const utcNow = new Date ( now . toUTCString ( ) ) ;
7
+ const utcDate = new Date ( new Date ( timestamp ) . toUTCString ( ) )
8
+ const diffMs = utcNow . getTime ( ) - utcDate . getTime ( )
9
+
10
+ const diffMinutes = Math . floor ( diffMs / 60000 ) ;
11
+ const diffHours = Math . floor ( diffMinutes / NB_MINUTES_HOUR ) ;
12
+ const diffDays = Math . floor ( diffHours / NB_HOURS_DAY ) ;
13
+ const diffYears = Math . floor ( diffDays / NB_DAYS_YEAR ) ;
14
+
15
+ if ( diffMinutes < 2 ) return "Posted a few minutes ago" ;
16
+
17
+ if ( diffMinutes < NB_MINUTES_HOUR ) return `Posted ${ diffMinutes } minutes ago` ;
18
+
19
+ if ( diffHours < NB_HOURS_DAY ) {
20
+ const hourLabel = diffHours === 1 ? "hour" : "hours" ;
21
+ return `Posted ${ diffHours } ${ hourLabel } ago` ;
22
+ }
23
+
24
+ if ( diffDays < NB_DAYS_YEAR ) {
25
+ const dayLabel = diffDays === 1 ? "day" : "days" ;
26
+ return `Posted ${ diffDays } ${ dayLabel } ago` ;
27
+ }
28
+
29
+ const yearLabel = diffYears === 1 ? "year" : "years" ;
30
+ return `Posted ${ diffYears } ${ yearLabel } ago` ;
31
+ } ;
You can’t perform that action at this time.
0 commit comments