11import React , { useState } from 'react' ;
2- import { Container , Button , Typography , Box } from '@mui/material' ;
3- import { useNavigate } from 'react-router-dom' ;
4- import { config } from '../config' ;
2+ import {
3+ Button ,
4+ Typography ,
5+ Container ,
6+ Modal ,
7+ Snackbar ,
8+ Alert ,
9+ } from '@mui/material' ;
510import FroalaEditor from 'react-froala-wysiwyg' ;
611import 'froala-editor/css/froala_style.min.css' ;
712import 'froala-editor/css/froala_editor.pkgd.min.css' ;
813import 'froala-editor/js/plugins/image.min.js' ;
914import api from "../serverApi.ts" ;
1015import { getUserAuth } from '../handlers/userAuth.ts' ;
16+ import { config } from '../config.ts' ;
1117
12- const NewPost : React . FC = ( ) => {
18+ type Props = {
19+ open : boolean ;
20+ onClose : ( ) => void ;
21+ onPostCreated ?: ( ) => void ;
22+ } ;
23+
24+ const NewPostModal : React . FC < Props > = ( { open, onClose, onPostCreated } ) => {
1325 const [ title , setTitle ] = useState ( '' ) ;
1426 const [ content , setContent ] = useState ( '' ) ;
15- const navigate = useNavigate ( ) ;
1627 const auth = getUserAuth ( ) ;
28+ const [ error , setError ] = useState < string | null > ( null ) ;
1729
1830 const handleSubmit = async ( e : React . FormEvent ) => {
1931 e . preventDefault ( ) ;
@@ -25,25 +37,48 @@ const NewPost: React.FC = () => {
2537 content,
2638 } ) ;
2739
28- navigate ( '/feed' ) ; // Redirect to feed after successful post creation
40+ onClose ( ) ;
41+ onPostCreated ?.( ) ; // Refresh feed if needed
2942 } catch ( error ) {
30- console . error ( 'Error creating post:' , error ) ;
43+ setError ( 'Error creating post: ' + error ) ;
3144 }
3245 } ;
3346
3447 return (
35- < Container component = "main" maxWidth = "md" >
36- < Box sx = { { display : 'flex' , flexDirection : 'column' , alignItems : 'center' , mt : 8 } } >
37- < Typography component = "h1" variant = "h4" gutterBottom >
48+ < Modal open = { open } onClose = { onClose } >
49+ < Container
50+ maxWidth = "md"
51+ sx = { {
52+ mt : 10 ,
53+ backgroundColor : 'background.paper' ,
54+ borderRadius : 2 ,
55+ p : 4 ,
56+ width : '40%' ,
57+ overflowY : 'auto' ,
58+ height : '80vh' ,
59+ color : 'text.primary' ,
60+ } }
61+ >
62+ < Typography variant = "h5" gutterBottom color = "text.primary" >
3863 Create New Post
3964 </ Typography >
40- < form onSubmit = { handleSubmit } style = { { width : '90vh' , overflowY : 'scroll' , height : '60vh' , marginTop : '1rem' } } >
65+ < form onSubmit = { handleSubmit } >
4166 < input
4267 type = "text"
4368 placeholder = "Title"
4469 value = { title }
4570 onChange = { ( e ) => setTitle ( e . target . value ) }
46- style = { { width : '100%' , marginBottom : '1rem' , padding : '10px' , fontSize : '16px' } }
71+ style = { {
72+ width : '100%' ,
73+ marginBottom : '1rem' ,
74+ padding : '10px' ,
75+ fontSize : '16px' ,
76+ backgroundColor : 'transparent' ,
77+ color : 'inherit' ,
78+ border : '1px solid' ,
79+ borderColor : 'divider' ,
80+ borderRadius : '4px' ,
81+ } }
4782 required
4883 />
4984 < FroalaEditor
@@ -94,33 +129,37 @@ const NewPost: React.FC = () => {
94129 editor . image . insert ( imageUrl , null , null , editor . image . get ( ) ) ;
95130 } )
96131 . catch ( error => {
97- console . error ( 'Error uploading image:' , error ) ;
132+ setError ( 'Error uploading image: ' + error ) ;
98133 } ) ;
99134
100135 return false ; // Prevent default upload
101136 } ,
102137 'image.error' : function ( error : any , response : any ) {
103- console . error ( 'Image upload error:' , error , response ) ;
138+ setError ( 'Image upload error: ' + error + ', response: ' + response ) ;
104139 }
105140 }
106141 } }
107142 />
108- < Button type = "submit" fullWidth variant = "contained" color = "primary" sx = { { mt : 3 , mb : 2 } } >
143+ < Button type = "submit" fullWidth variant = "contained" color = "primary" sx = { { mt : 3 } } >
109144 Submit
110145 </ Button >
111- < Button
112- fullWidth
113- variant = "outlined"
114- color = "secondary"
115- onClick = { ( ) => navigate ( '/feed' ) }
116- sx = { { mt : 2 } }
117- >
118- Back to Feed
146+ < Button fullWidth onClick = { onClose } sx = { { mt : 1 } } >
147+ Cancel
119148 </ Button >
120149 </ form >
121- </ Box >
122- </ Container >
150+ < Snackbar
151+ open = { ! ! error }
152+ autoHideDuration = { 6000 }
153+ onClose = { ( ) => setError ( null ) }
154+ anchorOrigin = { { vertical : 'top' , horizontal : 'center' } }
155+ >
156+ < Alert severity = "error" onClose = { ( ) => setError ( null ) } >
157+ { error }
158+ </ Alert >
159+ </ Snackbar >
160+ </ Container >
161+ </ Modal >
123162 ) ;
124163} ;
125164
126- export default NewPost ;
165+ export default NewPostModal ;
0 commit comments