Skip to content

Commit

Permalink
Added calendar switch to EventDetails #184
Browse files Browse the repository at this point in the history
  • Loading branch information
YukiGasai committed Oct 3, 2023
1 parent 8929100 commit 199686b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "google-calendar",
"name": "Google Calendar",
"version": "1.10.10",
"version": "1.10.11",
"minAppVersion": "0.12.0",
"description": "Interact with your Google Calendar from Inside Obsidian",
"author": "YukiGasai",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "google-calendar",
"version": "1.10.10",
"version": "1.10.11",
"description": "Interact with your Google Calendar from Inside Obsidian",
"main": "main.js",
"scripts": {
Expand Down
73 changes: 73 additions & 0 deletions src/googleApi/GoogleSwitchCalendar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { GoogleEvent } from "../helper/types";
import { settingsAreCompleteAndLoggedIn } from "../view/GoogleCalendarSettingTab";
import { createNotice } from "../helper/NoticeHelper";
import { callRequest } from "src/helper/RequestWrapper";
import { GoogleApiError } from "./GoogleApiError";
import GoogleCalendarPlugin from "../GoogleCalendarPlugin";
import { googleListCalendars } from "../googleApi/GoogleListCalendars";

/**
* This function will switch the calendar of an event at the google api
* @param _event
* @returns
*/
export async function googleSwitchCalendar(
_event: GoogleEvent,
newCalendarId: string,
): Promise<GoogleEvent> {

let event = structuredClone(_event)

const plugin = GoogleCalendarPlugin.getInstance();

if (!settingsAreCompleteAndLoggedIn()){
throw new GoogleApiError("Not logged in", null, 401, {error: "Not logged in"})
}

let calenderId = ""

if(event?.parent?.id){
calenderId = event.parent.id;
}else{
calenderId = plugin.settings.defaultCalendar;
}

if(calenderId === ""){
throw new GoogleApiError("Could not switch Calendar for Event because no default calendar selected in Settings", null, 999, {error: "No calendar set"})
}

let updatedEvent = await callRequest(`https://www.googleapis.com/calendar/v3/calendars/${calenderId}/events/${event.recurringEventId ?? event.id}/move?destination=${newCalendarId}`, "POST", event)
let calendars = await googleListCalendars()
_event.parent = calendars.find(calendar => calendar.id === newCalendarId);

return _event;
}


export async function switchCalendar(
event: GoogleEvent,
newCalendarId: string,
): Promise<GoogleEvent> {
try{
const updatedEvent = await googleSwitchCalendar(
event,
newCalendarId
);
createNotice(`Google Event ${updatedEvent.summary} switched calendar.`);
return updatedEvent;
}catch(error){
switch (error.status) {
case 401: break;
case 999:
createNotice(error.message)
break;
default:
createNotice(`Google Event ${event.summary} could not switch calendar.`);
console.error('[GoogleCalendar]', error);
break;
}
return null;
}
}


13 changes: 11 additions & 2 deletions src/svelte/views/EventDetails.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import { getEvent } from "src/googleApi/GoogleGetEvent";
import { createNoteFromEvent } from "src/helper/AutoEventNoteCreator";
import EventDescriptionInput from "../components/EventDescriptionInput.svelte";
import { switchCalendar } from "../../googleApi/GoogleSwitchCalendar";
import { googleClearCachedEvents } from "../../googleApi/GoogleListEvents";
export let event: GoogleEvent;
export let closeFunction :() => void;
Expand Down Expand Up @@ -145,14 +147,21 @@
}
});
const changeCalendar = (e:Event) => {
const changeCalendar = async (e:Event) => {
const selectElement = e.target;
if(selectElement instanceof HTMLSelectElement){
const value = selectElement.value;
selectedCalendarId = value;
}
if(event.id){
await switchCalendar(event, selectedCalendarId);
event.parent = calendars.find(calendar => calendar.id === selectedCalendarId);
googleClearCachedEvents()
}
}
const handleCreateEvent = async () => {
Expand Down Expand Up @@ -267,7 +276,7 @@ $: {

<label for="calendar">Calendar</label>

<select name="calendar" class="dropdown" on:change="{changeCalendar}" disabled={event?.id !== undefined}>
<select name="calendar" class="dropdown" on:change="{changeCalendar}">

{#each calendars as calendar}
<option id="{calendar.id}" value="{calendar.id}" selected="{calendar.id === selectedCalendarId}">{calendar.summary}</option>
Expand Down

0 comments on commit 199686b

Please sign in to comment.