refactor(react/collections/calendar): change event in api

This commit is contained in:
Elian Doran 2025-09-06 10:43:43 +03:00
parent cfddb6f04e
commit 6237afe3cd
No known key found for this signature in database
2 changed files with 40 additions and 38 deletions

View File

@ -1,7 +1,8 @@
import { CreateChildrenResponse } from "@triliumnext/commons"; import { CreateChildrenResponse } from "@triliumnext/commons";
import server from "../../../services/server"; import server from "../../../services/server";
import FNote from "../../../entities/fnote"; import FNote from "../../../entities/fnote";
import { setLabel } from "../../../services/attributes"; import { setAttribute, setLabel } from "../../../services/attributes";
import froca from "../../../services/froca";
interface NewEventOpts { interface NewEventOpts {
title: string; title: string;
@ -11,6 +12,13 @@ interface NewEventOpts {
endTime?: string | null; endTime?: string | null;
} }
interface ChangeEventOpts {
startDate: string;
endDate?: string | null;
startTime?: string | null;
endTime?: string | null;
}
export async function newEvent(parentNote: FNote, { title, startDate, endDate, startTime, endTime }: NewEventOpts) { export async function newEvent(parentNote: FNote, { title, startDate, endDate, startTime, endTime }: NewEventOpts) {
// Create the note. // Create the note.
const { note } = await server.post<CreateChildrenResponse>(`notes/${parentNote.noteId}/children?target=into`, { const { note } = await server.post<CreateChildrenResponse>(`notes/${parentNote.noteId}/children?target=into`, {
@ -31,3 +39,27 @@ export async function newEvent(parentNote: FNote, { title, startDate, endDate, s
setLabel(note.noteId, "endTime", endTime); setLabel(note.noteId, "endTime", endTime);
} }
} }
export async function changeEvent(note: FNote, { startDate, endDate, startTime, endTime }: ChangeEventOpts) {
// Don't store the end date if it's empty.
if (endDate === startDate) {
endDate = undefined;
}
// Since they can be customized via calendar:startDate=$foo and calendar:endDate=$bar we need to determine the
// attributes to be effectively updated
let startAttribute = note.getAttributes("label").filter(attr => attr.name == "calendar:startDate").shift()?.value||"startDate";
let endAttribute = note.getAttributes("label").filter(attr => attr.name == "calendar:endDate").shift()?.value||"endDate";
const noteId = note.noteId;
setLabel(noteId, startAttribute, startDate);
setAttribute(note, "label", endAttribute, endDate);
startAttribute = note.getAttributes("label").filter(attr => attr.name == "calendar:startTime").shift()?.value||"startTime";
endAttribute = note.getAttributes("label").filter(attr => attr.name == "calendar:endTime").shift()?.value||"endTime";
if (startTime && endTime) {
setAttribute(note, "label", startAttribute, startTime);
setAttribute(note, "label", endAttribute, endTime);
}
}

View File

@ -13,7 +13,7 @@ import { parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils"
import dialog from "../../../services/dialog"; import dialog from "../../../services/dialog";
import { t } from "../../../services/i18n"; import { t } from "../../../services/i18n";
import { buildEvents, buildEventsForCalendar } from "./event_builder"; import { buildEvents, buildEventsForCalendar } from "./event_builder";
import { newEvent } from "./api"; import { changeEvent, newEvent } from "./api";
import froca from "../../../services/froca"; import froca from "../../../services/froca";
interface CalendarViewData { interface CalendarViewData {
@ -83,43 +83,13 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarVi
}, [ note ]); }, [ note ]);
const onEventChange = useCallback(async (e: EventChangeArg) => { const onEventChange = useCallback(async (e: EventChangeArg) => {
// Handle start and end date const { startDate, endDate } = parseStartEndDateFromEvent(e.event);
let { startDate, endDate } = parseStartEndDateFromEvent(e.event); if (!startDate) return;
if (!startDate) {
return;
}
const noteId = e.event.extendedProps.noteId;
// Don't store the end date if it's empty. const { startTime, endTime } = parseStartEndTimeFromEvent(e.event);
if (endDate === startDate) { const note = await froca.getNote(e.event.extendedProps.noteId);
endDate = undefined; if (!note) return;
} changeEvent(note, { startDate, endDate, startTime, endTime });
// Update start date
const note = await froca.getNote(noteId);
if (!note) {
return;
}
// Since they can be customized via calendar:startDate=$foo and calendar:endDate=$bar we need to determine the
// attributes to be effectively updated
const startAttribute = note.getAttributes("label").filter(attr => attr.name == "calendar:startDate").shift()?.value||"startDate";
const endAttribute = note.getAttributes("label").filter(attr => attr.name == "calendar:endDate").shift()?.value||"endDate";
setLabel(noteId, startAttribute, startDate);
setLabel(noteId, endAttribute, endDate);
// Update start time and end time if needed.
if (!e.event.allDay) {
const startAttribute = note.getAttributes("label").filter(attr => attr.name == "calendar:startTime").shift()?.value||"startTime";
const endAttribute = note.getAttributes("label").filter(attr => attr.name == "calendar:endTime").shift()?.value||"endTime";
const { startTime, endTime } = parseStartEndTimeFromEvent(e.event);
if (startTime && endTime) {
setLabel(noteId, startAttribute, startTime);
setLabel(noteId, endAttribute, endTime);
}
}
}, []); }, []);
return (plugins && return (plugins &&