Merge pull request 'development' (#25) from development into main
Reviewed-on: #25
This commit is contained in:
@@ -6,12 +6,14 @@
|
||||
let {
|
||||
onSuccess,
|
||||
onError,
|
||||
onDisconnect,
|
||||
disabled = false,
|
||||
size = 'default',
|
||||
variant = 'primary'
|
||||
} = $props<{
|
||||
onSuccess?: (token: string) => void;
|
||||
onError?: (error: string) => void;
|
||||
onDisconnect?: () => void;
|
||||
disabled?: boolean;
|
||||
size?: 'small' | 'default' | 'large';
|
||||
variant?: 'primary' | 'secondary';
|
||||
@@ -41,6 +43,7 @@
|
||||
|
||||
async function handleDisconnect() {
|
||||
await authManager.disconnectGoogle();
|
||||
onDisconnect?.();
|
||||
}
|
||||
|
||||
// Size classes
|
||||
|
||||
@@ -51,25 +51,48 @@ export async function getRecentSpreadsheets(
|
||||
* Get data from a Google Sheet
|
||||
* @param refreshToken - Google refresh token
|
||||
* @param spreadsheetId - ID of the spreadsheet
|
||||
* @param range - Cell range to retrieve (default: A1:Z10)
|
||||
* @param range - Optional cell range. If not provided, it will fetch the entire first sheet.
|
||||
* @returns Sheet data as a 2D array
|
||||
*/
|
||||
export async function getSpreadsheetData(
|
||||
refreshToken: string,
|
||||
spreadsheetId: string,
|
||||
range: string = 'A1:Z10'
|
||||
refreshToken: string,
|
||||
spreadsheetId: string,
|
||||
range?: string
|
||||
): Promise<SheetData> {
|
||||
const oauth = getAuthenticatedClient(refreshToken);
|
||||
const sheets = google.sheets({ version: 'v4', auth: oauth });
|
||||
const oauth = getAuthenticatedClient(refreshToken);
|
||||
const sheets = google.sheets({ version: 'v4', auth: oauth });
|
||||
|
||||
const response = await sheets.spreadsheets.values.get({
|
||||
spreadsheetId,
|
||||
range
|
||||
});
|
||||
let effectiveRange = range;
|
||||
|
||||
return {
|
||||
values: response.data.values || []
|
||||
};
|
||||
// If no range is provided, get the name of the first sheet and use that as the range
|
||||
// to fetch all its content.
|
||||
if (!effectiveRange) {
|
||||
try {
|
||||
const info = await getSpreadsheetInfo(refreshToken, spreadsheetId);
|
||||
const firstSheetName = info.sheets?.[0]?.properties?.title;
|
||||
|
||||
if (firstSheetName) {
|
||||
// To use a sheet name as a range, it must be quoted if it contains spaces or special characters.
|
||||
effectiveRange = `'${firstSheetName}'`;
|
||||
} else {
|
||||
// Fallback if sheet name can't be determined.
|
||||
effectiveRange = 'A1:Z1000'; // A sensible default for a large preview
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Failed to get sheet info for spreadsheet ${spreadsheetId}`, error);
|
||||
// Fallback if the info call fails
|
||||
effectiveRange = 'A1:Z1000';
|
||||
}
|
||||
}
|
||||
|
||||
const response = await sheets.spreadsheets.values.get({
|
||||
spreadsheetId,
|
||||
range: effectiveRange
|
||||
});
|
||||
|
||||
return {
|
||||
values: response.data.values || []
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,17 +2,18 @@ import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { googleSheetsServer } from '$lib/google/sheets/server.js';
|
||||
|
||||
export const GET: RequestHandler = async ({ params, request }) => {
|
||||
export const GET: RequestHandler = async ({ params, request, url }) => {
|
||||
try {
|
||||
const { sheetId } = params;
|
||||
const authHeader = request.headers.get('authorization');
|
||||
|
||||
const range = url.searchParams.get('range') || undefined;
|
||||
|
||||
if (!authHeader?.startsWith('Bearer ')) {
|
||||
return json({ error: 'Missing or invalid authorization header' }, { status: 401 });
|
||||
}
|
||||
|
||||
const refreshToken = authHeader.slice(7);
|
||||
const sheetData = await googleSheetsServer.getSpreadsheetData(refreshToken, sheetId, 'A1:Z10');
|
||||
const sheetData = await googleSheetsServer.getSpreadsheetData(refreshToken, sheetId, range);
|
||||
|
||||
return json(sheetData);
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { isTokenValid, getUserInfo, revokeToken } from '$lib/google/auth/client.js';
|
||||
import type { GoogleSheet } from '$lib/google/sheets/types.ts';
|
||||
import { goto } from '$app/navigation';
|
||||
import { toast } from '$lib/stores/toast.js';
|
||||
@@ -16,19 +15,11 @@
|
||||
let { data } = $props();
|
||||
|
||||
// Step management
|
||||
let currentStep = $state(0); // Start at step 0 for Google auth check
|
||||
const totalSteps = 4; // Increased to include auth step
|
||||
let currentStep = $state(0);
|
||||
const totalSteps = 4;
|
||||
|
||||
// Step 0: Google Auth
|
||||
let authData = $state({
|
||||
isConnected: false,
|
||||
checking: true,
|
||||
connecting: false,
|
||||
showCancelOption: false,
|
||||
token: null as string | null,
|
||||
error: null as string | null,
|
||||
userEmail: null as string | null
|
||||
});
|
||||
let isGoogleConnected = $state(false);
|
||||
|
||||
// Step 1: Event Details
|
||||
let eventData = $state({
|
||||
@@ -42,13 +33,13 @@
|
||||
selectedSheet: null as GoogleSheet | null,
|
||||
sheetData: [] as string[][],
|
||||
columnMapping: {
|
||||
name: 0, // Initialize to 0 (no column selected)
|
||||
name: 0,
|
||||
surname: 0,
|
||||
email: 0,
|
||||
confirmation: 0
|
||||
},
|
||||
loading: false,
|
||||
expandedSheetList: true // Add this flag to control sheet list expansion
|
||||
expandedSheetList: true
|
||||
});
|
||||
|
||||
// Step 3: Email
|
||||
@@ -62,189 +53,11 @@
|
||||
let errors = $state<Record<string, string>>({});
|
||||
|
||||
onMount(async () => {
|
||||
// Check Google auth status on mount
|
||||
await checkGoogleAuth();
|
||||
|
||||
if (currentStep === 2) {
|
||||
await loadRecentSheets();
|
||||
}
|
||||
});
|
||||
|
||||
// Google Auth functions
|
||||
async function checkGoogleAuth() {
|
||||
authData.checking = true;
|
||||
try {
|
||||
const accessToken = localStorage.getItem('google_access_token');
|
||||
const refreshToken = localStorage.getItem('google_refresh_token');
|
||||
|
||||
if (accessToken && refreshToken) {
|
||||
// Check if token is still valid
|
||||
const isValid = await isTokenValid(accessToken);
|
||||
authData.isConnected = isValid;
|
||||
authData.token = accessToken;
|
||||
|
||||
if (isValid) {
|
||||
// Fetch user info
|
||||
await fetchUserInfo(accessToken);
|
||||
}
|
||||
} else {
|
||||
authData.isConnected = false;
|
||||
authData.userEmail = null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error checking Google auth:', error);
|
||||
authData.isConnected = false;
|
||||
authData.error = 'Error checking Google connection';
|
||||
authData.userEmail = null;
|
||||
} finally {
|
||||
authData.checking = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function connectToGoogle() {
|
||||
authData.error = '';
|
||||
authData.connecting = true;
|
||||
|
||||
try {
|
||||
// Open popup window for OAuth
|
||||
const popup = window.open(
|
||||
'/auth/google',
|
||||
'google-auth',
|
||||
'width=500,height=600,scrollbars=yes,resizable=yes,left=' +
|
||||
Math.round(window.screen.width / 2 - 250) +
|
||||
',top=' +
|
||||
Math.round(window.screen.height / 2 - 300)
|
||||
);
|
||||
|
||||
if (!popup) {
|
||||
authData.error = 'Failed to open popup window. Please allow popups for this site.';
|
||||
authData.connecting = false;
|
||||
return;
|
||||
}
|
||||
|
||||
let authCompleted = false;
|
||||
let popupTimer: number | null = null;
|
||||
let cancelTimeout: number | null = null;
|
||||
|
||||
// Store current timestamp to detect changes in localStorage
|
||||
const startTimestamp = localStorage.getItem('google_auth_timestamp') || '0';
|
||||
|
||||
// Poll localStorage for auth completion
|
||||
const pollInterval = setInterval(() => {
|
||||
try {
|
||||
const currentTimestamp = localStorage.getItem('google_auth_timestamp');
|
||||
|
||||
// If timestamp has changed, auth is complete
|
||||
if (currentTimestamp && currentTimestamp !== startTimestamp) {
|
||||
handleAuthSuccess();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error checking auth timestamp:', e);
|
||||
}
|
||||
}, 500); // Poll every 500ms
|
||||
|
||||
// Common handler for authentication success
|
||||
function handleAuthSuccess() {
|
||||
if (authCompleted) return; // Prevent duplicate handling
|
||||
|
||||
authCompleted = true;
|
||||
authData.connecting = false;
|
||||
authData.showCancelOption = false;
|
||||
|
||||
// Clean up timers
|
||||
clearInterval(pollInterval);
|
||||
if (popupTimer) clearTimeout(popupTimer);
|
||||
if (cancelTimeout) clearTimeout(cancelTimeout);
|
||||
|
||||
// Update auth state
|
||||
setTimeout(checkGoogleAuth, 100);
|
||||
}
|
||||
|
||||
// Clean up function to handle all cleanup in one place
|
||||
const cleanUp = () => {
|
||||
clearInterval(pollInterval);
|
||||
if (popupTimer) clearTimeout(popupTimer);
|
||||
if (cancelTimeout) clearTimeout(cancelTimeout);
|
||||
authData.connecting = false;
|
||||
};
|
||||
|
||||
// Set a timeout for initial auth check
|
||||
popupTimer = setTimeout(() => {
|
||||
// Only check if auth isn't already completed
|
||||
if (!authCompleted) {
|
||||
cleanUp();
|
||||
// Check if tokens were stored by the popup before it was closed
|
||||
setTimeout(checkGoogleAuth, 100);
|
||||
}
|
||||
}, 30 * 1000) as unknown as number; // Reduced from 60s to 30s
|
||||
|
||||
// Show cancel option sooner
|
||||
cancelTimeout = setTimeout(() => {
|
||||
if (!authCompleted) {
|
||||
authData.showCancelOption = true;
|
||||
}
|
||||
}, 10 * 1000) as unknown as number; // Reduced from 20s to 10s
|
||||
|
||||
// Final cleanup timeout
|
||||
setTimeout(() => {
|
||||
if (!authCompleted) {
|
||||
cleanUp();
|
||||
}
|
||||
}, 60 * 1000); // Reduced from 3min to 1min
|
||||
} catch (error) {
|
||||
console.error('Error connecting to Google:', error);
|
||||
authData.error = 'Failed to connect to Google';
|
||||
authData.connecting = false;
|
||||
}
|
||||
}
|
||||
|
||||
function cancelGoogleAuth() {
|
||||
authData.connecting = false;
|
||||
authData.showCancelOption = false;
|
||||
}
|
||||
|
||||
async function fetchUserInfo(accessToken: string) {
|
||||
try {
|
||||
// Use the new getUserInfo function from our lib
|
||||
const userData = await getUserInfo(accessToken);
|
||||
if (userData) {
|
||||
authData.userEmail = userData.email;
|
||||
} else {
|
||||
authData.userEmail = null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching user info:', error);
|
||||
authData.userEmail = null;
|
||||
}
|
||||
}
|
||||
|
||||
async function disconnectGoogle() {
|
||||
try {
|
||||
// First revoke the token at Google using our API
|
||||
const accessToken = localStorage.getItem('google_access_token');
|
||||
if (accessToken) {
|
||||
await revokeToken(accessToken);
|
||||
}
|
||||
|
||||
// Remove tokens from local storage
|
||||
localStorage.removeItem('google_access_token');
|
||||
localStorage.removeItem('google_refresh_token');
|
||||
|
||||
// Update auth state
|
||||
authData.isConnected = false;
|
||||
authData.token = null;
|
||||
authData.userEmail = null;
|
||||
|
||||
// Clear any selected sheets data
|
||||
sheetsData.availableSheets = [];
|
||||
sheetsData.selectedSheet = null;
|
||||
sheetsData.sheetData = [];
|
||||
} catch (error) {
|
||||
console.error('Error disconnecting from Google:', error);
|
||||
authData.error = 'Failed to disconnect from Google';
|
||||
}
|
||||
}
|
||||
|
||||
// Step navigation
|
||||
function nextStep() {
|
||||
if (validateCurrentStep()) {
|
||||
@@ -265,7 +78,7 @@
|
||||
let isValid = true;
|
||||
|
||||
if (currentStep === 0) {
|
||||
if (!authData.isConnected) {
|
||||
if (!isGoogleConnected) {
|
||||
toast.error('Please connect your Google account to continue');
|
||||
errors.auth = 'Please connect your Google account to continue';
|
||||
return false;
|
||||
@@ -359,8 +172,8 @@
|
||||
}
|
||||
|
||||
try {
|
||||
// Use the new unified API endpoint
|
||||
const response = await fetch(`/private/api/google/sheets/${sheet.id}/data`, {
|
||||
// Use the new unified API endpoint, requesting only a preview range
|
||||
const response = await fetch(`/private/api/google/sheets/${sheet.id}/data?range=A1:Z10`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Bearer ${localStorage.getItem('google_refresh_token')}`
|
||||
@@ -437,13 +250,13 @@
|
||||
|
||||
// Computed values
|
||||
let canProceed = $derived(() => {
|
||||
if (currentStep === 0) return authData.isConnected;
|
||||
if (currentStep === 1) return eventData.name && eventData.date;
|
||||
if (currentStep === 0) return isGoogleConnected;
|
||||
if (currentStep === 1) return !!(eventData.name && eventData.date);
|
||||
if (currentStep === 2) {
|
||||
const { name, surname, email, confirmation } = sheetsData.columnMapping;
|
||||
return sheetsData.selectedSheet && name && surname && email && confirmation;
|
||||
return !!(sheetsData.selectedSheet && name && surname && email && confirmation);
|
||||
}
|
||||
if (currentStep === 3) return emailData.subject && emailData.body;
|
||||
if (currentStep === 3) return !!(emailData.subject && emailData.body);
|
||||
return false;
|
||||
});
|
||||
</script>
|
||||
@@ -455,16 +268,9 @@
|
||||
<div class="mb-4 rounded border border-gray-300 bg-white p-6">
|
||||
{#if currentStep === 0}
|
||||
<GoogleAuthStep
|
||||
onSuccess={(token) => {
|
||||
authData.error = null;
|
||||
authData.token = token;
|
||||
authData.isConnected = true;
|
||||
setTimeout(checkGoogleAuth, 100);
|
||||
}}
|
||||
onError={(error) => {
|
||||
authData.error = error;
|
||||
authData.isConnected = false;
|
||||
}}
|
||||
onSuccess={() => (isGoogleConnected = true)}
|
||||
onDisconnect={() => (isGoogleConnected = false)}
|
||||
onError={(err) => toast.error(err)}
|
||||
/>
|
||||
{:else if currentStep === 1}
|
||||
<EventDetailsStep bind:eventData />
|
||||
@@ -485,7 +291,7 @@
|
||||
<StepNavigation
|
||||
{currentStep}
|
||||
{totalSteps}
|
||||
{canProceed}
|
||||
canProceed={canProceed()}
|
||||
{loading}
|
||||
{prevStep}
|
||||
{nextStep}
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
import GoogleAuthButton from '$lib/components/GoogleAuthButton.svelte';
|
||||
|
||||
// Props
|
||||
let { onSuccess, onError } = $props<{
|
||||
let { onSuccess, onError, onDisconnect } = $props<{
|
||||
onSuccess?: (token: string) => void;
|
||||
onError?: (error: string) => void;
|
||||
onDisconnect?: () => void;
|
||||
}>();
|
||||
</script>
|
||||
|
||||
@@ -15,11 +16,12 @@
|
||||
To create events and import participants from Google Sheets, you need to connect your Google account.
|
||||
</p>
|
||||
|
||||
<GoogleAuthButton
|
||||
size="large"
|
||||
variant="primary"
|
||||
onSuccess={onSuccess}
|
||||
onError={onError}
|
||||
/>
|
||||
<GoogleAuthButton
|
||||
size="large"
|
||||
variant="primary"
|
||||
{onSuccess}
|
||||
{onError}
|
||||
{onDisconnect}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -115,6 +115,8 @@
|
||||
}
|
||||
|
||||
syncingParticipants = true;
|
||||
const previousCount = participants.length; // Capture count before sync
|
||||
|
||||
try {
|
||||
// Fetch sheet data
|
||||
const response = await fetch(`/private/api/google/sheets/${event.sheet_id}/data`, {
|
||||
@@ -177,16 +179,23 @@
|
||||
|
||||
// Reload participants
|
||||
await loadParticipants();
|
||||
|
||||
// Show success message with count of synced participants
|
||||
const previousCount = participants.length;
|
||||
const newCount = names.length;
|
||||
const addedCount = Math.max(0, participants.length - previousCount);
|
||||
|
||||
toast.success(
|
||||
`Successfully synced participants. ${newCount} entries processed, ${addedCount} new participants added.`,
|
||||
5000
|
||||
);
|
||||
|
||||
// Show success message with accurate count of changes
|
||||
const newCount = participants.length;
|
||||
const diff = newCount - previousCount;
|
||||
const processedCount = names.length;
|
||||
|
||||
let message = `Sync complete. ${processedCount} confirmed entries processed from the sheet.`;
|
||||
|
||||
if (diff > 0) {
|
||||
message += ` ${diff} new participants added.`;
|
||||
} else if (diff < 0) {
|
||||
message += ` ${-diff} participants removed.`;
|
||||
} else {
|
||||
message += ` No changes to the participant list.`;
|
||||
}
|
||||
|
||||
toast.success(message, 6000);
|
||||
} catch (err) {
|
||||
console.error('Error syncing participants:', err);
|
||||
toast.error(`Failed to sync participants: ${err instanceof Error ? err.message : 'Unknown error'}`);
|
||||
|
||||
Reference in New Issue
Block a user