an unexpected error ocurred when trying to save the template
This commit is contained in:
@@ -28,7 +28,7 @@ function EmailPreview({ template }: { template: EmailTemplate }) {
|
|||||||
<p class="text-2xl font-bold">[Ready-Made Hours]+ hours</p>
|
<p class="text-2xl font-bold">[Ready-Made Hours]+ hours</p>
|
||||||
</div>
|
</div>
|
||||||
</div>`
|
</div>`
|
||||||
).replace(/\n/g, '<br />');
|
).replace('[User Name]', 'Valued Customer').replace(/\n/g, '<br />');
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="w-full rounded-lg border bg-muted p-6">
|
<div className="w-full rounded-lg border bg-muted p-6">
|
||||||
@@ -43,6 +43,7 @@ function EmailPreview({ template }: { template: EmailTemplate }) {
|
|||||||
export default function EmailTemplatesPage() {
|
export default function EmailTemplatesPage() {
|
||||||
const [isEditing, setIsEditing] = useState(false);
|
const [isEditing, setIsEditing] = useState(false);
|
||||||
const [template, setTemplate] = useState<EmailTemplate>({ subject: '', body: '' });
|
const [template, setTemplate] = useState<EmailTemplate>({ subject: '', body: '' });
|
||||||
|
const [originalTemplate, setOriginalTemplate] = useState<EmailTemplate>({ subject: '', body: '' });
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
|
||||||
@@ -53,6 +54,7 @@ export default function EmailTemplatesPage() {
|
|||||||
const fetchedTemplate = await getEmailTemplate();
|
const fetchedTemplate = await getEmailTemplate();
|
||||||
if (fetchedTemplate) {
|
if (fetchedTemplate) {
|
||||||
setTemplate(fetchedTemplate);
|
setTemplate(fetchedTemplate);
|
||||||
|
setOriginalTemplate(fetchedTemplate);
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Could not find email template.');
|
throw new Error('Could not find email template.');
|
||||||
}
|
}
|
||||||
@@ -69,6 +71,11 @@ export default function EmailTemplatesPage() {
|
|||||||
fetchTemplate();
|
fetchTemplate();
|
||||||
}, [toast]);
|
}, [toast]);
|
||||||
|
|
||||||
|
const handleCancel = () => {
|
||||||
|
setTemplate(originalTemplate);
|
||||||
|
setIsEditing(false);
|
||||||
|
};
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
try {
|
try {
|
||||||
const result = await updateEmailTemplate(template);
|
const result = await updateEmailTemplate(template);
|
||||||
@@ -77,6 +84,7 @@ export default function EmailTemplatesPage() {
|
|||||||
title: 'Success',
|
title: 'Success',
|
||||||
description: 'Email template updated successfully.',
|
description: 'Email template updated successfully.',
|
||||||
});
|
});
|
||||||
|
setOriginalTemplate(template);
|
||||||
setIsEditing(false);
|
setIsEditing(false);
|
||||||
} else {
|
} else {
|
||||||
throw new Error(result.message);
|
throw new Error(result.message);
|
||||||
@@ -109,7 +117,7 @@ export default function EmailTemplatesPage() {
|
|||||||
<Button variant="outline" onClick={() => setIsEditing(true)}>Edit Template</Button>
|
<Button variant="outline" onClick={() => setIsEditing(true)}>Edit Template</Button>
|
||||||
) : (
|
) : (
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Button variant="outline" onClick={() => setIsEditing(false)}>Cancel</Button>
|
<Button variant="outline" onClick={handleCancel}>Cancel</Button>
|
||||||
<Button onClick={handleSave}>Save Changes</Button>
|
<Button onClick={handleSave}>Save Changes</Button>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ export async function getEmailTemplate(): Promise<{ subject: string; body: strin
|
|||||||
return template;
|
return template;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to get email template:', error);
|
console.error('Failed to get email template:', error);
|
||||||
return null;
|
// Propagate the error to be handled by the caller
|
||||||
|
throw new Error('Database error while fetching email template.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,12 +44,16 @@ export async function updateEmailTemplate(data: { subject: string; body: string
|
|||||||
try {
|
try {
|
||||||
const stmt = db.prepare('UPDATE email_templates SET subject = ?, body = ? WHERE id = ?');
|
const stmt = db.prepare('UPDATE email_templates SET subject = ?, body = ? WHERE id = ?');
|
||||||
// We assume we're updating the template with id = 1
|
// We assume we're updating the template with id = 1
|
||||||
stmt.run(subject, body, 1);
|
const info = stmt.run(subject, body, 1);
|
||||||
|
|
||||||
|
if (info.changes === 0) {
|
||||||
|
return { success: false, message: 'Could not find the template to update. No changes were made.' };
|
||||||
|
}
|
||||||
|
|
||||||
revalidatePath('/admin/settings/email-templates');
|
revalidatePath('/admin/settings/email-templates');
|
||||||
return { success: true, message: 'Email template updated successfully!' };
|
return { success: true, message: 'Email template updated successfully!' };
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
console.error('Failed to update email template:', error);
|
console.error('Failed to update email template:', error);
|
||||||
return { success: false, message: 'An unexpected error occurred.' };
|
return { success: false, message: error.message || 'An unexpected database error occurred.' };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user