From aec5927d316e8995924087dd85710f263a95aed3 Mon Sep 17 00:00:00 2001 From: Martin MacDonald <79853687+mmacdo54@users.noreply.github.com> Date: Wed, 5 Jun 2024 12:52:07 +0100 Subject: [PATCH] feat: add hook for generating different date times (#3524) * Added hook for generating different date times * Fixed up logic to destructure the datetime params --------- Co-authored-by: Martin MacDonald --- .../components/MemoEditor/MemoEditorDialog.tsx | 15 ++++++++------- web/src/hooks/index.ts | 1 + web/src/hooks/useDateTime.ts | 13 +++++++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 web/src/hooks/useDateTime.ts diff --git a/web/src/components/MemoEditor/MemoEditorDialog.tsx b/web/src/components/MemoEditor/MemoEditorDialog.tsx index dc26fb0d..3adb7fca 100644 --- a/web/src/components/MemoEditor/MemoEditorDialog.tsx +++ b/web/src/components/MemoEditor/MemoEditorDialog.tsx @@ -1,7 +1,8 @@ import { IconButton } from "@mui/joy"; import clsx from "clsx"; -import { useEffect, useRef, useState } from "react"; +import { useEffect, useRef } from "react"; import useCurrentUser from "@/hooks/useCurrentUser"; +import useDateTime from "@/hooks/useDateTime"; import { useMemoStore, useTagStore } from "@/store/v1"; import { Memo } from "@/types/proto/api/v1/memo_service"; import MemoEditor, { Props as MemoEditorProps } from "."; @@ -21,7 +22,7 @@ const MemoEditorDialog: React.FC = ({ }: Props) => { const tagStore = useTagStore(); const memoStore = useMemoStore(); - const [displayTime, setDisplayTime] = useState(memoStore.getMemoByName(memoName || "")?.displayTime?.toISOString()); + const { setDateTime, displayDateTime, datePickerDateTime } = useDateTime(memoStore.getMemoByName(memoName || "")?.displayTime); const memoPatchRef = useRef>({ displayTime: memoStore.getMemoByName(memoName || "")?.displayTime, }); @@ -32,7 +33,7 @@ const MemoEditorDialog: React.FC = ({ }, []); const updateDisplayTime = (displayTime: string) => { - setDisplayTime(displayTime); + setDateTime(displayTime); memoPatchRef.current.displayTime = new Date(displayTime); }; @@ -50,14 +51,14 @@ const MemoEditorDialog: React.FC = ({ return ( <>
-
- {displayTime ? ( +
+ {displayDateTime ? (
- {new Date(displayTime).toLocaleString()} + {displayDateTime} e.target.showPicker()} onChange={(e) => updateDisplayTime(e.target.value)} /> diff --git a/web/src/hooks/index.ts b/web/src/hooks/index.ts index 881f9458..b8af6852 100644 --- a/web/src/hooks/index.ts +++ b/web/src/hooks/index.ts @@ -4,3 +4,4 @@ export * from "./useNavigateTo"; export * from "./useAsyncEffect"; export * from "./useFilterWithUrlParams"; export * from "./useResponsiveWidth"; +export * from "./useDateTime"; diff --git a/web/src/hooks/useDateTime.ts b/web/src/hooks/useDateTime.ts new file mode 100644 index 00000000..0c5aaec9 --- /dev/null +++ b/web/src/hooks/useDateTime.ts @@ -0,0 +1,13 @@ +import { useState } from "react"; + +const useDateTime = (initalState?: Date) => { + const [dateTime, setDateTimeInternal] = useState(initalState && new Date(initalState)); + + return { + setDateTime: (dateTimeString: string) => setDateTimeInternal(new Date(dateTimeString)), + displayDateTime: dateTime && dateTime.toLocaleString(), + datePickerDateTime: dateTime && new Date(dateTime.getTime() - dateTime.getTimezoneOffset() * 60000).toISOString().split(".")[0], + }; +}; + +export default useDateTime;