diff options
Diffstat (limited to 'src/client/views/nodes/TaskBox.tsx')
-rw-r--r-- | src/client/views/nodes/TaskBox.tsx | 109 |
1 files changed, 72 insertions, 37 deletions
diff --git a/src/client/views/nodes/TaskBox.tsx b/src/client/views/nodes/TaskBox.tsx index e755c6fe3..b59599052 100644 --- a/src/client/views/nodes/TaskBox.tsx +++ b/src/client/views/nodes/TaskBox.tsx @@ -211,9 +211,77 @@ export class TaskBox extends React.Component<TaskBoxProps> { const isCompleted = !!this.props.Document.$task_completed; const startTime = doc.$task_startTime instanceof DateField && doc.$task_startTime.date instanceof Date ? toLocalDateTimeString(doc.$task_startTime.date) : ''; - const endTime = doc.$task_endTime instanceof DateField && doc.$task_endTime.date instanceof Date ? toLocalDateTimeString(doc.$task_endTime.date) : ''; + const handleGoogleTaskClick = async () => { + console.log('GT button clicked'); + + try { + const token = await GoogleAuthenticationManager.Instance.fetchOrGenerateAccessToken(); + if (!token) { + const listener = () => { + window.removeEventListener('focusin', listener); + if (confirm('✅ Authorization complete. Try adding the task again?')) { + // you could refactor the click handler here + handleGoogleTaskClick(); + } + window.removeEventListener('focusin', listener); + }; + setTimeout(() => window.addEventListener('focusin', listener), 100); + return; + } + + let due: string | undefined; + + if (allDay) { + const rawRange = typeof doc.$task_dateRange === 'string' ? doc.$task_dateRange : ''; + const datePart = rawRange.split('|')[0]; + + if (datePart && !isNaN(new Date(datePart).getTime())) { + // Set time to midnight UTC to represent the start of the all-day event + const baseDate = datePart.includes('T') ? datePart : datePart + 'T00:00:00Z'; + due = new Date(baseDate).toISOString(); + } else { + due = undefined; + } + } else if (doc.$task_endTime instanceof DateField && doc.$task_endTime.date) { + due = doc.$task_endTime.date.toISOString(); + } else if (doc.$task_startTime instanceof DateField && doc.$task_startTime.date) { + due = doc.$task_startTime.date.toISOString(); + } else { + due = undefined; + } + + const response = await fetch('/googleTasks/create', { + method: 'POST', + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ + title: taskTitle || 'Untitled Task', + notes: taskDesc, + due, + status: doc.$task_completed ? 'completed' : 'needsAction', + completed: doc.$task_completed ? new Date().toISOString() : undefined, + }), + }); + + const result = await response.json(); + console.log('Google Task result:', result); + + if (result?.id) { + alert('✅ Task sent to Google Tasks!'); + } else { + alert(`❌ Failed: ${result?.error?.message || 'Unknown error'}`); + } + } catch (err) { + console.error('Fetch error:', err); + alert('❌ Task creation failed.'); + } + }; + return ( <div className="task-manager-container"> <input className="task-manager-title" type="text" placeholder="Task Title" value={taskTitle} onChange={this.updateTitle} disabled={isCompleted} style={{ opacity: isCompleted ? 0.7 : 1 }} /> @@ -257,44 +325,11 @@ export class TaskBox extends React.Component<TaskBoxProps> { <button className="task-manager-google" - onClick={async event => { + onClick={event => { event.preventDefault(); - - console.log('GT button clicked'); - try { - const token = await GoogleAuthenticationManager.Instance.fetchOrGenerateAccessToken(); - console.log('Got token', token); - - const response = await fetch('/googleTasks/create', { - method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${token}`, - }, - body: JSON.stringify({ - title: taskTitle || 'Untitled Task', - notes: taskDesc, - due: allDay ? new Date(String(doc.$task_dateRange)?.split('|')[0]).toISOString() : (doc.$task_endTime instanceof DateField && doc.$task_endTime.date?.toISOString()) || undefined, - status: doc.$task_completed ? 'completed' : 'needsAction', - completed: doc.$task_completed ? new Date().toISOString() : undefined, - }), - }); - - const result = await response.json(); - console.log('Google Task result:', result); - - if (result?.id) { - alert('✅ Task sent to Google Tasks!'); - } else { - alert(`❌ Failed: ${result?.error?.message || 'Unknown error'}`); - } - } catch (err) { - console.error('Fetch error:', err); - alert('❌ Task creation failed.'); - } + handleGoogleTaskClick(); }}> - Add to Google + Add to Google Tasks </button> </div> |