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

216 lines
9.1 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-10-17 14:21:22 +05:30
import { useGlobalInfoStore } from '../../context/globalInfo';
2024-10-17 14:43:45 +05:30
import { getStoredRecording } from '../../api/storage';
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;
spreadsheetName: string;
2024-09-17 20:25:10 +05:30
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: '',
spreadsheetName: '',
2024-09-17 20:25:10 +05:30
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 }[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
2024-10-17 14:21:22 +05:30
const { recordingId } = useGlobalInfoStore();
const [recording, setRecording] = useState<any>(null);
2024-10-17 14:21:22 +05:30
2024-10-16 22:37:53 +05:30
const authenticateWithGoogle = () => {
2024-10-17 14:21:22 +05:30
window.location.href = `http://localhost:8080/auth/google?robotId=${recordingId}`;
2024-10-16 22:37:53 +05:30
};
2024-09-17 17:28:43 +05:30
2024-10-16 22:37:53 +05:30
const handleOAuthCallback = async () => {
try {
2024-10-17 14:21:22 +05:30
const response = await axios.get(`http://localhost:8080/auth/google/callback`);
const { google_sheet_email, files } = response.data;
2024-10-16 22:37:53 +05:30
} 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-17 16:14:20 +05:30
const fetchSpreadsheetFiles = async () => {
try {
2024-10-17 16:15:58 +05:30
const response = await axios.get(`http://localhost:8080/auth/gsheets/files?robotId=${recordingId}`, {
2024-10-17 16:14:20 +05:30
withCredentials: true,
});
setSpreadsheets(response.data);
2024-10-17 16:14:43 +05:30
} catch (error: any) {
2024-10-17 16:14:20 +05:30
console.error('Error fetching spreadsheet files:', error.response?.data?.message || error.message);
}
};
2024-10-16 20:47:14 +05:30
const handleSpreadsheetSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
const selectedSheet = spreadsheets.find(sheet => sheet.id === e.target.value);
if (selectedSheet) {
setSettings({ ...settings, spreadsheetId: selectedSheet.id, spreadsheetName: selectedSheet.name });
}
2024-10-16 20:47:14 +05:30
};
2024-10-17 19:57:09 +05:30
const updateGoogleSheetId = async () => {
try {
const response = await axios.post(
`http://localhost:8080/auth/gsheets/update`,
{ spreadsheetId: settings.spreadsheetId, spreadsheetName: settings.spreadsheetName, robotId: recordingId },
2024-10-17 19:57:09 +05:30
{ withCredentials: true }
);
console.log('Google Sheet ID updated:', response.data);
} catch (error: any) {
console.error('Error updating Google Sheet ID:', error.response?.data?.message || error.message);
}
};
2024-10-21 02:27:45 +05:30
const removeIntegration = async () => {
try {
await axios.post(
`http://localhost:8080/auth/gsheets/remove`,
{ robotId: recordingId },
{ withCredentials: true }
);
2024-10-21 02:29:34 +05:30
setRecording(null);
setSpreadsheets([]);
2024-10-21 02:27:45 +05:30
setSettings({ spreadsheetId: '', spreadsheetName: '', data: '' });
} catch (error: any) {
console.error('Error removing Google Sheets integration:', error.response?.data?.message || error.message);
}
};
2024-10-16 22:37:53 +05:30
useEffect(() => {
2024-10-17 14:21:22 +05:30
// Check if we're on the callback URL
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');
if (code) {
2024-10-16 22:37:53 +05:30
handleOAuthCallback();
}
2024-10-17 15:28:22 +05:30
const fetchRecordingInfo = async () => {
if (!recordingId) return;
const recording = await getStoredRecording(recordingId);
if (recording) {
setRecording(recording);
2024-10-17 15:28:22 +05:30
}
};
fetchRecordingInfo();
}, [recordingId]);
2024-10-16 22:37:53 +05:30
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' }}>
2024-09-17 20:25:10 +05:30
<Typography sx={{ margin: '20px 0px' }}>Google Sheets Integration</Typography>
2024-09-17 20:10:51 +05:30
{recording && recording.google_sheet_id ? (
2024-10-21 02:39:13 +05:30
<>
2024-10-21 02:39:24 +05:30
<Typography sx={{ marginBottom: '10px' }}>
Google Sheet Integrated Successfully!
<br />
Sheet Name: {recording.google_sheet_name}
<br />
Sheet ID: {recording.google_sheet_id}
</Typography>
<Button
variant="outlined"
color="error"
onClick={removeIntegration}
style={{ marginTop: '15px' }}
>
Remove Integration
</Button>
2024-10-21 02:39:13 +05:30
</>
2024-10-16 22:37:53 +05:30
) : (
<>
{!recording?.google_sheet_email ? (
<Button
variant="contained"
color="primary"
onClick={authenticateWithGoogle}
style={{ marginBottom: '15px' }}
>
Authenticate with Google
</Button>
2024-10-16 22:37:53 +05:30
) : (
<>
{recording.google_sheet_email && (
2024-10-16 22:37:53 +05:30
<Typography sx={{ marginBottom: '10px' }}>
Logged in as: {recording.google_sheet_email}
2024-10-16 22:37:53 +05:30
</Typography>
)}
{loading ? (
<CircularProgress sx={{ marginBottom: '15px' }} />
) : error ? (
<Typography color="error">{error}</Typography>
) : spreadsheets.length === 0 ? (
<Button
variant="contained"
color="primary"
onClick={fetchSpreadsheetFiles}
style={{ marginBottom: '15px' }}
>
Fetch Google Spreadsheets
</Button>
) : (
<>
<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>
{settings.spreadsheetId && (
<Typography sx={{ marginBottom: '10px' }}>
Selected Sheet: {spreadsheets.find(s => s.id === settings.spreadsheetId)?.name} (ID: {settings.spreadsheetId})
</Typography>
)}
<Button
variant="contained"
color="primary"
2024-10-17 20:02:04 +05:30
onClick={() => {
updateGoogleSheetId();
handleStart(settings);
}}
style={{ marginTop: '10px' }}
disabled={!settings.spreadsheetId || loading}
>
Submit
</Button>
</>
)}
2024-10-16 22:37:53 +05:30
</>
)}
</>
)}
2024-09-17 20:25:10 +05:30
</div>
</GenericModal>
);
2024-09-17 20:10:51 +05:30
};