Files
parcer/src/components/molecules/IntegrationSettings.tsx

139 lines
5.4 KiB
TypeScript
Raw Normal View History

2024-10-16 20:47:14 +05:30
import React, { useState, useEffect } from 'react';
2024-09-17 17:28:43 +05:30
import { GenericModal } from "../atoms/GenericModal";
2024-10-16 22:37:53 +05:30
import { MenuItem, Typography, CircularProgress } from "@mui/material";
2024-09-17 17:28:43 +05:30
import Button from "@mui/material/Button";
2024-10-16 22:37:53 +05:30
import TextField from "@mui/material/TextField";
2024-10-16 20:47:14 +05:30
import axios from 'axios';
2024-09-17 17:28:43 +05:30
2024-09-17 20:16:50 +05:30
interface IntegrationProps {
2024-09-17 20:25:10 +05:30
isOpen: boolean;
handleStart: (data: IntegrationSettings) => void;
handleClose: () => void;
2024-09-17 17:28:43 +05:30
}
2024-09-17 20:16:50 +05:30
export interface IntegrationSettings {
2024-09-17 20:25:10 +05:30
spreadsheetId: string;
data: string;
2024-09-17 17:28:43 +05:30
}
2024-09-17 20:24:36 +05:30
export const IntegrationSettingsModal = ({ isOpen, handleStart, handleClose }: IntegrationProps) => {
2024-09-17 20:25:10 +05:30
const [settings, setSettings] = useState<IntegrationSettings>({
spreadsheetId: '',
data: '',
});
2024-10-16 22:37:53 +05:30
2024-10-16 20:47:14 +05:30
const [spreadsheets, setSpreadsheets] = useState<{ id: string, name: string }[]>([]);
2024-10-16 22:37:53 +05:30
const [userInfo, setUserInfo] = useState<{ email: string } | null>(null);
2024-10-16 20:47:14 +05:30
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
2024-10-16 22:37:53 +05:30
const [isAuthenticated, setIsAuthenticated] = useState(false);
2024-10-16 20:47:14 +05:30
2024-10-16 22:37:53 +05:30
// Function to trigger Google OAuth authentication
const authenticateWithGoogle = () => {
window.location.href = 'http://localhost:8080/auth/google'; // Redirect to backend Google OAuth route
};
2024-09-17 17:28:43 +05:30
2024-10-16 22:37:53 +05:30
// Function to handle Google OAuth callback and fetch spreadsheets
const handleOAuthCallback = async () => {
try {
const response = await axios.get('http://localhost:8080/auth/google/callback');
const { email, files } = response.data;
setUserInfo({ email });
setSpreadsheets(files);
setIsAuthenticated(true);
} catch (error) {
setError('Error authenticating with Google');
}
2024-09-17 20:25:10 +05:30
};
2024-09-17 20:10:51 +05:30
2024-10-16 22:37:53 +05:30
// Handle spreadsheet selection
2024-10-16 20:47:14 +05:30
const handleSpreadsheetSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
setSettings({ ...settings, spreadsheetId: e.target.value });
};
2024-10-16 22:37:53 +05:30
useEffect(() => {
// Simulate handling OAuth callback here after redirect
if (window.location.pathname === 'http://localhost:8080/auth/google/callback') {
handleOAuthCallback();
}
}, [isOpen]);
2024-09-17 20:25:10 +05:30
return (
<GenericModal
isOpen={isOpen}
onClose={handleClose}
>
<div style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'flex-start',
marginLeft: '65px',
}}>
<Typography sx={{ margin: '20px 0px' }}>Google Sheets Integration</Typography>
2024-09-17 20:10:51 +05:30
2024-10-16 22:37:53 +05:30
{/* If user is not authenticated, show Google OAuth button */}
{!isAuthenticated ? (
<Button
variant="contained"
color="primary"
onClick={authenticateWithGoogle}
style={{ marginBottom: '15px' }}
2024-10-16 20:47:14 +05:30
>
2024-10-16 22:37:53 +05:30
Authenticate with Google
</Button>
) : (
<>
{/* Show user info and allow spreadsheet selection once authenticated */}
{userInfo && (
<Typography sx={{ marginBottom: '10px' }}>
Logged in as: {userInfo.email}
</Typography>
)}
2024-09-17 20:10:51 +05:30
2024-10-16 22:37:53 +05:30
{loading ? (
<CircularProgress sx={{ marginBottom: '15px' }} />
) : error ? (
<Typography color="error">{error}</Typography>
) : (
<>
<TextField
sx={{ marginBottom: '15px' }}
select
label="Select Google Spreadsheet"
required
value={settings.spreadsheetId}
onChange={handleSpreadsheetSelect}
fullWidth
>
{spreadsheets.map(sheet => (
<MenuItem key={sheet.id} value={sheet.id}>
{sheet.name}
</MenuItem>
))}
</TextField>
2024-09-17 20:10:51 +05:30
2024-10-16 22:37:53 +05:30
{/* Display selected spreadsheet name */}
{settings.spreadsheetId && (
<Typography sx={{ marginBottom: '10px' }}>
Selected Spreadsheet ID: {settings.spreadsheetId}
</Typography>
)}
</>
)}
<Button
variant="contained"
color="primary"
onClick={() => handleStart(settings)}
style={{ marginTop: '10px' }}
disabled={!settings.spreadsheetId || loading}
>
Submit
</Button>
</>
)}
2024-09-17 20:25:10 +05:30
</div>
</GenericModal>
);
2024-09-17 20:10:51 +05:30
};