mirror of
https://github.com/zadam/trilium.git
synced 2025-11-03 13:09:01 +01:00
refactor(react/collections/calendar): change event in api
This commit is contained in:
parent
cfddb6f04e
commit
6237afe3cd
@ -1,7 +1,8 @@
|
||||
import { CreateChildrenResponse } from "@triliumnext/commons";
|
||||
import server from "../../../services/server";
|
||||
import FNote from "../../../entities/fnote";
|
||||
import { setLabel } from "../../../services/attributes";
|
||||
import { setAttribute, setLabel } from "../../../services/attributes";
|
||||
import froca from "../../../services/froca";
|
||||
|
||||
interface NewEventOpts {
|
||||
title: string;
|
||||
@ -11,6 +12,13 @@ interface NewEventOpts {
|
||||
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) {
|
||||
// Create the note.
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -13,7 +13,7 @@ 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";
|
||||
import { changeEvent, newEvent } from "./api";
|
||||
import froca from "../../../services/froca";
|
||||
|
||||
interface CalendarViewData {
|
||||
@ -83,43 +83,13 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarVi
|
||||
}, [ note ]);
|
||||
|
||||
const onEventChange = useCallback(async (e: EventChangeArg) => {
|
||||
// Handle start and end date
|
||||
let { startDate, endDate } = parseStartEndDateFromEvent(e.event);
|
||||
if (!startDate) {
|
||||
return;
|
||||
}
|
||||
const noteId = e.event.extendedProps.noteId;
|
||||
const { startDate, endDate } = parseStartEndDateFromEvent(e.event);
|
||||
if (!startDate) return;
|
||||
|
||||
// Don't store the end date if it's empty.
|
||||
if (endDate === startDate) {
|
||||
endDate = undefined;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
const { startTime, endTime } = parseStartEndTimeFromEvent(e.event);
|
||||
const note = await froca.getNote(e.event.extendedProps.noteId);
|
||||
if (!note) return;
|
||||
changeEvent(note, { startDate, endDate, startTime, endTime });
|
||||
}, []);
|
||||
|
||||
return (plugins &&
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user