Skip to content

Commit

Permalink
formatting dates
Browse files Browse the repository at this point in the history
  • Loading branch information
cchamangwana committed Aug 17, 2022
1 parent 85df5e6 commit 15b1c76
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@redwoodjs/router": "2.1.0",
"@redwoodjs/web": "2.1.0",
"daisyui": "^2.19.1",
"date-fns": "^2.29.1",
"prop-types": "15.8.1",
"react": "17.0.2",
"react-dom": "17.0.2",
Expand Down
12 changes: 10 additions & 2 deletions web/src/components/DetailsCell/DetailsCell.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { formatDateToNow } from 'src/utils/formatDate'
import AddToCart from '../AddToCart/AddToCart'
import RemoveFromCart from '../RemoveFromCart/RemoveFromCart'

export const QUERY = gql`
query productQuery($id: String!) {
product(id: $id) {
Expand All @@ -15,6 +17,7 @@ export const QUERY = gql`
reviews {
id
text
createdAt
user {
id
name
Expand Down Expand Up @@ -102,9 +105,14 @@ export const Success = ({ product }) => {
{reviews.map((review) => {
return (
<>
<p key={review.id} className="text-black">
{review.user.name} sayed {review.text}{' '}
<div className="bg-gray-100 p-3 mb-3">
<p key={review.id} className="text-black text-xl">
{review.user.name} said {review.text}{' '}
</p>
<p style={{ zoom: 0.9 }}>
{formatDateToNow(review.createdAt)}
</p>
</div>
</>
)
})}
Expand Down
27 changes: 27 additions & 0 deletions web/src/utils/formatDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
format,
isThisYear,
formatDistanceStrict,
formatDistanceToNow,
} from "date-fns";

export function formatPostDate(date) {
const formatShort = format(new Date(date), "MMMM d").toUpperCase();
// MARCH 23
const formatLong = format(new Date(date), "MMMM d, yyy").toUpperCase();
// FEBRUARY 2, 2019
return isThisYear(new Date(date)) ? formatShort : formatLong;
}

export function formatDateToNow(date) {
return formatDistanceToNow(new Date(date), { addSuffix: true });
}

export function formatDateToNowShort(date) {
// 5 days ago -> 5 days -> ['5', 'days'] -> ['5', 'd'] -> 5d
// 7 weeks ago -> 7w
return formatDistanceStrict(new Date(date), new Date(Date.now()))
.split(" ")
.map((s, i) => (i === 1 ? s[0] : s))
.join("");
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10300,6 +10300,13 @@ __metadata:
languageName: node
linkType: hard

"date-fns@npm:^2.29.1":
version: 2.29.1
resolution: "date-fns@npm:2.29.1"
checksum: 9d6eabcfd5b2c8d8179baaa37a3f7248092d3c3ab1f37994270ea3f3aaf88419991e67c4bfcea4816492ddb2b7a31e106ff70e6089ae881f153d5fd2ef80874c
languageName: node
linkType: hard

"debounce@npm:^1.2.0":
version: 1.2.1
resolution: "debounce@npm:1.2.1"
Expand Down Expand Up @@ -24088,6 +24095,7 @@ __metadata:
"@redwoodjs/web": 2.1.0
autoprefixer: ^10.4.7
daisyui: ^2.19.1
date-fns: ^2.29.1
postcss: ^8.4.14
postcss-loader: ^7.0.1
prop-types: 15.8.1
Expand Down

0 comments on commit 15b1c76

Please sign in to comment.