@hope-dies-here/next-blog-one
Version:
A plug-and-play Next.js blog feature for easy integration via npx.
33 lines (29 loc) • 996 B
JavaScript
'use client';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
export default function CreatePost() {
const [title, setTitle] = useState('');
const [content, setContent] = useState('');
const router = useRouter();
const handleSubmit = async (e) => {
e.preventDefault();
const response = await fetch('/api/blog-posts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title, content }),
});
if (response.ok) {
alert('Post created successfully!');
router.push('/blog');
} else {
alert('Error creating post');
}
};
return (
<form onSubmit={handleSubmit}>
<input value={title} onChange={(e) => setTitle(e.target.value)} placeholder="Title" />
<textarea value={content} onChange={(e) => setContent(e.target.value)} placeholder="Content" />
<button type="submit" className='bg-white p-2 text-black'>Create</button>
</form>
);
}