From 396d29c76bf07282cf0c58e565db1354c086ddd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Kr=C4=8Dek?= Date: Mon, 14 Jul 2025 21:25:57 +0200 Subject: [PATCH] Make emails editable --- .../private/events/event/view/+page.svelte | 45 ++++++- .../view/components/EmailTemplate.svelte | 123 ++++++++++++++++-- 2 files changed, 156 insertions(+), 12 deletions(-) diff --git a/src/routes/private/events/event/view/+page.svelte b/src/routes/private/events/event/view/+page.svelte index 6daffb4..367e143 100644 --- a/src/routes/private/events/event/view/+page.svelte +++ b/src/routes/private/events/event/view/+page.svelte @@ -10,7 +10,6 @@ import EmailSending from './components/EmailSending.svelte'; import EmailResults from './components/EmailResults.svelte'; import Statistics from './components/Statistics.svelte'; - import ToastContainer from '$lib/components/ToastContainer.svelte'; import { toast } from '$lib/stores/toast.js'; let { data } = $props(); @@ -48,6 +47,7 @@ let participantsLoading = $state(true); let syncingParticipants = $state(false); let sendingEmails = $state(false); + let updatingEmail = $state(false); let emailProgress = $state({ sent: 0, total: 0 }); let emailResults = $state<{success: boolean, results: any[], summary: any} | null>(null); @@ -268,6 +268,42 @@ } } + // For Email Template updating + async function handleEmailUpdate(eventId: string, subject: string, body: string) { + updatingEmail = true; + + try { + // Call the email_modify RPC function + const { error } = await data.supabase.rpc('email_modify', { + p_event_id: eventId, + p_email_subject: subject, + p_email_body: body + }); + + if (error) throw error; + + // Update the local event data on success + if (event) { + event.email_subject = subject; + event.email_body = body; + } + + toast.add({ + message: 'Email template updated successfully', + type: 'success' + }); + + } catch (err) { + console.error('Error updating email template:', err); + toast.add({ + message: 'Failed to update email template', + type: 'error' + }); + } finally { + updatingEmail = false; + } + } + function handleGoogleAuthSuccess() { // Success handled by toast in the component } @@ -312,7 +348,12 @@ onSyncParticipants={syncParticipants} /> - + interface Event { + id: string; email_subject: string; email_body: string; } - let { event, loading } = $props<{ + let { + event, + loading, + updatingEmail, + onUpdateEmail + } = $props<{ event: Event | null; loading: boolean; + updatingEmail: boolean; + onUpdateEmail: (eventId: string, subject: string, body: string) => void; }>(); + + // State for form + let isEditing = $state(false); + let emailSubject = $state(''); + let emailBody = $state(''); + + // Update form values when event changes + $effect(() => { + if (event) { + emailSubject = event.email_subject; + emailBody = event.email_body; + } + }); + + // Toggle editing mode + function toggleEdit() { + isEditing = !isEditing; + // Reset form when exiting edit mode without saving + if (!isEditing && event) { + emailSubject = event.email_subject; + emailBody = event.email_body; + } + } + + // Track the previous updatingEmail state to detect changes + let wasUpdating = $state(false); + + // Save email template + function handleSave() { + if (!event) return; + onUpdateEmail(event.id, emailSubject, emailBody); + } + + // Watch for updatingEmail changes to detect when operation completes + $effect(() => { + // Detect the transition from updating to not updating (operation completed) + if (wasUpdating && !updatingEmail) { + // If event data matches our form data, the update was successful + // Turn off editing mode after successful update + if (event && event.email_subject === emailSubject && event.email_body === emailBody) { + isEditing = false; + } + } + + // Store current state for next comparison + wasUpdating = updatingEmail; + });
-
+

Email Template

+ {#if !loading && event} +
+ {#if isEditing} + + + {:else} + + {/if} +
+ {/if}
{#if loading} @@ -31,17 +117,34 @@ {:else if event}
- Subject: -
-

{event.email_subject}

-
+ +
+
- Body: -
-

{event.email_body}

-
+ + + {#if isEditing} +
+ Template variables: {name}, + {surname} +
+ {/if}
+ +
{/if}