analyze the image
This commit is contained in:
@@ -56,21 +56,12 @@ export default function EmailTemplatesPage() {
|
|||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const fetchedTemplate = await getEmailTemplate();
|
const fetchedTemplate = await getEmailTemplate();
|
||||||
if (fetchedTemplate) {
|
|
||||||
setTemplate(fetchedTemplate);
|
setTemplate(fetchedTemplate);
|
||||||
setOriginalTemplate(fetchedTemplate);
|
setOriginalTemplate(fetchedTemplate);
|
||||||
} else {
|
|
||||||
toast({
|
|
||||||
variant: 'destructive',
|
|
||||||
title: 'Template Not Found',
|
|
||||||
description: 'No email template found in the database. You can create one by saving a new template.',
|
|
||||||
});
|
|
||||||
setIsEditing(true); // Default to editing if no template exists
|
|
||||||
}
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
toast({
|
toast({
|
||||||
variant: 'destructive',
|
variant: 'destructive',
|
||||||
title: 'Error',
|
title: 'Error Loading Template',
|
||||||
description: error.message || 'Failed to load email template.',
|
description: error.message || 'Failed to load email template.',
|
||||||
});
|
});
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
+61
-17
@@ -10,27 +10,77 @@ const emailTemplateSchema = z.object({
|
|||||||
body: z.string().min(1, 'Body is required.'),
|
body: z.string().min(1, 'Body is required.'),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const defaultSubject = "Your EstimateFlow Project Estimate is Ready!";
|
||||||
|
const defaultBody = `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Your Project Estimate</title>
|
||||||
|
</head>
|
||||||
|
<body style="font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f4f4f4; color: #333;">
|
||||||
|
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="max-width: 600px; margin: auto; background-color: #ffffff; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
|
||||||
|
<tr>
|
||||||
|
<td style="padding: 20px; text-align: center; background-color: #4A90E2; color: #ffffff; border-top-left-radius: 8px; border-top-right-radius: 8px;">
|
||||||
|
<h1 style="margin: 0; font-size: 24px;">EstimateFlow</h1>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td style="padding: 30px 20px;">
|
||||||
|
<h2 style="font-size: 20px; color: #333;">Hello, [User Name],</h2>
|
||||||
|
<p style="font-size: 16px; line-height: 1.6;">
|
||||||
|
Thank you for using EstimateFlow. We've prepared a rough estimate for your project based on your selections.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
[EstimateDetails]
|
||||||
|
|
||||||
|
<p style="font-size: 16px; line-height: 1.6; margin-top: 20px;">
|
||||||
|
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.
|
||||||
|
</p>
|
||||||
|
<div style="text-align: center; margin-top: 30px;">
|
||||||
|
<a href="#" style="background-color: #4A90E2; color: #ffffff; padding: 12px 25px; text-decoration: none; border-radius: 5px; font-size: 16px;">Contact Us</a>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td style="padding: 20px; text-align: center; font-size: 12px; color: #777; border-top: 1px solid #eaeaea;">
|
||||||
|
<p>Best regards,<br>The EstimateFlow Team</p>
|
||||||
|
<p>© ${new Date().getFullYear()} EstimateFlow. All rights reserved.</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</body>
|
||||||
|
</html>`;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the email template from the database.
|
* Gets the email template from the database.
|
||||||
|
* If no template is found, it creates and returns a default one.
|
||||||
*/
|
*/
|
||||||
export async function getEmailTemplate(): Promise<{ subject: string; body: string } | null> {
|
export async function getEmailTemplate(): Promise<{ subject: string; body: string }> {
|
||||||
try {
|
try {
|
||||||
const stmt = db.prepare('SELECT subject, body FROM email_templates WHERE id = ?');
|
const stmt = db.prepare('SELECT subject, body FROM email_templates WHERE id = ?');
|
||||||
// We assume there is only one template with id = 1
|
let template = stmt.get(1) as { subject: string; body: string } | undefined;
|
||||||
const template = stmt.get(1) as { subject: string; body: string } | undefined;
|
|
||||||
if (!template) {
|
if (!template) {
|
||||||
return null;
|
console.log('No template found, creating a default one.');
|
||||||
|
const insertStmt = db.prepare('INSERT INTO email_templates (id, subject, body) VALUES (?, ?, ?)');
|
||||||
|
insertStmt.run(1, defaultSubject, defaultBody);
|
||||||
|
template = { subject: defaultSubject, body: defaultBody };
|
||||||
|
revalidatePath('/admin/settings/email-templates');
|
||||||
}
|
}
|
||||||
|
|
||||||
return template;
|
return template;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to get email template:', error);
|
console.error('Failed to get or create email template:', error);
|
||||||
// Propagate the error to be handled by the caller
|
// Propagate the error to be handled by the caller
|
||||||
throw new Error('Database error while fetching email template.');
|
throw new Error('Database error while fetching or creating email template.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates or creates the email template in the database.
|
* Updates the email template in the database.
|
||||||
*/
|
*/
|
||||||
export async function updateEmailTemplate(data: { subject: string; body: string }): Promise<{ success: boolean; message: string }> {
|
export async function updateEmailTemplate(data: { subject: string; body: string }): Promise<{ success: boolean; message: string }> {
|
||||||
const validation = emailTemplateSchema.safeParse(data);
|
const validation = emailTemplateSchema.safeParse(data);
|
||||||
@@ -42,17 +92,11 @@ export async function updateEmailTemplate(data: { subject: string; body: string
|
|||||||
const { subject, body } = validation.data;
|
const { subject, body } = validation.data;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const checkStmt = db.prepare('SELECT id FROM email_templates WHERE id = 1');
|
|
||||||
const existing = checkStmt.get();
|
|
||||||
|
|
||||||
if (existing) {
|
|
||||||
// Update existing template
|
|
||||||
const stmt = db.prepare('UPDATE email_templates SET subject = ?, body = ? WHERE id = ?');
|
const stmt = db.prepare('UPDATE email_templates SET subject = ?, body = ? WHERE id = ?');
|
||||||
stmt.run(subject, body, 1);
|
const result = stmt.run(subject, body, 1);
|
||||||
} else {
|
|
||||||
// Insert new template
|
if (result.changes === 0) {
|
||||||
const stmt = db.prepare('INSERT INTO email_templates (id, subject, body) VALUES (?, ?, ?)');
|
return { success: false, message: 'Could not find the template to update. Please try reloading the page.' };
|
||||||
stmt.run(1, subject, body);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
revalidatePath('/admin/settings/email-templates');
|
revalidatePath('/admin/settings/email-templates');
|
||||||
|
|||||||
Reference in New Issue
Block a user