xFazz

CreatePost

Jul 28th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. import { useState } from 'react';
  2. import { supabase } from '../client';
  3. import { useNavigate } from 'react-router-dom';
  4. import './CreatePost.css';
  5.  
  6. const CreatePost = () => {
  7. const navigate = useNavigate();
  8. const [post, setPost] = useState({ title: "", author: "", description: "" });
  9.  
  10. const handleChange = (event) => {
  11. const { name, value } = event.target;
  12. setPost(prev => ({
  13. ...prev,
  14. [name]: value,
  15. }));
  16. }
  17.  
  18. const createPost = async (event) => {
  19. event.preventDefault();
  20.  
  21. try {
  22. await supabase
  23. .from('Posts')
  24. .insert({ title: post.title, author: post.author, description: post.description })
  25. .select();
  26.  
  27. navigate('/'); // Redirect after post creation
  28. } catch (error) {
  29. console.error("Error creating post: ", error);
  30. }
  31. }
  32.  
  33. return (
  34. <div>
  35. <form onSubmit={createPost}>
  36. <label htmlFor="title">Title</label> <br />
  37. <input
  38. type="text"
  39. id="title"
  40. name="title"
  41. value={post.title}
  42. onChange={handleChange}
  43. /><br />
  44. <br />
  45.  
  46. <label htmlFor="author">Author</label><br />
  47. <input
  48. type="text"
  49. id="author"
  50. name="author"
  51. value={post.author}
  52. onChange={handleChange}
  53. /><br />
  54. <br />
  55.  
  56. <label htmlFor="description">Description</label><br />
  57. <textarea
  58. rows="5"
  59. cols="50"
  60. id="description"
  61. name="description"
  62. value={post.description}
  63. onChange={handleChange}
  64. />
  65. <br />
  66. <input type="submit" value="Submit" />
  67. </form>
  68. </div>
  69. );
  70. }
  71.  
  72. export default CreatePost;
  73.  
Advertisement
Add Comment
Please, Sign In to add comment