Skip to content

Commit 4afabfc

Browse files
committed
fix/adapting date formats
1 parent 2937171 commit 4afabfc

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed

src/components/cubes/TableRow.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { useTimerStore } from "@/store/timerStore";
44
import { useCubesModalStore } from "@/store/CubesModalStore";
55
import { useRouter } from "@/navigation";
66
import { getAllCubes, saveCube } from "@/db/dbOperations";
7-
import { useTranslations } from "next-intl";
7+
import {useTranslations } from "next-intl";
88
import {
99
EllipsisHorizontalIcon,
1010
PlayIcon,
1111
StopIcon,
1212
} from "@heroicons/react/24/solid";
13+
import useLocale from "@/hooks/useLocale";
1314

1415
export default function TableRow({ cube }: { cube: Cube }) {
1516
const t = useTranslations("Index.CubesPage");
@@ -33,15 +34,7 @@ export default function TableRow({ cube }: { cube: Cube }) {
3334
setCubes(cubesDB);
3435
};
3536

36-
function formatDate(msDate: number) {
37-
const creationDate = new Date(msDate);
38-
const month = creationDate.getMonth() + 1;
39-
const day = creationDate.getDate();
40-
const year = creationDate.getFullYear();
41-
return `${month.toString().padStart(2, "0")}/${day
42-
.toString()
43-
.padStart(2, "0")}/${year}`;
44-
}
37+
const locale = useLocale(cube.createdAt);
4538

4639
const redirectToHome = (e: any) => {
4740
const targetDiv = e.target;
@@ -78,7 +71,7 @@ export default function TableRow({ cube }: { cube: Cube }) {
7871
{`${cube?.solves?.session.length}/${cube?.solves?.all.length}`}
7972
</div>
8073
<div className="hidden text-center align-middle cursor-pointer md:table-cell">
81-
{formatDate(cube?.createdAt)}
74+
{locale}
8275
</div>
8376
<div className="hidden text-center align-middle md:table-cell">
8477
{cube?.solves?.session.length > 0 ? (

src/components/solves/ModalSolve.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function ModalSolve({ currentTab }: { currentTab: SolveTab }) {
3434
const t = useTranslations("Index.SolvesPage");
3535
const submenuRef = useRef<HTMLDivElement | null>(null);
3636

37-
const formattedDate = useLocale(solve);
37+
const formattedDate = useLocale(solve?.endTime);
3838

3939
useEffect(() => {
4040
setShowOptions(false);

src/hooks/useLocale.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import { Solve } from "@/interfaces/Solve";
21
import { DateTime } from "luxon";
32
import { useEffect, useState } from "react";
43

5-
export default function useLocale(solve: Solve | null): string | null {
4+
export default function useLocale(time: number | undefined): string | null {
65
const [formattedDate, setFormattedDate] = useState<string>("");
76

87
useEffect(() => {
9-
if (solve && solve.endTime) {
10-
const parsedDate = DateTime.fromMillis(solve.endTime)
8+
if (time) {
9+
const parsedDate = DateTime.fromMillis(time)
1110
.setLocale(navigator.language)
1211
.toLocaleString(DateTime.DATE_MED);
1312
setFormattedDate(parsedDate);
1413
}
15-
}, [solve]);
14+
}, [time]);
1615

17-
if (!solve) return null;
16+
if (!time) return null;
1817

1918
return formattedDate;
2019
}

0 commit comments

Comments
 (0)