From 48227cf4596e2b95be3e7ac8609477bdeb251805 Mon Sep 17 00:00:00 2001 From: Rayce Ramsay Date: Tue, 3 Sep 2024 18:23:21 -0400 Subject: [PATCH] Add error checking for date limits form in dashboard --- frontend/pages/Dashboard.tsx | 85 +++++++++++++------ frontend/styles/Dashboard.module.css | 6 ++ .../DashboardDateLimitComponents.module.css | 1 + 3 files changed, 66 insertions(+), 26 deletions(-) diff --git a/frontend/pages/Dashboard.tsx b/frontend/pages/Dashboard.tsx index 5aa5a64..8241ee6 100644 --- a/frontend/pages/Dashboard.tsx +++ b/frontend/pages/Dashboard.tsx @@ -19,6 +19,7 @@ export default function Dashboard() { const [endDate, setEndDate] = useState(`${currentYear}-12-31`) const [fetchedStartDate, setFetchedStartDate] = useState(startDate) const [fetchedEndDate, setFetchedEndDate] = useState(endDate) + const [dateRangeFormError, setDateRangeFormError] = useState('') const [ getPageData, @@ -35,6 +36,29 @@ export default function Dashboard() { } }, [endDate, getPageData, pageCalled, startDate]) + const hasDateRangeFormError = () => { + setDateRangeFormError('') + + const validDateRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/ + if (!validDateRegex.test(startDate)) { + setDateRangeFormError('Start date is invalid.') + return true + } + if (!validDateRegex.test(endDate)) { + setDateRangeFormError('End date is invalid.') + return true + } + + return false + } + const submitGetPageDataForm = () => { + if (hasDateRangeFormError()) { + return + } + + getPageData({ variables: { startDate, endDate } }) + } + let DashboardContent = (
@@ -173,32 +197,41 @@ export default function Dashboard() { - setStartDate(e.target.value)} - className={styles.dateLimitInput} - max={currentDate} - disabled={pageLoading} - /> - setEndDate(e.target.value)} - className={styles.dateLimitInput} - min={startDate} - max={currentDate} - disabled={pageLoading} - /> - getPageData({ variables: { startDate, endDate } })} - loading={pageLoading} - disabled={pageLoading} - > - Get Clicks - - +
+
+ { + setStartDate(e.target.value) + hasDateRangeFormError() + }} + onFocus={hasDateRangeFormError} + onBlur={hasDateRangeFormError} + className={styles.dateLimitInput} + max={currentDate} + disabled={pageLoading} + /> + { + setEndDate(e.target.value) + hasDateRangeFormError() + }} + onFocus={hasDateRangeFormError} + onBlur={hasDateRangeFormError} + className={styles.dateLimitInput} + min={startDate} + max={currentDate} + disabled={pageLoading} + /> + + Get Clicks + + + {dateRangeFormError ?
{dateRangeFormError}
: null} +
} />
diff --git a/frontend/styles/Dashboard.module.css b/frontend/styles/Dashboard.module.css index 484b6b8..6901c12 100644 --- a/frontend/styles/Dashboard.module.css +++ b/frontend/styles/Dashboard.module.css @@ -26,6 +26,12 @@ gap: 1rem; } +.dateLimitsFormError { + color: #ef8b8b; + padding: 1rem 0; + text-align: center; +} + .quickStats { display: grid; grid-template-columns: minmax(0, 1fr); diff --git a/frontend/styles/components/DashboardDateLimitComponents.module.css b/frontend/styles/components/DashboardDateLimitComponents.module.css index ab90714..a3dd005 100644 --- a/frontend/styles/components/DashboardDateLimitComponents.module.css +++ b/frontend/styles/components/DashboardDateLimitComponents.module.css @@ -35,6 +35,7 @@ border-width: 1px; border-style: solid; border-radius: 0.5rem; + min-width: 9rem; cursor: text; }