02_lesson: Add timestamp for each post
parent
75f98755b7
commit
3b6ee94253
|
@ -9,6 +9,7 @@
|
|||
"version": "0.1.0",
|
||||
"dependencies": {
|
||||
"@reduxjs/toolkit": "^1.8.0",
|
||||
"date-fns": "^2.29.3",
|
||||
"prettier": "^2.8.4",
|
||||
"react": "^17.0.2",
|
||||
"react-dom": "^17.0.2",
|
||||
|
@ -5656,6 +5657,18 @@
|
|||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/date-fns": {
|
||||
"version": "2.29.3",
|
||||
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz",
|
||||
"integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==",
|
||||
"engines": {
|
||||
"node": ">=0.11"
|
||||
},
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/date-fns"
|
||||
}
|
||||
},
|
||||
"node_modules/debug": {
|
||||
"version": "4.3.4",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
|
||||
|
@ -19905,6 +19918,11 @@
|
|||
"whatwg-url": "^8.0.0"
|
||||
}
|
||||
},
|
||||
"date-fns": {
|
||||
"version": "2.29.3",
|
||||
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz",
|
||||
"integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA=="
|
||||
},
|
||||
"debug": {
|
||||
"version": "4.3.4",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
"private": true,
|
||||
"dependencies": {
|
||||
"@reduxjs/toolkit": "^1.8.0",
|
||||
"date-fns": "^2.29.3",
|
||||
"prettier": "^2.8.4",
|
||||
"react": "^17.0.2",
|
||||
"react-dom": "^17.0.2",
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { useSelector } from "react-redux";
|
||||
import { selectAllPosts } from "./postsSlice";
|
||||
import PostAuthor from "./PostAuthor";
|
||||
import TimeAgo from "./TimeAgo";
|
||||
|
||||
const PostsList = () => {
|
||||
const posts = useSelector(selectAllPosts);
|
||||
|
@ -11,6 +12,7 @@ const PostsList = () => {
|
|||
<p>{post.content.substring(0, 100)}</p>
|
||||
<p className="postCredit">
|
||||
<PostAuthor userId={post.userId} />
|
||||
<TimeAgo timestamp={post.date} />
|
||||
</p>
|
||||
</article>
|
||||
));
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
import { parseISO, formatDistanceToNow } from "date-fns";
|
||||
|
||||
const TimeAgo = ({ timestamp }) => {
|
||||
let timeAgo = "";
|
||||
if (timestamp) {
|
||||
const date = parseISO(timestamp);
|
||||
const timePeriod = formatDistanceToNow(date);
|
||||
timeAgo = `${timePeriod} ago`;
|
||||
}
|
||||
|
||||
return (
|
||||
<span title={timestamp}>
|
||||
<i>{timeAgo}</i>
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
export default TimeAgo;
|
|
@ -1,15 +1,18 @@
|
|||
import { createSlice, nanoid } from "@reduxjs/toolkit";
|
||||
import { sub } from "date-fns";
|
||||
|
||||
const initialState = [
|
||||
{
|
||||
id: "1",
|
||||
title: "Learning Redux Toolkit",
|
||||
content: "I've heard good things.",
|
||||
date: sub(new Date(), { minutes: 10 }).toISOString(),
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
title: "Slice...",
|
||||
content: "The more I say slice, the more I want pizza.",
|
||||
date: sub(new Date(), { minutes: 5 }).toISOString(),
|
||||
},
|
||||
];
|
||||
|
||||
|
@ -29,6 +32,7 @@ const postsSlice = createSlice({
|
|||
title,
|
||||
content,
|
||||
userId,
|
||||
date: new Date().toISOString(),
|
||||
},
|
||||
};
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue