integrationsettings

This commit is contained in:
AmitChauhan63390
2025-01-26 14:33:34 +05:30
parent 2009089e2d
commit 6dfdb496db

View File

@@ -71,62 +71,62 @@ export const IntegrationSettingsModal = ({
const redirectUri = `${window.location.origin}/google/callback`; const redirectUri = `${window.location.origin}/google/callback`;
window.location.href = `${apiUrl}/auth/google?robotId=${recordingId}&redirect_uri=${encodeURIComponent(redirectUri)}`; window.location.href = `${apiUrl}/auth/google?robotId=${recordingId}&redirect_uri=${encodeURIComponent(redirectUri)}`;
}; };
// Authenticate with Airtable
const authenticateWithAirtable = () => { const authenticateWithAirtable = () => {
const redirectUri = `${window.location.origin}/airtable/callback`; const redirectUri = `${window.location.origin}/airtable/callback`;
window.location.href = `${apiUrl}/auth/airtable?robotId=${recordingId}&redirect_uri=${encodeURIComponent(redirectUri)}`; window.location.href = `${apiUrl}/auth/airtable?robotId=${recordingId}&redirect_uri=${encodeURIComponent(redirectUri)}`;
}; };
// Handle OAuth callback // Handle Google OAuth callback
const handleGoogleCallback = async () => { const handleGoogleCallback = async () => {
try { try {
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code'); const code = urlParams.get("code");
if (!code) { if (!code) {
setError(t("integration_settings.errors.no_auth_code")); setError(t("integration_settings.errors.no_auth_code"));
return; return;
} }
const response = await axios.get( const response = await axios.get(
`${apiUrl}/auth/google/callback?code=${code}&robotId=${recordingId}` `${apiUrl}/auth/google/callback?code=${code}&robotId=${recordingId}`
); );
if (response.data.accessToken) { if (response.data.accessToken) {
notify("success", t("integration_settings.notifications.google_auth_success")); notify("success", t("integration_settings.notifications.google_auth_success"));
await fetchSpreadsheetFiles(); await fetchSpreadsheetFiles();
} }
// Clear URL parameters // Clear URL parameters
window.history.replaceState({}, document.title, window.location.pathname); window.history.replaceState({}, document.title, window.location.pathname);
} catch (error) { } catch (error) {
setError(t("integration_settings.errors.google_auth_error")); setError(t("integration_settings.errors.google_auth_error"));
} }
}; };
// Handle Airtable OAuth callback
const handleAirtableCallback = async () => { const handleAirtableCallback = async () => {
try { try {
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code'); const code = urlParams.get("code");
if (!code) { if (!code) {
setError(t("integration_settings.errors.no_auth_code")); setError(t("integration_settings.errors.no_auth_code"));
return; return;
} }
const response = await axios.get( const response = await axios.get(
`${apiUrl}/auth/airtable/callback?code=${code}&robotId=${recordingId}` `${apiUrl}/auth/airtable/callback?code=${code}&robotId=${recordingId}`
); );
if (response.data.accessToken) { if (response.data.accessToken) {
notify("success", t("integration_settings.notifications.airtable_auth_success")); notify("success", t("integration_settings.notifications.airtable_auth_success"));
await fetchAirtableBases(); await fetchAirtableBases();
} }
// Clear URL parameters // Clear URL parameters
window.history.replaceState({}, document.title, window.location.pathname); window.history.replaceState({}, document.title, window.location.pathname);
} catch (error) { } catch (error) {
setError(t("integration_settings.errors.airtable_auth_error")); setError(t("integration_settings.errors.airtable_auth_error"));
} }
@@ -135,6 +135,7 @@ export const IntegrationSettingsModal = ({
// Fetch Google Sheets // Fetch Google Sheets
const fetchSpreadsheetFiles = async () => { const fetchSpreadsheetFiles = async () => {
try { try {
setLoading(true);
const response = await axios.get(`${apiUrl}/auth/gsheets/files?robotId=${recordingId}`, { const response = await axios.get(`${apiUrl}/auth/gsheets/files?robotId=${recordingId}`, {
withCredentials: true, withCredentials: true,
}); });
@@ -142,12 +143,15 @@ export const IntegrationSettingsModal = ({
} catch (error: any) { } catch (error: any) {
console.error("Error fetching spreadsheet files:", error.response?.data?.message || error.message); console.error("Error fetching spreadsheet files:", error.response?.data?.message || error.message);
notify("error", t("integration_settings.errors.fetch_error", { message: error.response?.data?.message || error.message })); notify("error", t("integration_settings.errors.fetch_error", { message: error.response?.data?.message || error.message }));
} finally {
setLoading(false);
} }
}; };
// Fetch Airtable Bases // Fetch Airtable Bases
const fetchAirtableBases = async () => { const fetchAirtableBases = async () => {
try { try {
setLoading(true);
const response = await axios.get(`${apiUrl}/auth/airtable/bases?robotId=${recordingId}`, { const response = await axios.get(`${apiUrl}/auth/airtable/bases?robotId=${recordingId}`, {
withCredentials: true, withCredentials: true,
}); });
@@ -155,6 +159,8 @@ export const IntegrationSettingsModal = ({
} catch (error: any) { } catch (error: any) {
console.error("Error fetching Airtable bases:", error.response?.data?.message || error.message); console.error("Error fetching Airtable bases:", error.response?.data?.message || error.message);
notify("error", t("integration_settings.errors.fetch_error", { message: error.response?.data?.message || error.message })); notify("error", t("integration_settings.errors.fetch_error", { message: error.response?.data?.message || error.message }));
} finally {
setLoading(false);
} }
}; };
@@ -185,6 +191,7 @@ export const IntegrationSettingsModal = ({
// Update Google Sheet ID // Update Google Sheet ID
const updateGoogleSheetId = async () => { const updateGoogleSheetId = async () => {
try { try {
setLoading(true);
const response = await axios.post( const response = await axios.post(
`${apiUrl}/auth/gsheets/update`, `${apiUrl}/auth/gsheets/update`,
{ {
@@ -198,12 +205,15 @@ export const IntegrationSettingsModal = ({
console.log("Google Sheet ID updated:", response.data); console.log("Google Sheet ID updated:", response.data);
} catch (error: any) { } catch (error: any) {
console.error("Error updating Google Sheet ID:", error.response?.data?.message || error.message); console.error("Error updating Google Sheet ID:", error.response?.data?.message || error.message);
} finally {
setLoading(false);
} }
}; };
// Update Airtable Base ID // Update Airtable Base ID
const updateAirtableBaseId = async () => { const updateAirtableBaseId = async () => {
try { try {
setLoading(true);
const response = await axios.post( const response = await axios.post(
`${apiUrl}/auth/airtable/update`, `${apiUrl}/auth/airtable/update`,
{ {
@@ -217,12 +227,15 @@ export const IntegrationSettingsModal = ({
console.log("Airtable Base ID updated:", response.data); console.log("Airtable Base ID updated:", response.data);
} catch (error: any) { } catch (error: any) {
console.error("Error updating Airtable Base ID:", error.response?.data?.message || error.message); console.error("Error updating Airtable Base ID:", error.response?.data?.message || error.message);
} finally {
setLoading(false);
} }
}; };
// Remove Integration // Remove Integration
const removeIntegration = async () => { const removeIntegration = async () => {
try { try {
setLoading(true);
await axios.post( await axios.post(
`${apiUrl}/auth/gsheets/remove`, `${apiUrl}/auth/gsheets/remove`,
{ robotId: recordingId }, { robotId: recordingId },
@@ -232,9 +245,18 @@ export const IntegrationSettingsModal = ({
setRecording(null); setRecording(null);
setSpreadsheets([]); setSpreadsheets([]);
setAirtableBases([]); setAirtableBases([]);
setSettings({ spreadsheetId: "", spreadsheetName: "", airtableBaseId: "", airtableBaseName: "", data: "" }); setSettings({
spreadsheetId: "",
spreadsheetName: "",
airtableBaseId: "",
airtableBaseName: "",
data: "",
});
notify("success", t("integration_settings.notifications.integration_removed"));
} catch (error: any) { } catch (error: any) {
console.error("Error removing integration:", error.response?.data?.message || error.message); console.error("Error removing integration:", error.response?.data?.message || error.message);
} finally {
setLoading(false);
} }
}; };
@@ -242,25 +264,37 @@ export const IntegrationSettingsModal = ({
const checkAuthCallback = () => { const checkAuthCallback = () => {
const path = window.location.pathname; const path = window.location.pathname;
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code'); const code = urlParams.get("code");
if (code) { if (code) {
if (path.includes('/google/callback')) { if (path.includes("/google/callback")) {
handleGoogleCallback(); handleGoogleCallback();
} else if (path.includes('/airtable/callback')) { } else if (path.includes("/airtable/callback")) {
handleAirtableCallback(); handleAirtableCallback();
} }
} }
}; };
checkAuthCallback(); checkAuthCallback();
// Cleanup function // Cleanup function
return () => { return () => {
window.history.replaceState({}, document.title, window.location.pathname); window.history.replaceState({}, document.title, window.location.pathname);
}; };
}, []); }, []);
useEffect(() => {
const fetchRecordingInfo = async () => {
if (!recordingId) return;
const recording = await getStoredRecording(recordingId);
if (recording) {
setRecording(recording);
}
};
fetchRecordingInfo();
}, [recordingId]);
return ( return (
<GenericModal isOpen={isOpen} onClose={handleClose} modalStyle={modalStyle}> <GenericModal isOpen={isOpen} onClose={handleClose} modalStyle={modalStyle}>
<div style={{ display: "flex", flexDirection: "column", alignItems: "flex-start", marginLeft: "65px" }}> <div style={{ display: "flex", flexDirection: "column", alignItems: "flex-start", marginLeft: "65px" }}>