fix bugs with calendar

This commit is contained in:
marlon-erler
2024-07-26 16:49:13 +02:00
parent ddbdae9d14
commit 51bc40d8b1
6 changed files with 124 additions and 65 deletions
+39 -12
View File
@@ -644,6 +644,11 @@
const referencePath = [...monthPath, taskFileContent.fileId]; const referencePath = [...monthPath, taskFileContent.fileId];
this.storageModel.write(referencePath, ""); this.storageModel.write(referencePath, "");
}; };
deleteTaskReference = (monthString, taskId) => {
const monthPath = this.getMonthPath(monthString);
const referencePath = [...monthPath, taskId];
this.storageModel.write(referencePath, "");
};
listTaskIds = (monthString) => { listTaskIds = (monthString) => {
const monthPath = this.getMonthPath(monthString); const monthPath = this.getMonthPath(monthString);
return this.storageModel.list(monthPath); return this.storageModel.list(monthPath);
@@ -667,7 +672,10 @@
days: {} days: {}
}; };
for (let i = 0; i < daysInMonth; i++) { for (let i = 0; i < daysInMonth; i++) {
grid.days[i + 1] = defaultValueCreator(); const paddedDate = _CalendarModel.padDateOrMonth(
(i + 1).toString()
);
grid.days[paddedDate] = defaultValueCreator();
} }
return grid; return grid;
}; };
@@ -684,18 +692,22 @@
}; };
static isoToDateString = (dateISOString) => { static isoToDateString = (dateISOString) => {
const [year, month, date, _] = dateISOString.split("-"); const [year, month, date, _] = dateISOString.split("-");
return date; const paddedDate = _CalendarModel.padDateOrMonth(date ?? "");
return paddedDate;
}; };
static getMonthString = (year = "", month = "") => { static getMonthString = (year = "", month = "") => {
const paddedYear = year.padStart(4, "0"); const paddedYear = year.padStart(4, "0");
const paddedMonth = month.padStart(2, "0"); const paddedMonth = _CalendarModel.padDateOrMonth(month);
return `${paddedYear}-${paddedMonth}`; return `${paddedYear}-${paddedMonth}`;
}; };
static getISODateString = (year, month, date) => { static getISODateString = (year, month, date) => {
const monthString = _CalendarModel.getMonthString(year, month); const monthString = _CalendarModel.getMonthString(year, month);
const paddedDate = date.padStart(2, "0"); const paddedDate = _CalendarModel.padDateOrMonth(date);
return `${monthString}-${paddedDate}`; return `${monthString}-${paddedDate}`;
}; };
static padDateOrMonth = (input) => {
return input.padStart(2, "0");
};
}; };
// src/colors.ts // src/colors.ts
@@ -1684,20 +1696,33 @@
this.selectTask(taskViewModel); this.selectTask(taskViewModel);
this.updateTaskIndices(); this.updateTaskIndices();
}; };
getEventsForDate = () => {
const paddedDate = CalendarModel.padDateOrMonth(
this.selectedDate.toString()
);
if (this.monthGrid.value == void 0) {
return void 0;
}
return this.monthGrid.value.days[paddedDate];
};
// view // view
getTaskMapState = (taskFileContent) => { getTaskMapState = (taskFileContent) => {
if (this.monthGrid.value == null) return null; if (this.monthGrid.value == null) return null;
const dateString = CalendarModel.isoToDateString( const date = CalendarModel.isoToDateString(
taskFileContent.date ?? "" taskFileContent.date ?? ""
); );
if (dateString == void 0) return null; return this.monthGrid.value.days[date];
return this.monthGrid.value.days[dateString];
}; };
showTask = (taskFileContent) => { showTask = (taskFileContent) => {
const monthString = CalendarModel.isoToMonthString( const monthString = CalendarModel.isoToMonthString(
taskFileContent.date ?? "" taskFileContent.date ?? ""
); );
if (monthString == void 0 || monthString != this.monthString) { if (monthString == void 0 || monthString != this.monthString) {
this.removeTaskFromView(taskFileContent);
this.calendarModel.deleteTaskReference(
this.monthString,
taskFileContent.fileId
);
return; return;
} }
const taskViewModel = new TaskViewModel( const taskViewModel = new TaskViewModel(
@@ -1707,12 +1732,14 @@
taskFileContent taskFileContent
); );
const mapState = this.getTaskMapState(taskFileContent); const mapState = this.getTaskMapState(taskFileContent);
mapState?.set(taskFileContent.fileId, taskViewModel); this.taskViewModels.handleRemoval(taskViewModel, () => {
mapState?.remove(taskFileContent.fileId);
});
this.taskViewModels.remove(taskFileContent.fileId);
this.taskViewModels.set(taskFileContent.fileId, taskViewModel); this.taskViewModels.set(taskFileContent.fileId, taskViewModel);
mapState?.set(taskFileContent.fileId, taskViewModel);
}; };
removeTaskFromView = (taskFileContent) => { removeTaskFromView = (taskFileContent) => {
const mapState = this.getTaskMapState(taskFileContent);
mapState?.remove(taskFileContent.fileId);
this.taskViewModels.remove(taskFileContent.fileId); this.taskViewModels.remove(taskFileContent.fileId);
}; };
showToday = () => { showToday = () => {
@@ -2594,7 +2621,7 @@
}); });
return view; return view;
}; };
return /* @__PURE__ */ createElement("div", { class: "month-grid-wrapper" }, /* @__PURE__ */ createElement("div", { class: "day-labels" }, ...dayLabels), /* @__PURE__ */ createElement("div", { class: "month-grid" }, ...offsetElements, ...Object.entries(monthGrid.days).map((entry) => { return /* @__PURE__ */ createElement("div", { class: "month-grid-wrapper" }, /* @__PURE__ */ createElement("div", { class: "day-labels" }, ...dayLabels), /* @__PURE__ */ createElement("div", { class: "month-grid" }, ...offsetElements, ...Object.entries(monthGrid.days).sort((a, b) => localeCompare(a[0], b[0])).map((entry) => {
const [date, mapState] = entry; const [date, mapState] = entry;
const isSelected = createProxyState( const isSelected = createProxyState(
[selectedDate], [selectedDate],
@@ -2770,7 +2797,7 @@
calendarPageViewModel.selectedDate calendarPageViewModel.selectedDate
], ],
() => { () => {
const listState = calendarPageViewModel.monthGrid.value?.days[calendarPageViewModel.selectedDate.toString()]; const listState = calendarPageViewModel.getEventsForDate();
if (listState == void 0) { if (listState == void 0) {
return /* @__PURE__ */ createElement("div", null); return /* @__PURE__ */ createElement("div", null);
} else if (listState.value.size == 0) { } else if (listState.value.size == 0) {
+20 -5
View File
@@ -42,6 +42,13 @@ export default class CalendarModel {
this.storageModel.write(referencePath, ""); this.storageModel.write(referencePath, "");
}; };
deleteTaskReference = (monthString: string, taskId: string): void => {
const monthPath: string[] = this.getMonthPath(monthString);
const referencePath: string[] = [...monthPath, taskId];
this.storageModel.write(referencePath, "");
};
listTaskIds = (monthString: string): string[] => { listTaskIds = (monthString: string): string[] => {
const monthPath: string[] = this.getMonthPath(monthString); const monthPath: string[] = this.getMonthPath(monthString);
return this.storageModel.list(monthPath); return this.storageModel.list(monthPath);
@@ -81,7 +88,10 @@ export default class CalendarModel {
}; };
for (let i = 0; i < daysInMonth; i++) { for (let i = 0; i < daysInMonth; i++) {
grid.days[i + 1] = defaultValueCreator(); const paddedDate: string = CalendarModel.padDateOrMonth(
(i + 1).toString()
);
grid.days[paddedDate] = defaultValueCreator();
} }
return grid; return grid;
@@ -104,14 +114,15 @@ export default class CalendarModel {
return CalendarModel.getMonthString(year, month); return CalendarModel.getMonthString(year, month);
}; };
static isoToDateString = (dateISOString: string): string | undefined => { static isoToDateString = (dateISOString: string): string => {
const [year, month, date, _] = dateISOString.split("-"); const [year, month, date, _] = dateISOString.split("-");
return date; const paddedDate = CalendarModel.padDateOrMonth(date ?? "");
return paddedDate;
}; };
static getMonthString = (year: string = "", month: string = ""): string => { static getMonthString = (year: string = "", month: string = ""): string => {
const paddedYear: string = year.padStart(4, "0"); const paddedYear: string = year.padStart(4, "0");
const paddedMonth: string = month.padStart(2, "0"); const paddedMonth: string = CalendarModel.padDateOrMonth(month);
return `${paddedYear}-${paddedMonth}`; return `${paddedYear}-${paddedMonth}`;
}; };
@@ -121,9 +132,13 @@ export default class CalendarModel {
date: string date: string
): string => { ): string => {
const monthString: string = CalendarModel.getMonthString(year, month); const monthString: string = CalendarModel.getMonthString(year, month);
const paddedDate: string = date.padStart(2, "0"); const paddedDate: string = CalendarModel.padDateOrMonth(date);
return `${monthString}-${paddedDate}`; return `${monthString}-${paddedDate}`;
}; };
static padDateOrMonth = (input: string): string => {
return input.padStart(2, "0");
};
} }
export enum CalendarModelSubPaths { export enum CalendarModelSubPaths {
+1 -4
View File
@@ -40,10 +40,7 @@ export function CalendarPage(calendarPageViewModel: CalendarPageViewModel) {
calendarPageViewModel.selectedDate, calendarPageViewModel.selectedDate,
], ],
() => { () => {
const listState = const listState = calendarPageViewModel.getEventsForDate();
calendarPageViewModel.monthGrid.value?.days[
calendarPageViewModel.selectedDate.toString()
];
if (listState == undefined) { if (listState == undefined) {
return <div></div>; return <div></div>;
+4 -1
View File
@@ -5,6 +5,7 @@ import { MonthGrid } from "../../Model/Files/calendarModel";
import TaskViewModel from "../../ViewModel/Pages/taskViewModel"; import TaskViewModel from "../../ViewModel/Pages/taskViewModel";
import { translations } from "../translations"; import { translations } from "../translations";
import { allowDrop } from "../utility"; import { allowDrop } from "../utility";
import { localeCompare } from "../../Model/Utility/utility";
export function MonthGrid<T>( export function MonthGrid<T>(
monthGrid: MonthGrid<React.MapState<T>>, monthGrid: MonthGrid<React.MapState<T>>,
@@ -47,7 +48,9 @@ export function MonthGrid<T>(
<div class="day-labels">{...dayLabels}</div> <div class="day-labels">{...dayLabels}</div>
<div class="month-grid"> <div class="month-grid">
{...offsetElements} {...offsetElements}
{...Object.entries(monthGrid.days).map((entry) => { {...Object.entries(monthGrid.days)
.sort((a, b) => localeCompare(a[0], b[0]))
.map((entry) => {
const [date, mapState] = entry; const [date, mapState] = entry;
const isSelected = React.createProxyState( const isSelected = React.createProxyState(
+26 -9
View File
@@ -60,17 +60,28 @@ export default class CalendarPageViewModel extends TaskContainingPageViewModel {
this.updateTaskIndices(); this.updateTaskIndices();
}; };
getEventsForDate = (): React.MapState<TaskViewModel> | undefined => {
const paddedDate: string = CalendarModel.padDateOrMonth(
this.selectedDate.toString()
);
if (this.monthGrid.value == undefined) {
return undefined;
}
return this.monthGrid.value.days[paddedDate];
};
// view // view
getTaskMapState = ( getTaskMapState = (
taskFileContent: TaskFileContent taskFileContent: TaskFileContent
): React.MapState<TaskViewModel> | null => { ): React.MapState<TaskViewModel> | null => {
if (this.monthGrid.value == null) return null; if (this.monthGrid.value == null) return null;
const dateString: string | undefined = CalendarModel.isoToDateString( const date: string = CalendarModel.isoToDateString(
taskFileContent.date ?? "" taskFileContent.date ?? ""
); );
if (dateString == undefined) return null; return this.monthGrid.value.days[date];
return this.monthGrid.value.days[dateString];
}; };
showTask = (taskFileContent: TaskFileContent): void => { showTask = (taskFileContent: TaskFileContent): void => {
@@ -78,7 +89,11 @@ export default class CalendarPageViewModel extends TaskContainingPageViewModel {
taskFileContent.date ?? "" taskFileContent.date ?? ""
); );
if (monthString == undefined || monthString != this.monthString) { if (monthString == undefined || monthString != this.monthString) {
// TODO remove this.removeTaskFromView(taskFileContent);
this.calendarModel.deleteTaskReference(
this.monthString,
taskFileContent.fileId
);
return; return;
} }
@@ -90,16 +105,18 @@ export default class CalendarPageViewModel extends TaskContainingPageViewModel {
); );
const mapState: React.MapState<TaskViewModel> | null = const mapState: React.MapState<TaskViewModel> | null =
this.getTaskMapState(taskFileContent); this.getTaskMapState(taskFileContent);
mapState?.set(taskFileContent.fileId, taskViewModel);
this.taskViewModels.handleRemoval(taskViewModel, () => {
mapState?.remove(taskFileContent.fileId);
});
this.taskViewModels.remove(taskFileContent.fileId);
this.taskViewModels.set(taskFileContent.fileId, taskViewModel); this.taskViewModels.set(taskFileContent.fileId, taskViewModel);
mapState?.set(taskFileContent.fileId, taskViewModel);
}; };
removeTaskFromView = (taskFileContent: TaskFileContent): void => { removeTaskFromView = (taskFileContent: TaskFileContent): void => {
const mapState: React.MapState<TaskViewModel> | null =
this.getTaskMapState(taskFileContent);
mapState?.remove(taskFileContent.fileId);
this.taskViewModels.remove(taskFileContent.fileId); this.taskViewModels.remove(taskFileContent.fileId);
}; };
+1 -1
View File
@@ -78,7 +78,7 @@ export default class TaskViewModel {
setDate = (dateISOString: string): void => { setDate = (dateISOString: string): void => {
this.date.value = dateISOString; this.date.value = dateISOString;
this.save(); this.save();
} };
// view // view
open = (): void => { open = (): void => {