diff --git a/scripts/seed.ts b/scripts/seed.ts index 83b452c..5acdab7 100644 --- a/scripts/seed.ts +++ b/scripts/seed.ts @@ -13,6 +13,15 @@ function seed() { ) `); + // Create email_templates table + db.exec(` + CREATE TABLE IF NOT EXISTS email_templates ( + id INTEGER PRIMARY KEY, + subject TEXT, + body TEXT + ) + `); + // Check if the hourly_rate setting already exists const settingStmt = db.prepare('SELECT * FROM settings WHERE key = ?'); const hourlyRateSetting = settingStmt.get('hourly_rate'); @@ -28,6 +37,32 @@ function seed() { console.log('Hourly rate setting already exists.'); } + // Check if default email template exists + const templateStmt = db.prepare('SELECT * FROM email_templates WHERE id = ?'); + const defaultTemplate = templateStmt.get(1); + + if (!defaultTemplate) { + const insertTemplate = db.prepare( + "INSERT INTO email_templates (id, subject, body) VALUES (?, ?, ?)" + ); + const defaultSubject = "Your Project Estimate is Ready!"; + const defaultBody = `Hello, [User Name], + +Thank you for using EstimateFlow. We've prepared a rough estimate for your project based on your selections. + +[EstimateDetails] + +Please note that this is a preliminary estimate. For a more detailed quote and to discuss your project further, please don't hesitate to contact us. + +Best regards, +The EstimateFlow Team`; + insertTemplate.run(1, defaultSubject, defaultBody); + console.log('Default email template created.'); + } else { + console.log('Default email template already exists.'); + } + + console.log('Seeding complete.'); } diff --git a/src/app/admin/settings/email-templates/page.tsx b/src/app/admin/settings/email-templates/page.tsx index a7589ec..11cedaf 100644 --- a/src/app/admin/settings/email-templates/page.tsx +++ b/src/app/admin/settings/email-templates/page.tsx @@ -1,8 +1,95 @@ +'use client'; + +import { useState, useEffect } from 'react'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; +import { useToast } from '@/hooks/use-toast'; +import { getEmailTemplate, updateEmailTemplate } from '@/lib/actions/email'; +import { Input } from '@/components/ui/input'; +import { Textarea } from '@/components/ui/textarea'; +import { Label } from '@/components/ui/label'; + +type EmailTemplate = { + subject: string; + body: string; +}; + +function EmailPreview({ template }: { template: EmailTemplate }) { + const previewBody = template.body.replace( + '[EstimateDetails]', + `
+
+

Custom Development Estimate

+

[Custom Hours]+ hours

+
+
+

Ready-Made Tools Estimate

+

[Ready-Made Hours]+ hours

+
+
` + ).replace(/\n/g, '
'); + + return ( +
+
+

{template.subject}

+
+
+
+ ); +} export default function EmailTemplatesPage() { + const [isEditing, setIsEditing] = useState(false); + const [template, setTemplate] = useState({ subject: '', body: '' }); + const [isLoading, setIsLoading] = useState(true); + const { toast } = useToast(); + + useEffect(() => { + async function fetchTemplate() { + setIsLoading(true); + try { + const fetchedTemplate = await getEmailTemplate(); + if (fetchedTemplate) { + setTemplate(fetchedTemplate); + } else { + throw new Error('Could not find email template.'); + } + } catch (error: any) { + toast({ + variant: 'destructive', + title: 'Error', + description: error.message || 'Failed to load email template.', + }); + } finally { + setIsLoading(false); + } + } + fetchTemplate(); + }, [toast]); + + const handleSave = async () => { + try { + const result = await updateEmailTemplate(template); + if (result.success) { + toast({ + title: 'Success', + description: 'Email template updated successfully.', + }); + setIsEditing(false); + } else { + throw new Error(result.message); + } + } catch (error: any) { + toast({ + variant: 'destructive', + title: 'Error', + description: error.message || 'Failed to save template.', + }); + } + }; + return (
@@ -18,33 +105,45 @@ export default function EmailTemplatesPage() { Estimate Completion Email This is the email users receive.
- + {!isEditing ? ( + + ) : ( +
+ + +
+ )}
-
-
-

Your Project Estimate is Ready!

-

Hello, [User Name],

-

Thank you for using EstimateFlow. We've prepared a rough estimate for your project based on your selections.

- -
-
-

Custom Development Estimate

-

[Custom Hours]+ hours

-
-
-

Ready-Made Tools Estimate

-

[Ready-Made Hours]+ hours

-
+ {isLoading ? ( +

Loading template...

+ ) : isEditing ? ( +
+
+ + setTemplate({ ...template, subject: e.target.value })} + /> +
+
+ +