Skip to content

Commit

Permalink
fix: refactor dates logic (#140)
Browse files Browse the repository at this point in the history
* fix: refactor dates logic

* feat: changes to dates formatter
  • Loading branch information
wainola authored Jan 23, 2024
1 parent e8b972c commit ceb726a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
56 changes: 55 additions & 1 deletion src/utils/Helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it, expect } from "vitest"
import { formatDistanceStrict, sub } from "date-fns"
import { SharedConfigDomain } from "../types"
import { getFormatedFee } from "./Helpers"
import { formatDistanceDate, getFormatedFee } from "./Helpers"

describe("Helpers", () => {
describe("getFormatedFee", () => {
Expand Down Expand Up @@ -35,4 +36,57 @@ describe("Helpers", () => {
expect(formatedFee).toEqual("0.1 PHA")
})
})

describe("formatDistanceDate", () => {
const wholeYear = sub(new Date(), { years: 1, days: 1 })
const yearAndSomeDays = sub(new Date(), { years: 1, days: 14, hours: 7 })
const yearAndSomeMonths = sub(new Date(), { years: 1, months: 2, hours: 10 })

it.only("should return formated date days + hours", () => {
const formattedDate = formatDistanceDate(wholeYear.toISOString())
const distanceInDays = formatDistanceStrict(new Date(), wholeYear, { unit: "day" })
const formattedDate2 = formatDistanceDate(yearAndSomeDays.toISOString())
const distanceInDays2 = formatDistanceStrict(new Date(), yearAndSomeDays, { unit: "day" })
const formattedDate3 = formatDistanceDate(yearAndSomeMonths.toISOString())
const distanceInDays3 = formatDistanceStrict(new Date(), yearAndSomeMonths, { unit: "day" })

expect(formattedDate.includes(distanceInDays)).toBe(true)
expect(formattedDate.includes("hours ago")).toBe(false)
expect(formattedDate2.includes(distanceInDays2)).toBe(true)
expect(formattedDate2.includes("hours ago")).toBe(true)
expect(formattedDate3.includes(distanceInDays3)).toBe(true)
expect(formattedDate3.includes("hours ago")).toBe(true)
})

it("should return just days if date is between one month", () => {
const monthAgo = sub(new Date(), { months: 1 })
const formattedDate = formatDistanceDate(monthAgo.toISOString())
const distanceInDays = formatDistanceStrict(new Date(), monthAgo, { unit: "day" })
expect(formattedDate.includes(distanceInDays)).toBe(true)
})

it("should return days + hours if date is between one month and one year", () => {
const twoMonthsAgo = sub(new Date(), { months: 2, hours: 6 })
const formattedDate = formatDistanceDate(twoMonthsAgo.toISOString())
const distanceInDays = formatDistanceStrict(new Date(), twoMonthsAgo, { unit: "day" })
expect(formattedDate.includes(distanceInDays)).toBe(true)
expect(formattedDate.includes("hours ago")).toBe(true)
})

it("should return hours + minutes if date is less than a day", () => {
const twoHoursAgo = sub(new Date(), { hours: 2, minutes: 6 })
const formattedDate = formatDistanceDate(twoHoursAgo.toISOString())
const distanceInHours = formatDistanceStrict(new Date(), twoHoursAgo, { unit: "hour" })
expect(formattedDate.includes(distanceInHours)).toBe(true)
expect(formattedDate.includes("minutes ago")).toBe(true)
})

it("should return minutes ago if date is less than an hour", () => {
const twoMinutesAgo = sub(new Date(), { minutes: 2, seconds: 6 })
const formattedDate = formatDistanceDate(twoMinutesAgo.toISOString())
const distanceInMinutes = formatDistanceStrict(new Date(), twoMinutesAgo, { unit: "minute" })
expect(formattedDate.includes(distanceInMinutes)).toBe(true)
expect(formattedDate.includes("minutes ago")).toBe(true)
})
})
})
27 changes: 15 additions & 12 deletions src/utils/Helpers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs from "dayjs"
import { intervalToDuration } from "date-fns"
import { intervalToDuration, formatDistanceStrict, formatDuration } from "date-fns"

import { BigNumberish, ethers } from "ethers"
import { DomainTypes, ResourceTypes, SharedConfigDomain, Transfer } from "../types"
Expand Down Expand Up @@ -159,19 +159,22 @@ export const sanitizeTransferData = (transfers: Transfer[]): Transfer[] => {

export const formatDistanceDate = (timestamp: string): string => {
const intervalToDurationResult = intervalToDuration({ start: new Date(timestamp), end: new Date() })

const { days, hours, minutes } = intervalToDurationResult

let dateIntervalResult: string
if (days !== undefined && days > 0) {
dateIntervalResult = `${days !== undefined ? `${days} days` : ""} ${hours !== undefined && hours > 0 ? `${hours} hours` : ""}`
const distanceInDays = formatDistanceStrict(new Date(timestamp), new Date(), { unit: "day" })
const { years, months, days, hours, minutes } = intervalToDurationResult

if (years !== 0 || months !== 0) {
const formatOptions = ["hours"]
const formatInterval = formatDuration(intervalToDurationResult, { format: formatOptions })
const formatedString = `${distanceInDays} ${formatInterval !== "" ? `and ${formatInterval} ago` : ""}`.trim()
return formatedString
} else if (days !== 0) {
console.log("here", days, hours)
const formatedString = hours !== 0 ? `${distanceInDays} and ${hours!} hours ago` : `${distanceInDays} ago`
return formatedString
} else {
dateIntervalResult = `${hours !== undefined && hours > 0 ? `${hours} hours` : ""} ${
minutes !== undefined && minutes > 0 ? `${minutes} minutes` : ""
}`
const formatedString = hours !== 0 ? `${hours!} hours and ${minutes!} minutes ago` : `${minutes!} minutes ago`
return formatedString
}

return dateIntervalResult
}

export const getFormatedFee = (fee: Transfer["fee"] | string, domain: SharedConfigDomain): string => {
Expand Down

0 comments on commit ceb726a

Please sign in to comment.