guild_front/src/components/Calendar/CalendarComponent.js

99 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-06-17 18:21:48 +03:00
import React, { useState, useEffect } from 'react';
import style from './CalendarComponent.module.css';
import ellipse from '../../images/ellipse.png';
import rectangle from '../../images/rectangle__calendar.png';
2021-06-18 17:16:08 +03:00
import calendarIcon from '../../images/calendar_icon.png';
2021-06-17 18:21:48 +03:00
import moment from 'moment';
import 'moment/locale/ru';
2021-06-22 18:00:52 +03:00
import { calendarHelper, currentMonthAndDay } from './calendarHelper';
2021-06-17 18:21:48 +03:00
const CalendarComponent = () => {
const [value, setValue] = useState(moment());
const [calendar, setCalendar] = useState([]);
useEffect(() => {
setCalendar(calendarHelper(value));
}, [value]);
2021-06-18 17:16:08 +03:00
function beforeToday(day) {
return day.isBefore(new Date(), 'day');
2021-06-17 18:21:48 +03:00
}
2021-06-18 17:16:08 +03:00
function isToday(day) {
return day.isSame(new Date(), 'day');
}
2021-06-17 18:21:48 +03:00
2021-06-18 17:16:08 +03:00
function dayStyles(day) {
if (beforeToday(day)) return `${style.before}`;
if (isToday(day)) return `${style.today}`;
if (day.day() === 6 || day.day() === 0) return `${style.selected}`;
return '';
2021-06-17 18:21:48 +03:00
}
function prevMonth() {
return value.clone().subtract(1, 'month');
}
function nextMonth() {
2021-06-23 13:17:59 +03:00
return value.clone().subtract(2, 'month');
2021-06-17 18:21:48 +03:00
}
2021-06-23 13:17:59 +03:00
const prevMonthFirst = () => {
return moment().subtract(1, 'month').format('MMMM');
};
const prevMonthSecond = () => {
return moment().subtract(2, 'month').format('MMMM');
};
2021-06-17 18:21:48 +03:00
return (
<div className={style.CalendarComponent}>
<div className={style.CalendarComponent__header}>
<h3>Мои отчеты</h3>
<div className={style.CalendarComponent__header__box}>
<img src={ellipse} alt="" />
2021-06-23 13:17:59 +03:00
<span onClick={() => setValue(prevMonth())}>{prevMonthFirst()}</span>
2021-06-17 18:21:48 +03:00
</div>
<div className={style.CalendarComponent__header__box}>
<img src={ellipse} alt="" />
2021-06-23 13:17:59 +03:00
<span onClick={() => setValue(nextMonth())}>{prevMonthSecond()}</span>
2021-06-17 18:21:48 +03:00
</div>
</div>
<div className={style.CalendarComponent__rectangle}>
<img src={rectangle} alt="" />
</div>
<div className={style.CalendarComponent__body}>
<div>
2021-06-21 17:41:26 +03:00
<p>Пн</p>
<p>Вт</p>
<p>Ср</p>
<p>Чт</p>
<p>Пт</p>
<p>Сб</p>
<p>Вс</p>
2021-06-17 18:21:48 +03:00
</div>
<div className={style.CalendarComponent__form}>
{calendar.map((week) =>
week.map((day) => (
2021-06-18 17:16:08 +03:00
<button
onClick={() => setValue(day)}
key={day}
className={dayStyles(day)}
name={day.format('dddd')}
id="btn"
>
<img className={style.calendarIcon} src={calendarIcon} alt="" />
2021-06-23 12:48:02 +03:00
{currentMonthAndDay(day)}
2021-06-17 18:21:48 +03:00
</button>
))
)}
</div>
</div>
</div>
);
};
export default CalendarComponent;