From f0b5954c54138942f9c7314a868bbe7c45194b6f Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 5 Sep 2025 18:10:34 +0300 Subject: [PATCH] refactor(react/collections/calendar): refactor into API --- .../src/widgets/collections/calendar/api.ts | 33 +++++++++++++++++++ .../widgets/collections/calendar/index.tsx | 31 +++-------------- 2 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 apps/client/src/widgets/collections/calendar/api.ts diff --git a/apps/client/src/widgets/collections/calendar/api.ts b/apps/client/src/widgets/collections/calendar/api.ts new file mode 100644 index 000000000..e12844fab --- /dev/null +++ b/apps/client/src/widgets/collections/calendar/api.ts @@ -0,0 +1,33 @@ +import { CreateChildrenResponse } from "@triliumnext/commons"; +import server from "../../../services/server"; +import FNote from "../../../entities/fnote"; +import { setLabel } from "../../../services/attributes"; + +interface NewEventOpts { + title: string; + startDate: string; + endDate?: string | null; + startTime?: string | null; + endTime?: string | null; +} + +export async function newEvent(parentNote: FNote, { title, startDate, endDate, startTime, endTime }: NewEventOpts) { + // Create the note. + const { note } = await server.post(`notes/${parentNote.noteId}/children?target=into`, { + title, + content: "", + type: "text" + }); + + // Set the attributes. + setLabel(note.noteId, "startDate", startDate); + if (endDate) { + setLabel(note.noteId, "endDate", endDate); + } + if (startTime) { + setLabel(note.noteId, "startTime", startTime); + } + if (endTime) { + setLabel(note.noteId, "endTime", endTime); + } +} diff --git a/apps/client/src/widgets/collections/calendar/index.tsx b/apps/client/src/widgets/collections/calendar/index.tsx index 6ed019df1..ed4450b30 100644 --- a/apps/client/src/widgets/collections/calendar/index.tsx +++ b/apps/client/src/widgets/collections/calendar/index.tsx @@ -6,13 +6,14 @@ import "./index.css"; import { useNoteLabel, useNoteLabelBoolean, useResizeObserver, useSpacedUpdate, useTriliumOption, useTriliumOptionInt } from "../../react/hooks"; import { CreateChildrenResponse, LOCALE_IDS } from "@triliumnext/commons"; import { Calendar as FullCalendar } from "@fullcalendar/core"; -import { setLabel } from "../../../services/attributes"; +import { removeOwnedAttributesByNameOrType, setLabel } from "../../../services/attributes"; import { circle } from "leaflet"; import server from "../../../services/server"; import { parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils"; import dialog from "../../../services/dialog"; import { t } from "../../../services/i18n"; import { buildEvents, buildEventsForCalendar } from "./event_builder"; +import { newEvent } from "./api"; interface CalendarViewData { @@ -67,13 +68,8 @@ export default function CalendarView({ note, noteIds }: ViewModeProps { - // Handle start and end date const { startDate, endDate } = parseStartEndDateFromEvent(e); - if (!startDate) { - return; - } - - // Handle start and end time. + if (!startDate) return; const { startTime, endTime } = parseStartEndTimeFromEvent(e); // Ask for the title @@ -82,25 +78,8 @@ export default function CalendarView({ note, noteIds }: ViewModeProps(`notes/${note.noteId}/children?target=into`, { - title, - content: "", - type: "text" - }); - - // Set the attributes. - setLabel(eventNote.noteId, "startDate", startDate); - if (endDate) { - setLabel(eventNote.noteId, "endDate", endDate); - } - if (startTime) { - setLabel(eventNote.noteId, "startTime", startTime); - } - if (endTime) { - setLabel(eventNote.noteId, "endTime", endTime); - } - }, []); + newEvent(note, { title, startDate, endDate, startTime, endTime }); + }, [ note ]); return (plugins &&