Skip to content

Commit d47b116

Browse files
authored
Merge pull request #3 from CCU-Class/refactor_function
switch function to utils.js
2 parents 9947b20 + 4e3b898 commit d47b116

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

src/js/popup.js

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentYearMonth, downloadICS } from "./utils.js";
1+
import { getCurrentYearMonth, downloadICS, addOneEventToCalendar } from "./utils.js";
22

33
function initializeInputs() {
44
const { year, month } = getCurrentYearMonth();
@@ -116,7 +116,7 @@ async function insertEventsToGCal(events) {
116116
// add event Google Calendar
117117
for (const event of events) {
118118
try {
119-
await addEventToCalendar(token, event);
119+
await addOneEventToCalendar(token, event);
120120
console.log("add Event Success", event.title);
121121
} catch (err) {
122122
console.error("add Event error:", event.title, err);
@@ -208,38 +208,6 @@ function getGoogleAuthToken() {
208208
});
209209
}
210210

211-
// Google Calendar API add Event
212-
async function addEventToCalendar(token, event) {
213-
// Google Calendar API call
214-
// Docs: https://developers.google.com/calendar/api/v3/reference/events/insert
215-
const res = await fetch("https://www.googleapis.com/calendar/v3/calendars/primary/events", {
216-
method: "POST",
217-
headers: {
218-
Authorization: `Bearer ${token}`,
219-
"Content-Type": "application/json",
220-
},
221-
body: JSON.stringify({
222-
summary: event.title,
223-
description: event.description,
224-
start: {
225-
dateTime: event.startDate.toISOString(),
226-
timeZone: "Asia/Taipei",
227-
},
228-
end: {
229-
dateTime: event.endDate.toISOString(),
230-
timeZone: "Asia/Taipei",
231-
},
232-
}),
233-
});
234-
235-
if (!res.ok) {
236-
const errText = await res.text();
237-
throw new Error(`API Error: ${res.status}, ${errText}`);
238-
}
239-
240-
return res.json();
241-
}
242-
243211
// Initialize when DOM is fully loaded
244212
document.addEventListener("DOMContentLoaded", () => {
245213
initializeInputs();

src/js/utils.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,35 @@ END:VEVENT
4141
a.click();
4242
URL.revokeObjectURL(url);
4343
}
44+
45+
// Google Calendar API add Event
46+
export async function addOneEventToCalendar(token, event) {
47+
// Google Calendar API call
48+
// Docs: https://developers.google.com/calendar/api/v3/reference/events/insert
49+
const res = await fetch("https://www.googleapis.com/calendar/v3/calendars/primary/events", {
50+
method: "POST",
51+
headers: {
52+
Authorization: `Bearer ${token}`,
53+
"Content-Type": "application/json",
54+
},
55+
body: JSON.stringify({
56+
summary: event.title,
57+
description: event.description,
58+
start: {
59+
dateTime: event.startDate.toISOString(),
60+
timeZone: "Asia/Taipei",
61+
},
62+
end: {
63+
dateTime: event.endDate.toISOString(),
64+
timeZone: "Asia/Taipei",
65+
},
66+
}),
67+
});
68+
69+
if (!res.ok) {
70+
const errText = await res.text();
71+
throw new Error(`API Error: ${res.status}, ${errText}`);
72+
}
73+
74+
return res.json();
75+
}

0 commit comments

Comments
 (0)