From 91b3473f4b530c746c5afe8c3d0dc5673735132d Mon Sep 17 00:00:00 2001 From: nzambello Date: Tue, 21 Dec 2021 19:00:28 +0100 Subject: [PATCH] fix: handle future dates --- src/App.tsx | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index a5b43e1..1b62a2f 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,14 @@ import { useCallback, useState, useRef, useEffect } from "react"; import { usePosition } from "use-position"; import { getDistance } from "geolib"; -import { format, parse, differenceInMilliseconds, isMatch } from "date-fns"; +import { + format, + parse, + differenceInMilliseconds, + isMatch, + isPast, + add, +} from "date-fns"; import useStayAwake from "use-stay-awake"; import { TextField, Button, Dialog, Card } from "ui-neumorphism"; @@ -157,9 +164,11 @@ function App() { placeholder="08:00" value={alarm ? format(alarm, "HH:mm") : undefined} onChange={({ value }: { value: string }) => { - console.log(value); if (isMatch(value, "HH:mm") || isMatch(value, "H:mm")) { - setAlarm(parse(value, "HH:mm", new Date())); + const parsedValue = parse(value, "HH:mm", new Date()); + if (isPast(parsedValue)) + setAlarm(add(parsedValue, { days: 1 })); + else setAlarm(parsedValue); } }} />