Add title field to EmailTemplate model and update UI
This commit is contained in:
@ -6,6 +6,7 @@ import Link from 'next/link';
|
|||||||
|
|
||||||
interface EmailTemplate {
|
interface EmailTemplate {
|
||||||
id: string;
|
id: string;
|
||||||
|
title: string;
|
||||||
content: string;
|
content: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
modifiedAt: string;
|
modifiedAt: string;
|
||||||
@ -18,6 +19,7 @@ interface EditEmailTemplateProps {
|
|||||||
export default function EditEmailTemplate({ id }: EditEmailTemplateProps) {
|
export default function EditEmailTemplate({ id }: EditEmailTemplateProps) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [formData, setFormData] = useState({
|
const [formData, setFormData] = useState({
|
||||||
|
title: '',
|
||||||
content: '',
|
content: '',
|
||||||
});
|
});
|
||||||
const [isLoading, setIsLoading] = useState(!!id); // Only loading if editing
|
const [isLoading, setIsLoading] = useState(!!id); // Only loading if editing
|
||||||
@ -42,6 +44,7 @@ export default function EditEmailTemplate({ id }: EditEmailTemplateProps) {
|
|||||||
const template: EmailTemplate = await response.json();
|
const template: EmailTemplate = await response.json();
|
||||||
|
|
||||||
setFormData({
|
setFormData({
|
||||||
|
title: template.title,
|
||||||
content: template.content,
|
content: template.content,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -55,7 +58,7 @@ export default function EditEmailTemplate({ id }: EditEmailTemplateProps) {
|
|||||||
fetchEmailTemplate();
|
fetchEmailTemplate();
|
||||||
}, [id]);
|
}, [id]);
|
||||||
|
|
||||||
const handleChange = (e: ChangeEvent<HTMLTextAreaElement>) => {
|
const handleChange = (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
||||||
const { name, value } = e.target;
|
const { name, value } = e.target;
|
||||||
setFormData((prev) => ({ ...prev, [name]: value }));
|
setFormData((prev) => ({ ...prev, [name]: value }));
|
||||||
};
|
};
|
||||||
@ -67,8 +70,8 @@ export default function EditEmailTemplate({ id }: EditEmailTemplateProps) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Validate form
|
// Validate form
|
||||||
if (!formData.content) {
|
if (!formData.title || !formData.content) {
|
||||||
throw new Error('Content is required');
|
throw new Error('Title and content are required');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine if creating or updating
|
// Determine if creating or updating
|
||||||
@ -132,6 +135,22 @@ export default function EditEmailTemplate({ id }: EditEmailTemplateProps) {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
<form onSubmit={handleSubmit} className="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
|
<form onSubmit={handleSubmit} className="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
|
||||||
|
<div className="mb-4">
|
||||||
|
<label className="block text-gray-700 text-sm font-bold mb-2" htmlFor="title">
|
||||||
|
Title
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
|
||||||
|
id="title"
|
||||||
|
type="text"
|
||||||
|
name="title"
|
||||||
|
value={formData.title}
|
||||||
|
onChange={handleChange}
|
||||||
|
placeholder="Email template title"
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="mb-6">
|
<div className="mb-6">
|
||||||
<label className="block text-gray-700 text-sm font-bold mb-2" htmlFor="content">
|
<label className="block text-gray-700 text-sm font-bold mb-2" htmlFor="content">
|
||||||
Content
|
Content
|
||||||
|
|||||||
@ -83,6 +83,12 @@ export default async function EmailTemplateDetailPage(props: { params: Promise<{
|
|||||||
</dd>
|
</dd>
|
||||||
</div>
|
</div>
|
||||||
<div className="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
|
<div className="bg-white px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
|
||||||
|
<dt className="text-sm font-medium text-gray-500">Title</dt>
|
||||||
|
<dd className="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
|
||||||
|
{emailTemplate.title}
|
||||||
|
</dd>
|
||||||
|
</div>
|
||||||
|
<div className="bg-gray-50 px-4 py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
|
||||||
<dt className="text-sm font-medium text-gray-500">Content</dt>
|
<dt className="text-sm font-medium text-gray-500">Content</dt>
|
||||||
<dd className="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2 whitespace-pre-wrap">
|
<dd className="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2 whitespace-pre-wrap">
|
||||||
{emailTemplate.content}
|
{emailTemplate.content}
|
||||||
|
|||||||
@ -40,7 +40,7 @@ export default async function AdminEmailTemplates() {
|
|||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
||||||
>
|
>
|
||||||
Content Preview
|
Title
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
@ -76,9 +76,7 @@ export default async function AdminEmailTemplates() {
|
|||||||
href={`/admin/email-templates/detail/${template.id}`}
|
href={`/admin/email-templates/detail/${template.id}`}
|
||||||
className="text-indigo-600 hover:text-indigo-900"
|
className="text-indigo-600 hover:text-indigo-900"
|
||||||
>
|
>
|
||||||
{template.content.length > 100
|
{template.title}
|
||||||
? template.content.substring(0, 100) + '...'
|
|
||||||
: template.content}
|
|
||||||
</Link>
|
</Link>
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
||||||
|
|||||||
@ -55,17 +55,18 @@ export async function PUT(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const data = await request.json();
|
const data = await request.json();
|
||||||
const { content } = data;
|
const { title, content } = data;
|
||||||
|
|
||||||
// Validate required fields
|
// Validate required fields
|
||||||
if (!content) {
|
if (!title || !content) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Content is required' },
|
{ error: 'Title and content are required' },
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update email template fields
|
// Update email template fields
|
||||||
|
emailTemplate.title = title;
|
||||||
emailTemplate.content = content;
|
emailTemplate.content = content;
|
||||||
|
|
||||||
// Save the updated email template
|
// Save the updated email template
|
||||||
|
|||||||
@ -28,18 +28,19 @@ export async function POST(request: NextRequest) {
|
|||||||
const emailTemplateRepository = dataSource.getRepository(EmailTemplate);
|
const emailTemplateRepository = dataSource.getRepository(EmailTemplate);
|
||||||
|
|
||||||
const data = await request.json();
|
const data = await request.json();
|
||||||
const { content } = data;
|
const { title, content } = data;
|
||||||
|
|
||||||
// Validate required fields
|
// Validate required fields
|
||||||
if (!content) {
|
if (!title || !content) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: 'Content is required' },
|
{ error: 'Title and content are required' },
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and save the new email template
|
// Create and save the new email template
|
||||||
const emailTemplate = new EmailTemplate();
|
const emailTemplate = new EmailTemplate();
|
||||||
|
emailTemplate.title = title;
|
||||||
emailTemplate.content = content;
|
emailTemplate.content = content;
|
||||||
|
|
||||||
const savedEmailTemplate = await emailTemplateRepository.save(emailTemplate);
|
const savedEmailTemplate = await emailTemplateRepository.save(emailTemplate);
|
||||||
|
|||||||
@ -5,6 +5,9 @@ export class EmailTemplate {
|
|||||||
@PrimaryGeneratedColumn('uuid')
|
@PrimaryGeneratedColumn('uuid')
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
title: string;
|
||||||
|
|
||||||
@Column('text')
|
@Column('text')
|
||||||
content: string;
|
content: string;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user