()
| 7 | import { useNavigate } from 'react-router-dom'; |
| 8 | |
| 9 | const CreateBlog = () => { |
| 10 | const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com"; |
| 11 | |
| 12 | const [formData, setFormData] = useState({ |
| 13 | id: '', |
| 14 | title: '', |
| 15 | author: '', |
| 16 | excerpt: '', |
| 17 | category: '', |
| 18 | }); |
| 19 | const navigate = useNavigate() |
| 20 | const [loading, setLoading] = useState(false); |
| 21 | const [errors, setErrors] = useState(''); |
| 22 | |
| 23 | const handleChange = (e) => { |
| 24 | const { name, value } = e.target; |
| 25 | setFormData({ |
| 26 | ...formData, |
| 27 | [name]: value |
| 28 | }); |
| 29 | }; |
| 30 | |
| 31 | const handleContentChange = (value) => { |
| 32 | setFormData({ |
| 33 | ...formData, |
| 34 | content: value |
| 35 | }); |
| 36 | }; |
| 37 | |
| 38 | const handleSubmit = async (e) => { |
| 39 | e.preventDefault(); |
| 40 | setLoading(true); // Start loading |
| 41 | |
| 42 | // Generate a unique ID for the blog post |
| 43 | const uniqueId = uuidv4(); |
| 44 | const blogPost = { ...formData, id: uniqueId }; // Add the ID to formData |
| 45 | console.log(blogPost) |
| 46 | try { |
| 47 | const response = await axios.post(`${VITE_SERVER_PORT}/api/blog/post-blog`, blogPost, { |
| 48 | headers: { 'Content-Type': 'application/json' } |
| 49 | }); |
| 50 | |
| 51 | console.log('Response:', response.data); |
| 52 | setFormData({ title: '', author: '', content: '', id: '', category: '' }); // Reset form data |
| 53 | alert('Blog created successfully.'); |
| 54 | navigate('/blog'); |
| 55 | |
| 56 | } catch (error) { |
| 57 | console.error('Error:', error); |
| 58 | setErrors('There was an issue with your submission. Please try again later.'); |
| 59 | } finally { |
| 60 | setLoading(false); // Stop loading |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | return ( |
| 65 | <> |
| 66 | <div className="create-blog-container"> |
nothing calls this directly
no outgoing calls
no test coverage detected