From e4b5d922db1baa387366b88185fe166904a8cb6d Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Wed, 16 Oct 2024 22:37:53 +0530 Subject: [PATCH] feat: handle oauth --- .../molecules/IntegrationSettings.tsx | 162 ++++++++++-------- 1 file changed, 88 insertions(+), 74 deletions(-) diff --git a/src/components/molecules/IntegrationSettings.tsx b/src/components/molecules/IntegrationSettings.tsx index 485c762c..a7c68c96 100644 --- a/src/components/molecules/IntegrationSettings.tsx +++ b/src/components/molecules/IntegrationSettings.tsx @@ -1,9 +1,9 @@ import React, { useState, useEffect } from 'react'; import { GenericModal } from "../atoms/GenericModal"; -import { MenuItem, TextField, Typography, CircularProgress } from "@mui/material"; +import { MenuItem, Typography, CircularProgress } from "@mui/material"; import Button from "@mui/material/Button"; +import TextField from "@mui/material/TextField"; import axios from 'axios'; -import { modalStyle } from "./AddWhereCondModal"; interface IntegrationProps { isOpen: boolean; @@ -12,52 +12,56 @@ interface IntegrationProps { } export interface IntegrationSettings { - credentials: string; spreadsheetId: string; - range: string; data: string; } export const IntegrationSettingsModal = ({ isOpen, handleStart, handleClose }: IntegrationProps) => { const [settings, setSettings] = useState({ - credentials: '', spreadsheetId: '', - range: '', 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); - useEffect(() => { - if (isOpen) { - // Fetch Google Sheets from backend when modal is opened - setLoading(true); - axios.get('http://localhost:8080/auth/google/callback') - .then(response => { - setSpreadsheets(response.data.files); - setLoading(false); - }) - .catch(error => { - setError(`Error fetching spreadsheets: ${error}`); - setLoading(false); - }); - } - }, [isOpen]); - - const handleChange = (field: keyof IntegrationSettings) => (e: React.ChangeEvent) => { - setSettings({ ...settings, [field]: e.target.value }); + // 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.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'); + } + }; + + // 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 - - - {loading ? ( - - ) : error ? ( - {error} - ) : ( - - {spreadsheets.map(sheet => ( - - {sheet.name} - - ))} - + Authenticate with Google + + ) : ( + <> + {/* 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} + + )} + + )} + + + )} - - - -
);