Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/post submit time #5

Merged
merged 5 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions client/src/Components/Screens/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Link } from 'react-router-dom'
import Loading from './Loading'
import { UserContext } from '../../App'
import M from 'materialize-css'
import { relativeDate } from '../../Utils/date-util.js';

function Home() {

Expand Down Expand Up @@ -207,7 +208,8 @@ function Home() {
<img src={post.photo} alt="post_img" />
</div>
<div className="card-content">
{
<div class="post-info-stripe">
{
miniLoading ?
<div className="like-loader">
</div>
Expand All @@ -221,6 +223,9 @@ function Home() {
}
</div>
}
<div class="post-date">{ relativeDate(post.createdAt) }</div>
</div>

<p className="likes">{post.likes.length} likes</p>
<p>{post.body}</p>

Expand Down Expand Up @@ -272,4 +277,4 @@ function Home() {
)
}
}
export default Home
export default Home
11 changes: 8 additions & 3 deletions client/src/Components/Screens/SinglePost.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '../../Styles/Home.css'
import { Link } from 'react-router-dom'
import { UserContext } from '../../App'
import M from 'materialize-css'
import { relativeDate } from '../../Utils/date-util.js';


function SinglePost() {
Expand Down Expand Up @@ -156,7 +157,8 @@ function SinglePost() {
<img src={post.photo} alt="post_img" />
</div>
<div className="card-content">
{
<div class="post-info-stripe">
{
miniLoading ?
<div className="like-loader">
</div>
Expand All @@ -169,7 +171,10 @@ function SinglePost() {
<i onClick={() => likePost(post._id)} className="like material-icons">thumb_up</i>
}
</div>
}
}
<div class="post-date">{ relativeDate(post.createdAt) }</div>
</div>

<p className="likes">{post.likes.length} likes</p>
<p>{post.body}</p>

Expand Down Expand Up @@ -217,4 +222,4 @@ function SinglePost() {
)
}

export default SinglePost
export default SinglePost
17 changes: 16 additions & 1 deletion client/src/Styles/Home.css
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,19 @@ color: var(--sec-text) !important;
font-size: 18px;
color: var(--primary-text);
font-weight: 600;
}
}

.card
> .card-content
> .post-info-stripe {
display: flex;
justify-content: space-between;
}

.card
> .card-content
> .post-info-stripe
> .post-date {
color: var(--sec-text);
font-style: italic;
}
31 changes: 31 additions & 0 deletions client/src/Utils/date-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const relativeDate = (timestamp) => {
const NB_DAYS_YEAR = 365; // nb days on a year
const NB_HOURS_DAY = 24; // nb hours on a day
const NB_MINUTES_HOUR = 60; // nb minutes on a hour
const now = new Date();
const utcNow = new Date(now.toUTCString());
const utcDate = new Date(new Date(timestamp).toUTCString())
const diffMs = utcNow.getTime() - utcDate.getTime()

const diffMinutes = Math.floor(diffMs / 60000);
const diffHours = Math.floor(diffMinutes / NB_MINUTES_HOUR);
const diffDays = Math.floor(diffHours / NB_HOURS_DAY);
const diffYears = Math.floor(diffDays / NB_DAYS_YEAR);

if (diffMinutes < 2) return "Posted a few minutes ago";

if (diffMinutes < NB_MINUTES_HOUR) return `Posted ${diffMinutes} minutes ago`;

if (diffHours < NB_HOURS_DAY) {
const hourLabel = diffHours === 1 ? "hour" : "hours";
return `Posted ${diffHours} ${hourLabel} ago`;
}

if (diffDays < NB_DAYS_YEAR) {
const dayLabel = diffDays === 1 ? "day" : "days";
return `Posted ${diffDays} ${dayLabel} ago`;
}

const yearLabel = diffYears === 1 ? "year" : "years";
return `Posted ${diffYears} ${yearLabel} ago`;
};