|
1 |
| -/* eslint-disable no-unused-vars */ |
2 | 1 | /* eslint-disable react/prop-types */
|
3 |
| -import { useState } from 'react'; |
4 |
| -import EmojisComponent from '../EmojisComponent'; |
5 |
| -import PostOptions from '../Post/PostOptions'; |
6 |
| -import UploadImages from '../UploadImages'; |
7 |
| -import { toFormData } from '../../../Functions/toFormData'; |
8 |
| -import useFetch from '../../../CustomHooks/useFetch'; |
9 |
| -import CreatePostModalFooter from './CreatePostModalFooter'; |
10 |
| -import { useNavigate } from 'react-router-dom'; |
11 |
| -import { postOptions as options } from '../../../options' |
| 2 | +import { useState } from 'react'; |
12 | 3 | import { useDispatch } from 'react-redux';
|
| 4 | +import useFetch from '../../../CustomHooks/useFetch'; |
| 5 | +import { toFormData } from '../../../Functions/toFormData'; |
| 6 | +import { postOptions } from '../../../options'; |
13 | 7 | import { addPost } from '../../../store/posts/postsSlice';
|
| 8 | +import CreatePostForm from './CreatePostForm'; |
14 | 9 |
|
15 |
| - |
16 |
| -function CreatePostModal({ closeModal, editPost, post }) { |
17 |
| - const [uploadedImage, setUploadedImage] = useState(post&&post['image_path'] ?`http://localhost:8000/${post['image_path']}`: null); |
| 10 | +function CreatePostModal({ closeModal, editPost, post,editError,editLoading }) { |
| 11 | + const [uploadedImage, setUploadedImage] = useState(post && post['image_path'] ? `http://localhost:8000/${post['image_path']}` : null); |
18 | 12 | const [contentValue, setContentValue] = useState(post ? post.content : '');
|
| 13 | + const { fetchApi:createPost, error:createError,loading:createLoading } = useFetch('http://localhost:8000/api/posts/create', postOptions); |
19 | 14 | const dispatch=useDispatch()
|
20 |
| - const navigate = useNavigate(); |
21 |
| - const { fetchApi:createPost, loading, error } = useFetch('http://localhost:8000/api/posts/create', options); |
22 | 15 |
|
23 | 16 | const changeHandler = (e) => {
|
24 | 17 | setContentValue(e.target.value);
|
25 | 18 | };
|
26 | 19 |
|
27 |
| - const publishHandler = async (e) => { |
28 |
| - e.preventDefault(); |
29 |
| - console.log(editPost) |
30 |
| - let resData; |
| 20 | +const publishHandler = async (e) => { |
| 21 | + e.preventDefault(); |
| 22 | + let resData; |
| 23 | + if (editPost) { |
| 24 | + // If editing an existing post |
| 25 | + const formData = toFormData([ |
| 26 | + { name: 'content', value: contentValue }, |
| 27 | + uploadedImage ? { value: uploadedImage, name: 'file_path' } : null, |
| 28 | + ]); |
| 29 | + resData = await editPost(formData); |
| 30 | + } else { |
| 31 | + // If creating a new post |
| 32 | + const formData = toFormData([ |
| 33 | + { name: 'content', value: contentValue }, |
| 34 | + uploadedImage ? { value: uploadedImage, name: 'file_path' } : null, |
| 35 | + ]); |
| 36 | + resData = await createPost(formData); |
| 37 | + } |
| 38 | + if (resData.ok) { |
31 | 39 | if (editPost) {
|
32 |
| - |
33 |
| - resData = await editPost(toFormData([{ name: 'content', value: contentValue }, uploadedImage ? { value: uploadedImage, name: 'file_path' } : null])); |
| 40 | + dispatch(editPost(resData.data.post)); |
34 | 41 | } else {
|
35 |
| - resData = await createPost(toFormData([{ name: 'content', value: contentValue }, uploadedImage ? { value: uploadedImage, name: 'file_path' } : null])); |
36 |
| - } |
37 |
| - if (resData.ok) { |
38 |
| - dispatch(addPost(resData.data.post)) |
39 |
| - setUploadedImage(null); |
40 |
| - setContentValue(''); |
41 |
| - closeModal(); |
42 |
| - } |
43 |
| - }; |
| 42 | + dispatch(addPost(resData.data.post)); |
44 | 43 |
|
| 44 | + } |
| 45 | + setUploadedImage(null); |
| 46 | + setContentValue(''); |
| 47 | + closeModal(); |
| 48 | + } |
| 49 | +}; |
45 | 50 | return (
|
46 |
| - <form onSubmit={publishHandler} className="bg-[rgba(252,250,248,1)] shadow-[0px_4px_4px_0px_rgba(0,0,0,0.25)] py-8 px-12 rounded-3xl w-[700px] max-w-full m-2"> |
47 |
| - <h3 className="text-4xl font-bold text-center">Create Post</h3> |
48 |
| - <div className='w-full bg-white mx-auto my-2'> |
49 |
| - <textarea className='w-full min-h-[200px] resize-none outline-none p-2' onChange={changeHandler} value={contentValue} /> |
50 |
| - <div className='flex justify-between p-2'> |
51 |
| - <UploadImages setUploadedImage={setUploadedImage} /> |
52 |
| - <EmojisComponent /> |
53 |
| - </div> |
54 |
| - </div> |
55 |
| - {error ? <p className='text-red-800 font-medium'>*{error}</p> : ''} |
56 |
| - <PostOptions uploadedImage={uploadedImage} setUploadedImage={setUploadedImage} /> |
57 |
| - <CreatePostModalFooter closeModal={closeModal} loading={loading} /> |
58 |
| - </form> |
| 51 | + <CreatePostForm |
| 52 | + contentValue={contentValue} |
| 53 | + changeHandler={changeHandler} |
| 54 | + uploadedImage={uploadedImage} |
| 55 | + closeModal={closeModal} |
| 56 | + publishHandler={publishHandler} |
| 57 | + setUploadedImage={setUploadedImage} |
| 58 | + error={editError?editError:createError} |
| 59 | + loading={editLoading?editLoading:createLoading} |
| 60 | + |
| 61 | + /> |
59 | 62 | );
|
60 | 63 | }
|
61 | 64 |
|
|
0 commit comments