Skip to content
This repository has been archived by the owner on Jul 12, 2021. It is now read-only.

Commit

Permalink
Add min/max dates
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmeuli committed Jan 24, 2020
1 parent cc0561a commit 94f32e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -19,36 +20,42 @@ export interface DispatchProps {

type Props = StateProps & DispatchProps;

const CalendarNav: FunctionComponent<Props> = (props: Props): JSX.Element => {
export default function CalendarNav(props: Props): ReactElement {
const {
allowFutureEntries,
monthSelected,
setMonthSelectedNext,
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 (
<div className="calendar-nav">
<button type="button" className="button button-invisible" onClick={setMonthSelectedPrevious}>
<button
type="button"
className="button button-invisible"
disabled={!canClickPrev}
onClick={setMonthSelectedPrevious}
>
<PrevIcon {...iconProps} title={translations["previous-month"]} />
</button>
<h1 className="month-name">{toMonthYear(month)}</h1>
<button
type="button"
className="button button-invisible"
disabled={disableNextButton}
disabled={!canClickNext}
onClick={setMonthSelectedNext}
>
<NextIcon {...iconProps} title={translations["next-month"]} />
</button>
</div>
);
};

export default CalendarNav;
}
4 changes: 4 additions & 0 deletions src/renderer/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Dates

export const MIN_DATE = new Date(1900, 0, 1);
export const MAX_DATE = new Date(2099, 11, 31);
9 changes: 8 additions & 1 deletion src/renderer/store/diary/actionCreators.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
},
};
}
Expand Down

0 comments on commit 94f32e2

Please sign in to comment.