import React, { Component, Fragment } from "react";
import PropTypes from "prop-types";
import * as Styled from "./styles";
import calendar, {
isDate,
isSameDay,
isSameMonth,
getDateISO,
getNextMonth,
getPreviousMonth,
WEEK_DAYS,
CALENDAR_MONTHS
} from "../../helpers/calendar";
class Calendar extends Component {
state = { ...this.resolveStateFromProp(), today: new Date() };
resolveStateFromDate(date) {
const isDateObject = isDate(date);
const _date = isDateObject ? date : new Date();
// Render the month and year header with arrow controls
// for navigating through months and years
renderMonthAndYear = () => {
const { month, year } = this.state;
// Resolve the month name from the CALENDAR_MONTHS object map
const monthname = Object.keys(CALENDAR_MONTHS)[
Math.max(0, Math.min(month - 1, 11))
];
// Render the label for day of the week
// This method is used as a map callback as seen in render()
renderDayLabel = (day, index) => {
// Resolve the day of the week label from the WEEK_DAYS object map
const daylabel = WEEK_DAYS[day].toUpperCase();
// Render a calendar date as returned from the calendar builder function
// This method is used as a map callback as seen in render()
renderCalendarDate = (date, index) => {
const { current, month, year, today } = this.state;
const _date = new Date(date.join("-"));
// Check if calendar date is same day as today
const isToday = isSameDay(_date, today);
// Check if calendar date is same day as currently selected date
const isCurrent = current && isSameDay(_date, current);
// Check if calendar date is in the same month as the state month and year
const inMonth = month && year && isSameMonth(_date, new Date([year, month, 1].join("-")));
// The click handler
const onClick = this.gotoDate(_date);