|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | + |
| 3 | +interface Post { |
| 4 | + title: string; |
| 5 | + content: string; |
| 6 | + category: string; |
| 7 | + date: string; |
| 8 | +} |
| 9 | + |
| 10 | +const Stories = () => { |
| 11 | + const [posts, setPosts] = useState<Post[]>([]); |
| 12 | + const [title, setTitle] = useState(''); |
| 13 | + const [content, setContent] = useState(''); |
| 14 | + const [category, setCategory] = useState(''); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + // Fetch posts from the backend API |
| 18 | + const fetchPosts = async () => { |
| 19 | + try { |
| 20 | + const response = await fetch( |
| 21 | + 'http://localhost:5000/api/stories/getposts', |
| 22 | + ); |
| 23 | + if (response.ok) { |
| 24 | + const data = await response.json(); |
| 25 | + setPosts(data); |
| 26 | + } else { |
| 27 | + console.error('Failed to fetch posts'); |
| 28 | + } |
| 29 | + } catch (error) { |
| 30 | + console.error('Error fetching posts:', error); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + fetchPosts(); |
| 35 | + }, []); |
| 36 | + |
| 37 | + const handleSubmit = async (e: React.FormEvent) => { |
| 38 | + e.preventDefault(); |
| 39 | + |
| 40 | + if (title && content && category) { |
| 41 | + const newPost = { |
| 42 | + title, |
| 43 | + content, |
| 44 | + category, |
| 45 | + date: new Date().toISOString(), |
| 46 | + }; |
| 47 | + |
| 48 | + try { |
| 49 | + const response = await fetch( |
| 50 | + 'http://localhost:5000/api/stories/saveposts', |
| 51 | + { |
| 52 | + method: 'POST', |
| 53 | + headers: { |
| 54 | + 'Content-Type': 'application/json', |
| 55 | + }, |
| 56 | + body: JSON.stringify(newPost), |
| 57 | + }, |
| 58 | + ); |
| 59 | + |
| 60 | + if (response.ok) { |
| 61 | + const savedPost = await response.json(); |
| 62 | + setPosts([savedPost, ...posts]); // Add the new post to state |
| 63 | + } else { |
| 64 | + console.error('Failed to save the post'); |
| 65 | + } |
| 66 | + } catch (error) { |
| 67 | + console.error('Error saving the post:', error); |
| 68 | + } |
| 69 | + |
| 70 | + // Clear form fields |
| 71 | + setTitle(''); |
| 72 | + setContent(''); |
| 73 | + setCategory(''); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <> |
| 79 | + <h1 className='text-3xl font-bold text-center mb-5 text-gray-100 mt-20'> |
| 80 | + Real Stories, Real Advice: Share Your Experience |
| 81 | + </h1> |
| 82 | + |
| 83 | + <div className='flex flex-col lg:flex-row items-start gap-8 px-6 lg:px-20 mb-14'> |
| 84 | + {/* Left side - Posts */} |
| 85 | + <div className='flex-1 space-y-6'> |
| 86 | + {posts.length === 0 ? ( |
| 87 | + <p className='text-gray-400 text-center'> |
| 88 | + No posts yet. Share your experience! |
| 89 | + </p> |
| 90 | + ) : ( |
| 91 | + posts.map((post, index) => ( |
| 92 | + <div |
| 93 | + key={index} |
| 94 | + className='bg-gray-800 text-white shadow-lg rounded-xl p-8 border border-gray-700 hover:shadow-xl transition-all duration-300 ease-in-out' |
| 95 | + > |
| 96 | + <h3 className='text-2xl font-bold text-gray-100 mb-2'> |
| 97 | + {post.title} |
| 98 | + </h3> |
| 99 | + <p className='text-sm text-indigo-400 font-medium mb-6'> |
| 100 | + {post.category} |
| 101 | + </p> |
| 102 | + <p className='text-gray-300 leading-relaxed mb-4'> |
| 103 | + {post.content} |
| 104 | + </p> |
| 105 | + <div className='flex items-center justify-between'> |
| 106 | + <p className='text-xs text-gray-500'> |
| 107 | + {new Date(post.date).toLocaleDateString()} |
| 108 | + </p> |
| 109 | + <button className='text-indigo-500 text-sm font-medium border border-indigo-100 rounded-full px-4 py-1 hover:bg-indigo-700 transition-colors'> |
| 110 | + Read More |
| 111 | + </button> |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + )) |
| 115 | + )} |
| 116 | + </div> |
| 117 | + |
| 118 | + {/* Right side - Form */} |
| 119 | + <div className='w-full lg:w-1/3 bg-gray-800 p-6 rounded-lg shadow-md'> |
| 120 | + <form className='space-y-4'> |
| 121 | + <input |
| 122 | + type='text' |
| 123 | + placeholder='Title of your story' |
| 124 | + value={title} |
| 125 | + onChange={e => setTitle(e.target.value)} |
| 126 | + className='w-full p-3 rounded-md border border-gray-600 bg-gray-700 text-white focus:outline-none focus:border-blue-500' |
| 127 | + /> |
| 128 | + <textarea |
| 129 | + placeholder='Write about your story...' |
| 130 | + value={content} |
| 131 | + onChange={e => setContent(e.target.value)} |
| 132 | + className='w-full p-3 h-32 rounded-md border border-gray-600 bg-gray-700 text-white focus:outline-none focus:border-blue-500' |
| 133 | + ></textarea> |
| 134 | + <select |
| 135 | + value={category} |
| 136 | + onChange={e => setCategory(e.target.value)} |
| 137 | + className='block w-full px-4 py-2 bg-gray-700 text-white border border-gray-600 rounded-md shadow-sm focus:border-indigo-500 focus:ring-indigo-500' |
| 138 | + > |
| 139 | + <option value='' disabled> |
| 140 | + Select Category |
| 141 | + </option> |
| 142 | + |
| 143 | + <optgroup label='GlassyUI-Components'> |
| 144 | + <option value='GlassyUI Introduction'> |
| 145 | + GlassyUI Introduction |
| 146 | + </option> |
| 147 | + <option value='Customizing GlassyUI Components'> |
| 148 | + Customizing GlassyUI Components |
| 149 | + </option> |
| 150 | + <option value='Advanced GlassyUI Techniques'> |
| 151 | + Advanced GlassyUI Techniques |
| 152 | + </option> |
| 153 | + <option value='GlassyUI Best Practices'> |
| 154 | + GlassyUI Best Practices |
| 155 | + </option> |
| 156 | + <option value='Contributing to GlassyUI'> |
| 157 | + Contributing to GlassyUI |
| 158 | + </option> |
| 159 | + <option value='React and GlassyUI Integration'> |
| 160 | + React and GlassyUI Integration |
| 161 | + </option> |
| 162 | + <option value='GlassyUI in Real Projects'> |
| 163 | + GlassyUI in Real Projects |
| 164 | + </option> |
| 165 | + <option value='GlassyUI Updates'>GlassyUI Updates</option> |
| 166 | + </optgroup> |
| 167 | + </select> |
| 168 | + |
| 169 | + <button |
| 170 | + onClick={handleSubmit} |
| 171 | + className='w-full bg-blue-500 hover:bg-blue-600 hover:text-white text-white font-semibold py-2 rounded-md focus:outline-none' |
| 172 | + > |
| 173 | + Post Experience |
| 174 | + </button> |
| 175 | + </form> |
| 176 | + </div> |
| 177 | + </div> |
| 178 | + </> |
| 179 | + ); |
| 180 | +}; |
| 181 | + |
| 182 | +export default Stories; |
0 commit comments