02_lesson: Add timestamp for each post

02_lesson
Jason Zhu 2023-03-07 13:50:23 +11:00
parent 75f98755b7
commit 3b6ee94253
5 changed files with 43 additions and 0 deletions

View File

@ -9,6 +9,7 @@
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"@reduxjs/toolkit": "^1.8.0", "@reduxjs/toolkit": "^1.8.0",
"date-fns": "^2.29.3",
"prettier": "^2.8.4", "prettier": "^2.8.4",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",
@ -5656,6 +5657,18 @@
"node": ">=10" "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": { "node_modules/debug": {
"version": "4.3.4", "version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
@ -19905,6 +19918,11 @@
"whatwg-url": "^8.0.0" "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": { "debug": {
"version": "4.3.4", "version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",

View File

@ -4,6 +4,7 @@
"private": true, "private": true,
"dependencies": { "dependencies": {
"@reduxjs/toolkit": "^1.8.0", "@reduxjs/toolkit": "^1.8.0",
"date-fns": "^2.29.3",
"prettier": "^2.8.4", "prettier": "^2.8.4",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",

View File

@ -1,6 +1,7 @@
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { selectAllPosts } from "./postsSlice"; import { selectAllPosts } from "./postsSlice";
import PostAuthor from "./PostAuthor"; import PostAuthor from "./PostAuthor";
import TimeAgo from "./TimeAgo";
const PostsList = () => { const PostsList = () => {
const posts = useSelector(selectAllPosts); const posts = useSelector(selectAllPosts);
@ -11,6 +12,7 @@ const PostsList = () => {
<p>{post.content.substring(0, 100)}</p> <p>{post.content.substring(0, 100)}</p>
<p className="postCredit"> <p className="postCredit">
<PostAuthor userId={post.userId} /> <PostAuthor userId={post.userId} />
<TimeAgo timestamp={post.date} />
</p> </p>
</article> </article>
)); ));

View File

@ -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}>
&nbsp; <i>{timeAgo}</i>
</span>
);
};
export default TimeAgo;

View File

@ -1,15 +1,18 @@
import { createSlice, nanoid } from "@reduxjs/toolkit"; import { createSlice, nanoid } from "@reduxjs/toolkit";
import { sub } from "date-fns";
const initialState = [ const initialState = [
{ {
id: "1", id: "1",
title: "Learning Redux Toolkit", title: "Learning Redux Toolkit",
content: "I've heard good things.", content: "I've heard good things.",
date: sub(new Date(), { minutes: 10 }).toISOString(),
}, },
{ {
id: "2", id: "2",
title: "Slice...", title: "Slice...",
content: "The more I say slice, the more I want pizza.", 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, title,
content, content,
userId, userId,
date: new Date().toISOString(),
}, },
}; };
}, },