HomeCalendar.js 2.97 KB
import React from "react";
import styled from "styled-components";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faArrowLeft, faArrowRight } from "@fortawesome/free-solid-svg-icons";
import moment from "moment";

const HomeCalendarWrapper = styled.div`
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 50%;
  width: 100%;
  background-color: white;
  opacity: 0.8;
  border-radius: 10px;
`;

const CalenderBox = styled.div`
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
`;

const CalenderMenuBox = styled.div`
  display: flex;
  flex-direction: row;
  justify-content: space-evenly;
  align-items: center;
  height: 15%;
  width: 100%;
`;

const DateInfoSpan = styled.span`
  font-size: 15px;
  font-family: "Kanit", sans-serif;
`;

const DateBodyBox = styled.div`
  height: 85%;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
`;

const DateRow = styled.div`
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
`;

const DateText = styled.span`
  text-align: center;
  font-family: "Overpass", sans-serif;
  color: ${(props) => {
    if (props.className === "Sunday") return "#EA2027";
    else if (props.className === "Saturday") return "#0652DD";
  }};
`;

export default () => {
  const generate = () => {
    const today = moment();
    const startWeek = today.clone().startOf("month").week();
    const endWeek =
      today.clone().endOf("month").week() === 1
        ? 53
        : today.clone().endOf("month").week();
    let calendar = [];
    for (let week = startWeek; week <= endWeek; week++) {
      calendar.push(
        <DateRow key={week}>
          {Array(7)
            .fill(0)
            .map((n, i) => {
              let current = today
                .clone()
                .week(week)
                .startOf("week")
                .add(n + i, "day");
              return (
                <DateText className={`text`} key={i}>
                  {current.format("D")}
                </DateText>
              );
            })}
        </DateRow>
      );
    }
    return calendar;
  };

  return (
    <HomeCalendarWrapper>
      <CalenderBox>
        <CalenderMenuBox>
          <FontAwesomeIcon icon={faArrowLeft} />
          <DateInfoSpan>May 2020</DateInfoSpan>
          <FontAwesomeIcon icon={faArrowRight} />
        </CalenderMenuBox>
        <DateBodyBox>
          <DateRow>
            <DateText className="Sunday">SUN</DateText>
            <DateText>MON</DateText>
            <DateText>TUE</DateText>
            <DateText>WED</DateText>
            <DateText>THU</DateText>
            <DateText>FRI</DateText>
            <DateText className="Saturday">SAT</DateText>
          </DateRow>
          {generate()}
        </DateBodyBox>
      </CalenderBox>
    </HomeCalendarWrapper>
  );
};