Files
CT/src/Views/Objects/calendarView.tsx
T

117 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-07-18 14:54:11 +02:00
import * as React from "bloatless-react";
import { Chat, MessageObject } from "../../Model/chatModel";
2024-07-18 17:42:41 +02:00
import { DayView } from "./dayView";
2024-07-18 22:35:25 +02:00
import { MiniatureDayView } from "./miniatureDayView";
2024-07-18 17:42:41 +02:00
import { dayInCalendar } from "../../Model/model";
2024-07-18 23:25:37 +02:00
import { translation } from "../../translations";
2024-07-18 14:54:11 +02:00
export function CalendarView(
chat: Chat,
messageObjects: React.MapState<MessageObject>,
selectedObject: React.State<MessageObject | undefined>,
isShowingObjectModal: React.State<boolean>
) {
2024-07-18 23:25:37 +02:00
const selectedDate = new Date(dayInCalendar.value);
2024-07-18 23:36:47 +02:00
const selectedMonth = new React.State(selectedDate.getMonth() + 1);
2024-07-18 23:25:37 +02:00
const selectedYear = new React.State(selectedDate.getFullYear());
2024-07-18 23:36:47 +02:00
React.bulkSubscribe([selectedMonth, selectedYear], () => updateSelectedDate());
updateSelectedDate();
2024-07-18 23:25:37 +02:00
2024-07-18 23:36:47 +02:00
function updateSelectedDate() {
2024-07-18 23:25:37 +02:00
selectedDate.setMonth(selectedMonth.value - 1);
selectedDate.setFullYear(selectedYear.value);
dayInCalendar.value = selectedDate.toISOString().split("T")[0];
2024-07-18 23:36:47 +02:00
}
2024-07-18 23:25:37 +02:00
function previousMonth() {
if (selectedMonth.value <= 1) {
selectedMonth.value = 12;
selectedYear.value -= 1;
return;
}
selectedMonth.value -= 1;
}
function nextMonth() {
if (selectedMonth.value >= 12) {
selectedMonth.value = 1;
selectedYear.value += 1;
return;
}
selectedMonth.value += 1;
}
2024-07-18 22:35:25 +02:00
2024-07-18 23:25:37 +02:00
let monthGridCells = new React.State<HTMLElement[]>([]);
2024-07-18 22:35:25 +02:00
dayInCalendar.subscribe((newValue) => {
2024-07-18 23:25:37 +02:00
const newSelectedDate = new Date(newValue);
2024-07-18 22:35:25 +02:00
const currentDate = new Date();
monthGridCells.value = [];
// initialize to first day of month
currentDate.setDate(1);
2024-07-18 23:25:37 +02:00
currentDate.setMonth(newSelectedDate.getMonth());
// labels
for (let i = 0; i < 7; i++) {
monthGridCells.value.push(
<b class="secondary ellipsis width-100">{translation.weekdays[i]}</b>
);
}
2024-07-18 22:35:25 +02:00
// create blank dates starting sunday
const monthOffset = currentDate.getDay();
for (let i = 0; i < monthOffset; i++) {
2024-07-18 23:25:37 +02:00
monthGridCells.value.push(<div></div>);
2024-07-18 22:35:25 +02:00
}
// add day views
2024-07-18 23:25:37 +02:00
while (currentDate.getMonth() == newSelectedDate.getMonth()) {
2024-07-18 22:35:25 +02:00
monthGridCells.value.push(
2024-07-18 23:25:37 +02:00
MiniatureDayView(chat, messageObjects, new Date(currentDate))
2024-07-18 22:35:25 +02:00
);
currentDate.setDate(currentDate.getDate() + 1);
}
2024-07-18 23:25:37 +02:00
monthGridCells.callSubscriptions();
2024-07-18 22:35:25 +02:00
});
return (
<div class="calendar-wrapper">
<div class="month-grid padding scroll-v">
2024-07-18 23:25:37 +02:00
<div class="flex-row justify-center gap">
<button
on:click={previousMonth}
aria-label={translation.previousMonth}
>
<span class="icon">arrow_back</span>
</button>
<input
style="width: 90px"
type="number"
bind:value={selectedMonth}
></input>
<input
style="width: 110px"
type="number"
bind:value={selectedYear}
></input>
<button on:click={nextMonth} aria-label={translation.nextMonth}>
<span class="icon">arrow_forward</span>
</button>
</div>
<hr></hr>
2024-07-18 22:35:25 +02:00
<div
class="grid gap"
style="grid-template-columns: repeat(7, 1fr)"
children:set={monthGridCells}
></div>
2024-07-18 17:42:41 +02:00
</div>
{DayView(chat, messageObjects, selectedObject, isShowingObjectModal)}
</div>
2024-07-18 22:35:25 +02:00
);
2024-07-18 14:54:11 +02:00
}