-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
359 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useState } from "react"; | ||
import { createPost } from "@/services/postsService"; | ||
|
||
export const useCreatePost = () => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
const handleCreatePost = async (postData: any) => { | ||
|
||
try { | ||
setIsLoading(true); | ||
setError(null); | ||
const data = await createPost(postData); | ||
return data; | ||
} catch (err: any) { | ||
setError(err.response?.data?.message || "Failed to create post."); | ||
throw err; | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
return { handleCreatePost, isLoading, error }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { useState, useRef } from "react"; | ||
import { Link } from "react-router-dom"; | ||
import { MdCloudUpload } from "react-icons/md"; | ||
import JoditEditor from "jodit-react"; | ||
import { toast } from "sonner"; | ||
import { MainLayout } from "@/layout/MainLayout"; | ||
import { useAuthStore } from "@/store/authStore"; | ||
import { useCreatePost } from "@/hooks/useCreatePost"; | ||
|
||
const CreatePost: React.FC = () => { | ||
const { store } = useAuthStore(); | ||
const editor = useRef<JoditEditor | null>(null); | ||
|
||
// State variables | ||
const [title, setTitle] = useState<string>(""); | ||
const [description, setDescription] = useState<string>(""); | ||
const [image, setImage] = useState<File | null>(null); | ||
const [imgPreview, setImgPreview] = useState<string>(""); | ||
|
||
const { handleCreatePost, isLoading } = useCreatePost(); | ||
|
||
const handleImageChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const { files } = e.target; | ||
if (files && files.length > 0) { | ||
setImage(files[0]); | ||
setImgPreview(URL.createObjectURL(files[0])); | ||
} | ||
}; | ||
|
||
const addPost = async (e: React.FormEvent) => { | ||
e.preventDefault(); | ||
if (!image) { | ||
toast.error("Please select an image."); | ||
return; | ||
} | ||
|
||
const formData = new FormData(); | ||
formData.append("title", title); | ||
formData.append("description", description); | ||
formData.append("image", image); | ||
|
||
try { | ||
const response = await handleCreatePost(formData); | ||
toast.success(response.message); | ||
} catch (error) { | ||
// Error handling is already managed in the hook, so no additional logic is needed here. | ||
} | ||
}; | ||
|
||
return ( | ||
<MainLayout> | ||
<div className="bg-white rounded-md"> | ||
<div className="flex justify-between p-4"> | ||
<h2 className="text-xl font-medium">Add Post</h2> | ||
<Link | ||
className="px-3 py-[6px] bg-purple-500 rounded-sm text-white hover:bg-purple-600" | ||
to="/dashboard/posts" | ||
> | ||
Posts | ||
</Link> | ||
</div> | ||
|
||
<div className="p-4"> | ||
<form onSubmit={addPost}> | ||
{/* Title Input */} | ||
<div className="flex flex-col gap-y-2 mb-6"> | ||
<label | ||
className="text-md font-medium text-gray-600" | ||
htmlFor="title" | ||
> | ||
Title | ||
</label> | ||
<input | ||
type="text" | ||
id="title" | ||
name="title" | ||
value={title} | ||
onChange={(e) => setTitle(e.target.value)} | ||
placeholder="Enter title" | ||
required | ||
className="px-3 py-2 rounded-md border border-gray-300 focus:border-green-500" | ||
/> | ||
</div> | ||
|
||
{/* Image Upload */} | ||
<div className="mb-6"> | ||
<label | ||
htmlFor="image" | ||
className="w-full h-[240px] flex items-center justify-center border-2 border-dashed rounded cursor-pointer text-gray-600" | ||
> | ||
{imgPreview ? ( | ||
<img | ||
src={imgPreview} | ||
Check warning Code scanning / CodeQL DOM text reinterpreted as HTML Medium DOM text Error loading related location Loading |
||
alt="Preview" | ||
className="w-full h-full object-cover" | ||
/> | ||
) : ( | ||
<div className="flex flex-col items-center"> | ||
<MdCloudUpload className="text-2xl" /> | ||
<p>Select Image</p> | ||
</div> | ||
)} | ||
</label> | ||
<input | ||
type="file" | ||
id="image" | ||
onChange={handleImageChange} | ||
className="hidden" | ||
required | ||
/> | ||
</div> | ||
|
||
{/* Description */} | ||
<div className="flex flex-col gap-y-2 mb-6"> | ||
<label | ||
className="text-md font-medium text-gray-600" | ||
htmlFor="description" | ||
> | ||
Description | ||
</label> | ||
<JoditEditor | ||
ref={editor} | ||
value={description} | ||
onBlur={(value) => setDescription(value)} | ||
onChange={() => {}} | ||
/> | ||
</div> | ||
|
||
{/* Submit Button */} | ||
<button | ||
type="submit" | ||
disabled={isLoading} | ||
className={`px-3 py-[6px] rounded-sm text-white ${ | ||
isLoading ? "bg-gray-500" : "bg-purple-500 hover:bg-purple-600" | ||
}`} | ||
> | ||
{isLoading ? "Loading..." : "Add Post"} | ||
</button> | ||
</form> | ||
</div> | ||
</div> | ||
</MainLayout> | ||
); | ||
}; | ||
|
||
export default CreatePost; |
Oops, something went wrong.