Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1f18ffc

Browse files
authoredOct 13, 2020
Merge pull request #5 from matteoauger/feature/post-submit-time
Feature/post submit time
2 parents 0355739 + 3fce2b7 commit 1f18ffc

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed
 

‎client/src/Components/Screens/Home.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Link } from 'react-router-dom'
44
import Loading from './Loading'
55
import { UserContext } from '../../App'
66
import M from 'materialize-css'
7+
import { relativeDate } from '../../Utils/date-util.js';
78

89
function Home() {
910

@@ -207,7 +208,8 @@ function Home() {
207208
<img src={post.photo} alt="post_img" />
208209
</div>
209210
<div className="card-content">
210-
{
211+
<div class="post-info-stripe">
212+
{
211213
miniLoading ?
212214
<div className="like-loader">
213215
</div>
@@ -221,6 +223,9 @@ function Home() {
221223
}
222224
</div>
223225
}
226+
<div class="post-date">{ relativeDate(post.createdAt) }</div>
227+
</div>
228+
224229
<p className="likes">{post.likes.length} likes</p>
225230
<p>{post.body}</p>
226231

@@ -272,4 +277,4 @@ function Home() {
272277
)
273278
}
274279
}
275-
export default Home
280+
export default Home

‎client/src/Components/Screens/SinglePost.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import '../../Styles/Home.css'
66
import { Link } from 'react-router-dom'
77
import { UserContext } from '../../App'
88
import M from 'materialize-css'
9+
import { relativeDate } from '../../Utils/date-util.js';
910

1011

1112
function SinglePost() {
@@ -156,7 +157,8 @@ function SinglePost() {
156157
<img src={post.photo} alt="post_img" />
157158
</div>
158159
<div className="card-content">
159-
{
160+
<div class="post-info-stripe">
161+
{
160162
miniLoading ?
161163
<div className="like-loader">
162164
</div>
@@ -169,7 +171,10 @@ function SinglePost() {
169171
<i onClick={() => likePost(post._id)} className="like material-icons">thumb_up</i>
170172
}
171173
</div>
172-
}
174+
}
175+
<div class="post-date">{ relativeDate(post.createdAt) }</div>
176+
</div>
177+
173178
<p className="likes">{post.likes.length} likes</p>
174179
<p>{post.body}</p>
175180

@@ -217,4 +222,4 @@ function SinglePost() {
217222
)
218223
}
219224

220-
export default SinglePost
225+
export default SinglePost

‎client/src/Styles/Home.css

+16-1
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,19 @@ color: var(--sec-text) !important;
226226
font-size: 18px;
227227
color: var(--primary-text);
228228
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+
}

‎client/src/Utils/date-util.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.