import React, { useState, useEffect } from "react"; import { Card, CardHeader, CardTitle, CardDescription, CardContent, } from "../components/ui/card"; import { Button } from "../components/ui/button"; import { Badge } from "../components/ui/badge"; import { Switch } from "../components/ui/switch"; import { useToast } from "../components/ui/toast"; import { Calendar, CheckCircle, XCircle, RefreshCw, Settings, ExternalLink, ArrowLeft, } from "lucide-react"; import { apiService } from "../lib/api-client"; interface GoogleCalendarSettings { connected: boolean; calendar_id: string; calendar_name: string; auto_sync: boolean; sync_bookings: boolean; sync_departures: boolean; send_invitations: boolean; reminder_days: number[]; last_sync: string | null; } const GoogleCalendar: React.FC = () => { const [settings, setSettings] = useState(null); const [loading, setLoading] = useState(true); const [connecting, setConnecting] = useState(false); const [syncing, setSyncing] = useState(false); const { showToast } = useToast(); // This dashboard lives under Settings → Integration; the Back button returns // there (and deep-links the Integration section via ?section=integration). const adminUrl = (window as any).yatraAdmin?.adminUrl || "admin.php"; const settingsIntegrationUrl = `${adminUrl}?page=yatra&subpage=settings§ion=integration`; const __ = (text: string) => text; useEffect(() => { fetchSettings(); }, []); const fetchSettings = async () => { try { const response = await apiService.getGoogleCalendarSettings(); setSettings(response.data); } catch (error) { console.error("Failed to fetch settings:", error); } finally { setLoading(false); } }; const handleConnect = async () => { setConnecting(true); try { const data = await apiService.connectGoogleCalendar(); if (data.success && data.data.auth_url) { // Redirect to Google OAuth window.location.href = data.data.auth_url; } else { showToast(data.message || __("Failed to connect"), "error"); } } catch (error) { showToast(__("Failed to connect to Google Calendar"), "error"); } finally { setConnecting(false); } }; const handleDisconnect = async () => { if (!confirm(__("Are you sure you want to disconnect Google Calendar?"))) { return; } try { await apiService.disconnectGoogleCalendar(); showToast(__("Disconnected successfully"), "success"); fetchSettings(); } catch (error) { showToast(__("Failed to disconnect"), "error"); } }; const handleSyncAll = async () => { setSyncing(true); try { const data = await apiService.syncAllGoogleCalendar(); if (data.success) { showToast(__("Sync completed successfully"), "success"); fetchSettings(); } else { showToast(data.message || __("Sync failed"), "error"); } } catch (error) { showToast(__("Failed to sync bookings"), "error"); } finally { setSyncing(false); } }; const handleSettingChange = async ( key: keyof GoogleCalendarSettings, value: any, ) => { try { await apiService.updateGoogleCalendarSettings({ [key]: value }); setSettings((prev) => (prev ? { ...prev, [key]: value } : null)); showToast(__("Settings updated"), "success"); } catch (error) { showToast(__("Failed to update settings"), "error"); } }; if (loading) { return (
); } return (
{/* Back to Settings → Integration */} {__("Back to Settings → Integration")} {/* Header */}

{__("Google Calendar Integration")}

{__( "Automatically sync your bookings and departures to Google Calendar", )}

{/* Connection Status Card */} {__("Connection Status")} {settings?.connected ? ( {__("Connected")} ) : ( {__("Not Connected")} )} {settings?.connected ? __("Your Google Calendar is connected and ready to sync") : __("Connect your Google account to start syncing bookings")} {settings?.connected ? ( <>

{settings.calendar_name || __("Primary Calendar")}

{__("Calendar ID")}: {settings.calendar_id}

{settings.last_sync && (

{__("Last synced")}:{" "} {new Date(settings.last_sync).toLocaleString()}

)}
) : ( )}
{/* Sync Settings */} {settings?.connected && ( {__("Sync Settings")} {__("Configure what gets synced to your Google Calendar")}

{__("Automatically sync new bookings and changes")}

handleSettingChange("auto_sync", checked) } />

{__("Create calendar events for new bookings")}

handleSettingChange("sync_bookings", checked) } />

{__("Create calendar events for departures")}

handleSettingChange("sync_departures", checked) } />

{__("Send calendar invitations to customers")}

handleSettingChange("send_invitations", checked) } />
)} {/* Documentation */} {__("Documentation")} {__("Learn more about Google Calendar integration")} {__("View Documentation")}
); }; export default GoogleCalendar;