qcal/src/Calendar.tsx

132 lines
4.8 KiB
TypeScript
Raw Normal View History

2022-02-12 13:02:31 -05:00
import React from 'react';
2022-02-12 14:55:46 -05:00
import './Calendar.scss';
import {dateName, Day, decadeNames, frIsLeap, frJDN, jdnGregorian, jdnLongCount, Month, monthName} from './dates';
2022-02-12 13:02:31 -05:00
type MonthProps = {
year: number;
month: Month;
};
type DateProps = MonthProps & {
day: Day;
};
2022-02-12 15:40:45 -05:00
export type CalendarProps = MonthProps & {
2022-02-12 15:09:24 -05:00
todayJDN: number;
2022-02-12 15:40:45 -05:00
onSwitch?: (year: number, month: Month) => void,
2022-02-12 13:02:31 -05:00
};
type CalendarState = {};
function DecadeName({name}: { name: string }): JSX.Element {
2022-02-12 13:27:14 -05:00
return <div className="DecadeName">{name}</div>;
2022-02-12 13:02:31 -05:00
}
function DayDetail({jdn}: { jdn: number }): JSX.Element {
return <div className="DayDetail">
2022-02-12 13:27:14 -05:00
<div className="DayDetail-gregorian">{jdnGregorian(jdn).toDateString()}</div>
2022-02-12 13:40:34 -05:00
<div className="DayDetail-lc">{jdnLongCount(jdn)}</div>
2022-02-12 13:02:31 -05:00
</div>;
}
2022-02-12 15:09:24 -05:00
function NormalDay({year, month, day, todayJDN}: DateProps & { todayJDN: number }): JSX.Element {
const jdn = frJDN(year, month, day);
return <div className={`Day NormalDay ${jdn === todayJDN ? 'Day-today' : ''}`}>
2022-02-12 13:02:31 -05:00
<div className="Day-name">{day}</div>
2022-02-12 13:27:14 -05:00
<div className="Day-decade">{decadeNames[(day - 1) % 10]}</div>
2022-02-12 15:09:24 -05:00
<DayDetail jdn={jdn}/>
2022-02-12 13:27:14 -05:00
</div>;
2022-02-12 13:02:31 -05:00
}
2022-02-12 15:09:24 -05:00
function NormalMonth({year, month, todayJDN}: MonthProps & { todayJDN: number }): JSX.Element {
2022-02-12 13:02:31 -05:00
const decadeHeads = decadeNames.map(name => <DecadeName name={name}/>);
2022-02-12 13:27:14 -05:00
return <div className="Month">
<div className="Month-decadeHead">{decadeHeads}</div>
<div className="Month-decades">{
Array.from(Array(3).keys()).map(i => <div className="Month-decade">{
2022-02-12 14:55:46 -05:00
Array.from(Array(10).keys()).map(j => <>
2022-02-12 15:09:24 -05:00
<NormalDay year={year} month={month} day={i * 10 + j + 1 as Day} todayJDN={todayJDN}/>
2022-02-12 14:55:46 -05:00
{j === 4 && <div className="Month-decadeSplitter"/>}
</>)
2022-02-12 13:27:14 -05:00
}</div>)
}</div>
</div>;
2022-02-12 13:02:31 -05:00
}
function ComplementaryDay({year, month, day, todayJDN}: DateProps & { todayJDN: number }): JSX.Element {
const jdn = frJDN(year, month, day);
return <div className={`Day ComplementaryDay ${jdn === todayJDN ? 'Day-today' : ''}`}>
<div className="Day-name">{dateName(month, day)}</div>
<DayDetail jdn={jdn}/>
</div>;
}
function ComplementaryDays({year, todayJDN}: {year: number, todayJDN: number}): JSX.Element {
return <div className="ComplementaryDays">{
Array.from(Array(frIsLeap(year) ? 6 : 5).keys()).map(i => <>
<ComplementaryDay year={year} month={13} day={i + 1 as Day} todayJDN={todayJDN}/>
{i % 2 === 1 && <div className="ComplementaryDays-splitter"/>}
</>)
}</div>;
}
2022-02-12 13:02:31 -05:00
export class Calendar extends React.Component<CalendarProps, CalendarState> {
2022-02-12 15:40:45 -05:00
private goToNormalized(year: number, month: number) {
if (month < 1) {
2022-02-12 15:40:45 -05:00
--year;
month += 13;
}
if (month > 13) {
++year;
month -= 13;
}
this.props.onSwitch && this.props.onSwitch(year, month as Month);
}
prevYear() {
this.goToNormalized(this.props.year - 1, this.props.month);
}
prevMonth() {
this.goToNormalized(this.props.year, this.props.month - 1);
}
nextYear() {
this.goToNormalized(this.props.year + 1, this.props.month);
}
nextMonth() {
this.goToNormalized(this.props.year, this.props.month + 1);
}
2022-02-12 13:02:31 -05:00
render(): JSX.Element {
2022-02-12 15:40:45 -05:00
return <div className="Calendar">
<div className="Calendar-head">
<div className="Calendar-prev">
<button type="button" className="btn btn-secondary" title="Previous year"
onClick={this.prevYear.bind(this)}>«
</button>
<button type="button" className="btn btn-secondary" title="Previous month"
onClick={this.prevMonth.bind(this)}>
</button>
</div>
<div className="Calendar-month-name">
{this.props.month < 13 && monthName(this.props.month)} {this.props.year}
</div>
2022-02-12 15:40:45 -05:00
<div className="Calendar-next">
<button type="button" className="btn btn-secondary" title="Next month"
onClick={this.nextMonth.bind(this)}>
</button>
<button type="button" className="btn btn-secondary" title="Next year"
onClick={this.nextYear.bind(this)}>»
</button>
</div>
</div>
{this.props.month < 13 && <NormalMonth year={this.props.year} month={this.props.month} todayJDN={this.props.todayJDN}/>}
{this.props.month === 13 && <ComplementaryDays year={this.props.year} todayJDN={this.props.todayJDN}/>}
2022-02-12 15:40:45 -05:00
</div>;
2022-02-12 13:02:31 -05:00
}
}