diff --git a/nextstep-frontend/src/App.tsx b/nextstep-frontend/src/App.tsx index f9f548c..9c411ef 100644 --- a/nextstep-frontend/src/App.tsx +++ b/nextstep-frontend/src/App.tsx @@ -7,7 +7,6 @@ import './App.css'; import Feed from './pages/Feed'; import Footer from './components/Footer'; import RequireAuth from './hoc/RequireAuth'; -import NewPost from './pages/NewPost'; import PostDetails from './pages/PostDetails'; import Chat from './pages/Chat'; import Resume from './pages/Resume'; @@ -74,7 +73,6 @@ const App: React.FC = () => { } /> } /> - } /> } /> } /> } /> diff --git a/nextstep-frontend/src/pages/NewPost.tsx b/nextstep-frontend/src/components/NewPost.tsx similarity index 62% rename from nextstep-frontend/src/pages/NewPost.tsx rename to nextstep-frontend/src/components/NewPost.tsx index f7208a3..15a0dbe 100644 --- a/nextstep-frontend/src/pages/NewPost.tsx +++ b/nextstep-frontend/src/components/NewPost.tsx @@ -1,19 +1,31 @@ import React, { useState } from 'react'; -import { Container, Button, Typography, Box } from '@mui/material'; -import { useNavigate } from 'react-router-dom'; -import { config } from '../config'; +import { + Button, + Typography, + Container, + Modal, + Snackbar, + Alert, +} from '@mui/material'; import FroalaEditor from 'react-froala-wysiwyg'; import 'froala-editor/css/froala_style.min.css'; import 'froala-editor/css/froala_editor.pkgd.min.css'; import 'froala-editor/js/plugins/image.min.js'; import api from "../serverApi.ts"; import { getUserAuth } from '../handlers/userAuth.ts'; +import { config } from '../config.ts'; -const NewPost: React.FC = () => { +type Props = { + open: boolean; + onClose: () => void; + onPostCreated?: () => void; +}; + +const NewPostModal: React.FC = ({ open, onClose, onPostCreated }) => { const [title, setTitle] = useState(''); const [content, setContent] = useState(''); - const navigate = useNavigate(); const auth = getUserAuth(); + const [error, setError] = useState(null); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); @@ -25,25 +37,48 @@ const NewPost: React.FC = () => { content, }); - navigate('/feed'); // Redirect to feed after successful post creation + onClose(); + onPostCreated?.(); // Refresh feed if needed } catch (error) { - console.error('Error creating post:', error); + setError('Error creating post: ' + error); } }; return ( - - - + + + Create New Post -
+ setTitle(e.target.value)} - style={{ width: '100%', marginBottom: '1rem', padding: '10px', fontSize: '16px' }} + style={{ + width: '100%', + marginBottom: '1rem', + padding: '10px', + fontSize: '16px', + backgroundColor: 'transparent', + color: 'inherit', + border: '1px solid', + borderColor: 'divider', + borderRadius: '4px', + }} required /> { editor.image.insert(imageUrl, null, null, editor.image.get()); }) .catch(error => { - console.error('Error uploading image:', error); + setError('Error uploading image: ' + error); }); return false; // Prevent default upload }, 'image.error': function (error: any, response: any) { - console.error('Image upload error:', error, response); + setError('Image upload error: ' + error + ', response: ' + response); } } }} /> - - -
-
+ setError(null)} + anchorOrigin={{ vertical: 'top', horizontal: 'center' }} + > + setError(null)}> + {error} + + + + ); }; -export default NewPost; \ No newline at end of file +export default NewPostModal; diff --git a/nextstep-frontend/src/pages/Feed.tsx b/nextstep-frontend/src/pages/Feed.tsx index 6ba9901..2c0b226 100644 --- a/nextstep-frontend/src/pages/Feed.tsx +++ b/nextstep-frontend/src/pages/Feed.tsx @@ -26,6 +26,7 @@ import { Post } from "../models/Post.tsx"; import api from "../serverApi.ts"; import {getUserAuth} from "../handlers/userAuth.ts"; import defaultProfileImage from '../../assets/defaultProfileImage.jpg'; // Import the default profile image +import NewPostModal from '../components/NewPost.tsx'; const Feed: React.FC = () => { @@ -42,10 +43,12 @@ const Feed: React.FC = () => { const [profileImages, setProfileImages] = useState<{ [key: string]: string }>({}); const [currentPage, setCurrentPage] = useState(1); const [totalPages, setTotalPages] = useState(1); + const [showNewPostModal, setShowNewPostModal] = useState(false); + const auth = getUserAuth(); const handleCreatePost = () => { - navigate('/new-post'); + setShowNewPostModal(true); }; const handleDeletePost = async () => { @@ -197,10 +200,46 @@ const Feed: React.FC = () => { - - + + + + What's on your mind{', ' + auth.username || 'User'}? + + + + + + + + + + { color="primary" /> } - label="Show My Posts" - /> - + label="Show only my posts" + /> + setShowNewPostModal(false)} + onPostCreated={() => loadPosts(currentPage)} + /> {isLoading ? ( @@ -220,11 +263,12 @@ const Feed: React.FC = () => { {error} ) : ( <> - + {posts.map((post) => ( { )} - + { totalPages > 0 && + }