Skip to content

Build your personal knowledge base with Trilium Notes

refactor(react/collections/calendar): refactor into API

Elian Doranf0b5954c5413
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 0000000000..e12844fabd
--- /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<CreateChildrenResponse>(`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 6ed019df1a..ed4450b304 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<CalendarVi
const locale = useLocale();
const onCalendarSelection = useCallback(async (e: DateSelectArg) => {
- // 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<CalendarVi
return;
}
- // Create the note.
- const { note: eventNote } = await server.post<CreateChildrenResponse>(`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 &&
<div className="calendar-view" ref={containerRef}>