Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Commit

Permalink
fix: replace moment with date-fns
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Aug 1, 2019
1 parent 1929ef5 commit c1f8ffd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
8 changes: 1 addition & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"aws-sdk": "^2.503.0",
"bootstrap": "^4.3.1",
"classnames": "^2.2.6",
"date-fns": "^1.30.1",
"leaflet": "^1.5.1",
"moment": "^2.24.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-leaflet": "^2.4.0",
Expand Down
13 changes: 7 additions & 6 deletions src/RelativeTime/RelativeTime.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React, { useState, useEffect } from 'react'
import moment from 'moment'
import { distanceInWords } from 'date-fns'

const getDiffInSeconds = (ts: Date) =>
(Date.now() - ts.getTime()) / 1000
const getDiffInSeconds = (ts: Date) => (Date.now() - ts.getTime()) / 1000

export const RelativeTime = ({ ts }: { ts: Date }) => {
const [label, setLabel] = useState(moment(ts).fromNow())
const [label, setLabel] = useState(
distanceInWords(new Date(), ts, { includeSeconds: true }),
)
const [diffInSeconds, setDiffInSeconds] = useState(getDiffInSeconds(ts))

useEffect(() => {
const updateDiffInSeconds = () => {
const d = getDiffInSeconds(ts)
setDiffInSeconds(d)
setLabel(moment(ts).fromNow())
setLabel(distanceInWords(new Date(), ts, { includeSeconds: true }))
}

let t: NodeJS.Timeout
Expand All @@ -28,5 +29,5 @@ export const RelativeTime = ({ ts }: { ts: Date }) => {
}
}, [diffInSeconds, ts])

return <time dateTime={ts.toISOString()}>{label}</time>
return <time dateTime={ts.toISOString()}>{label} ago</time>
}

0 comments on commit c1f8ffd

Please sign in to comment.