From 94f32e21876cd9499f3bb817688c5a4c8beb005a Mon Sep 17 00:00:00 2001 From: Samuel Meuli Date: Fri, 24 Jan 2020 16:39:59 +0700 Subject: [PATCH] Add min/max dates --- .../sidebar/calendar-nav/CalendarNav.tsx | 29 ++++++++++++------- src/renderer/constants.ts | 4 +++ src/renderer/store/diary/actionCreators.ts | 9 +++++- 3 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 src/renderer/constants.ts diff --git a/src/renderer/components/elements/sidebar/calendar-nav/CalendarNav.tsx b/src/renderer/components/elements/sidebar/calendar-nav/CalendarNav.tsx index 3fbfc157..8a28a750 100644 --- a/src/renderer/components/elements/sidebar/calendar-nav/CalendarNav.tsx +++ b/src/renderer/components/elements/sidebar/calendar-nav/CalendarNav.tsx @@ -1,8 +1,9 @@ import PrevIcon from "feather-icons/dist/icons/chevron-left.svg"; import NextIcon from "feather-icons/dist/icons/chevron-right.svg"; import moment from "moment"; -import React, { FunctionComponent } from "react"; +import React, { ReactElement } from "react"; +import { MAX_DATE, MIN_DATE } from "../../../../constants"; import { toMonthYear } from "../../../../utils/dateFormat"; import { translations } from "../../../../utils/i18n"; import { iconProps } from "../../../../utils/icons"; @@ -19,7 +20,7 @@ export interface DispatchProps { type Props = StateProps & DispatchProps; -const CalendarNav: FunctionComponent = (props: Props): JSX.Element => { +export default function CalendarNav(props: Props): ReactElement { const { allowFutureEntries, monthSelected, @@ -27,28 +28,34 @@ const CalendarNav: FunctionComponent = (props: Props): JSX.Element => { setMonthSelectedPrevious, } = props; - const month = moment(monthSelected); - - // If future entries are disallowed: Disable "next" button if current month is reached const today = moment(); - const disableNextButton = !allowFutureEntries && month.isSame(today, "month"); + + // Check if buttons for switching to previous/next month should be enabled. Determined based on + // the min/max dates and whether future diary entries are allowed + const month = moment(monthSelected); + const canClickPrev = month.isAfter(MIN_DATE, "month"); + const canClickNext = + month.isBefore(MAX_DATE, "month") && (allowFutureEntries || month.isBefore(today, "month")); return (
-

{toMonthYear(month)}

); -}; - -export default CalendarNav; +} diff --git a/src/renderer/constants.ts b/src/renderer/constants.ts new file mode 100644 index 00000000..944e57b9 --- /dev/null +++ b/src/renderer/constants.ts @@ -0,0 +1,4 @@ +// Dates + +export const MIN_DATE = new Date(1900, 0, 1); +export const MAX_DATE = new Date(2099, 11, 31); diff --git a/src/renderer/store/diary/actionCreators.ts b/src/renderer/store/diary/actionCreators.ts index ce78834e..71288a65 100644 --- a/src/renderer/store/diary/actionCreators.ts +++ b/src/renderer/store/diary/actionCreators.ts @@ -1,5 +1,6 @@ import moment from "moment"; +import { MAX_DATE, MIN_DATE } from "../../constants"; import { searchIndex } from "../../utils/searchIndex"; import { ThunkActionT } from "../store"; import { @@ -14,10 +15,16 @@ import { // Action creators export function setDateSelected(dateSelected: Date): SetDateSelectedAction { + let dateValidated = dateSelected; + if (dateSelected < MIN_DATE) { + dateValidated = MIN_DATE; + } else if (dateSelected > MAX_DATE) { + dateValidated = MAX_DATE; + } return { type: SET_DATE_SELECTED, payload: { - dateSelected, + dateSelected: dateValidated, }, }; }