-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
minor changes and feat: create Post #15
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,151 @@ | ||
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) { | ||
const file = files[0]; | ||
if (file.type.startsWith("image/")) { | ||
setImage(file); | ||
setImgPreview(URL.createObjectURL(file)); | ||
} else { | ||
toast.error("Please select a valid image file."); | ||
} | ||
} | ||
}; | ||
|
||
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} | ||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
DOM text reinterpreted as HTML Medium
Copilot Autofix AI about 1 month ago
To fix the problem, we need to ensure that the
imgPreview
value is safe before using it as thesrc
attribute of animg
element. One way to do this is to validate the file type and ensure it is an image before creating the object URL. Additionally, we can use a library likeDOMPurify
to sanitize the URL if necessary.src
attribute of theimg
element.