import React, { useState, useEffect } from 'react'; import { GenericModal } from "../atoms/GenericModal"; import { MenuItem, Typography, CircularProgress } from "@mui/material"; import Button from "@mui/material/Button"; import TextField from "@mui/material/TextField"; import axios from 'axios'; interface IntegrationProps { isOpen: boolean; handleStart: (data: IntegrationSettings) => void; handleClose: () => void; } export interface IntegrationSettings { spreadsheetId: string; data: string; } export const IntegrationSettingsModal = ({ isOpen, handleStart, handleClose }: IntegrationProps) => { const [settings, setSettings] = useState({ spreadsheetId: '', data: '', }); const [spreadsheets, setSpreadsheets] = useState<{ id: string, name: string }[]>([]); const [userInfo, setUserInfo] = useState<{ email: string } | null>(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [isAuthenticated, setIsAuthenticated] = useState(false); // Function to trigger Google OAuth authentication const authenticateWithGoogle = () => { window.location.href = 'http://localhost:8080/auth/google'; // Redirect to backend Google OAuth route }; // Function to handle Google OAuth callback and fetch spreadsheets const handleOAuthCallback = async () => { try { const response = await axios.post('http://localhost:8080/auth/google/callback', { // code: new URLSearchParams(window.location.search).get('code'), robotId: '' }); const { google_sheet_email, files } = response.data; setUserInfo({ email:google_sheet_email }); setSpreadsheets(files); setIsAuthenticated(true); } catch (error) { setError('Error authenticating with Google'); } }; // Handle spreadsheet selection const handleSpreadsheetSelect = (e: React.ChangeEvent) => { setSettings({ ...settings, spreadsheetId: e.target.value }); }; useEffect(() => { // Simulate handling OAuth callback here after redirect if (window.location.pathname === 'http://localhost:8080/auth/google/callback') { handleOAuthCallback(); } }, [isOpen]); return (
Google Sheets Integration {/* If user is not authenticated, show Google OAuth button */} {!isAuthenticated ? ( ) : ( <> {/* Show user info and allow spreadsheet selection once authenticated */} {userInfo && ( Logged in as: {userInfo.email} )} {loading ? ( ) : error ? ( {error} ) : ( <> {spreadsheets.map(sheet => ( {sheet.name} ))} {/* Display selected spreadsheet name */} {settings.spreadsheetId && ( Selected Spreadsheet ID: {settings.spreadsheetId} )} )} )}
); };