@@ -163,43 +163,80 @@ export class VAPIClient {
163
163
}
164
164
165
165
/**
166
- * Add task to Google Calendar
166
+ * Add task to Google Calendar (using Google Calendar URL)
167
167
*/
168
168
async addToGoogleCalendar ( task : Task ) : Promise < { success : boolean ; eventId ?: string } > {
169
169
try {
170
- const response = await fetch ( `${ this . baseURL } /api/calendar/google/add` , {
171
- method : "POST" ,
172
- headers : this . getHeaders ( ) ,
173
- body : JSON . stringify ( { task } ) ,
170
+ // Import calendar utilities (dynamic import for client-side only)
171
+ const { openGoogleCalendar } = await import ( "./calendar-utils" ) ;
172
+
173
+ console . log ( "📅 Adding task to Google Calendar:" , task . title ) ;
174
+
175
+ // Open Google Calendar with pre-filled event
176
+ const result = await openGoogleCalendar ( {
177
+ id : task . id ,
178
+ title : task . title ,
179
+ description : task . description ,
180
+ dueDate : task . dueDate ,
181
+ type : task . type ,
174
182
} ) ;
175
183
176
- if ( ! response . ok ) {
177
- throw new Error ( `Failed to add to Google Calendar: ${ response . statusText } ` ) ;
184
+ if ( result . success ) {
185
+ return {
186
+ success : true ,
187
+ eventId : task . id , // Return task ID as event identifier
188
+ } ;
189
+ } else {
190
+ // Even if popup was blocked, we can still consider it a success
191
+ // since the URL is available for manual opening
192
+ if ( result . url ) {
193
+ console . log ( "📋 Google Calendar URL (popup blocked):" , result . url ) ;
194
+ return {
195
+ success : true ,
196
+ eventId : task . id ,
197
+ } ;
198
+ }
199
+ throw new Error ( "Failed to create Google Calendar event" ) ;
178
200
}
179
-
180
- return await response . json ( ) ;
181
201
} catch ( error ) {
182
202
console . error ( "Error adding to Google Calendar:" , error ) ;
183
203
throw error ;
184
204
}
185
205
}
186
206
187
207
/**
188
- * Add task to iOS calendar (using Web API)
208
+ * Add task to iOS calendar (using .ics file download)
209
+ * Based on: https://qiita.com/bananbo/items/281f2c98419355d7324c
189
210
*/
190
211
async addToIosCalendar ( task : Task ) : Promise < { success : boolean ; eventId ?: string } > {
191
212
try {
192
- const response = await fetch ( `${ this . baseURL } /api/calendar/ios/add` , {
193
- method : "POST" ,
194
- headers : this . getHeaders ( ) ,
195
- body : JSON . stringify ( { task } ) ,
196
- } ) ;
213
+ // Import calendar utilities (dynamic import for client-side only)
214
+ const { createAndDownloadIcsFile, isIcsDownloadSupported } = await import ( "./calendar-utils" ) ;
197
215
198
- if ( ! response . ok ) {
199
- throw new Error ( `Failed to add to iOS Calendar: ${ response . statusText } ` ) ;
216
+ // Check if .ics download is supported
217
+ if ( ! isIcsDownloadSupported ( ) ) {
218
+ throw new Error ( "Calendar file download is not supported in this environment" ) ;
200
219
}
201
220
202
- return await response . json ( ) ;
221
+ console . log ( "📱 Adding task to iOS Calendar via .ics download:" , task . title ) ;
222
+
223
+ // Create and download .ics file
224
+ const result = await createAndDownloadIcsFile ( {
225
+ id : task . id ,
226
+ title : task . title ,
227
+ description : task . description ,
228
+ dueDate : task . dueDate ,
229
+ type : task . type ,
230
+ } ) ;
231
+
232
+ if ( result . success ) {
233
+ return {
234
+ success : true ,
235
+ eventId : result . filename , // Return filename as event identifier
236
+ } ;
237
+ } else {
238
+ throw new Error ( "Failed to create calendar file" ) ;
239
+ }
203
240
} catch ( error ) {
204
241
console . error ( "Error adding to iOS Calendar:" , error ) ;
205
242
throw error ;
0 commit comments