-
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
1 parent
af0daf5
commit ff97dc4
Showing
5 changed files
with
68 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
VITE_SERVER_URL="http://localhost:3001" |
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 |
---|---|---|
@@ -1,13 +1,27 @@ | ||
import { useEffect, useState } from "react" | ||
import { useParams } from "react-router" | ||
|
||
export default function Blog(props: any) { | ||
export default function Blog() { | ||
const { blogId } = useParams() | ||
const [blog, setBlog] = useState<{ id: string, title: string, blogContent: string }>(); | ||
const [loading, setLoading] = useState(true); | ||
|
||
useEffect(() => { | ||
fetch(import.meta.env.VITE_SERVER_URL + "/api/posts/" + blogId) | ||
.then(res => res.json()) | ||
.then(body => setBlog({ id: body.data.id, title: body.data.title, blogContent: body.data.blogContent })) | ||
.catch(err => console.log("Error while retreiving blog", err)) | ||
.finally(() => setLoading(false)); | ||
}, []); | ||
|
||
|
||
return <div className="flex flex-col gap-4 max-w-[700px] mx-auto mt-16 items-center"> | ||
{blogId && !props.blogs[blogId] && <div>There is No such Blog!!!</div>} | ||
{loading && <div className="text-4xl text-gray-500">Loading...</div>} | ||
|
||
{!loading && blogId && !blog && <div>There is No such Blog!!!</div>} | ||
|
||
<div className="font-bold text-start text-4xl p-4 rounded-lg w-full" >{blogId && props.blogs[blogId] && props.blogs[blogId].title}</div> | ||
<div className="font-bold text-start text-4xl p-4 rounded-lg w-full" >{blogId && blog && blog.title}</div> | ||
|
||
<div className="text-start text-xl p-2 rounded-lg w-full break-words" >{blogId && props.blogs[blogId] && props.blogs[blogId].blog}</div> | ||
<div className="text-start text-xl p-2 rounded-lg w-full break-words" >{blogId && blog && blog.blogContent}</div> | ||
</div > | ||
} |
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 |
---|---|---|
@@ -1,28 +1,43 @@ | ||
import { useState } from "react" | ||
import { NavigateFunction, useNavigate } from "react-router"; | ||
|
||
function handleBlogPublish(title: string, blog: string, setBlogs: any) { | ||
setBlogs((prev: any) => { return [...prev, { title, blog }] }) | ||
async function handleBlogPublish(title: string, blog: string, navigate: NavigateFunction) { | ||
await fetch(import.meta.env.VITE_SERVER_URL+"/api/posts", { | ||
method: "POST", | ||
body: JSON.stringify({ title: title, blogContent: blog }), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}) | ||
navigate("/"); | ||
} | ||
|
||
export default function WriteBlog(props: any) { | ||
export default function WriteBlog() { | ||
const navigate = useNavigate(); | ||
const [title, setTitle] = useState("") | ||
const [blog, setBlog] = useState("") | ||
|
||
return <div className="flex flex-col gap-4 max-w-[700px] mx-auto mt-16 items-center"> | ||
<div | ||
className="title-input font-bold text-start text-4xl p-4 bg-gray-700 rounded-lg w-full" contentEditable={true} | ||
onInput={ | ||
(e: any) => { | ||
setTitle(e.target.textContent); | ||
(e: React.FormEvent<HTMLDivElement>) => { | ||
const target = e.target as HTMLDivElement; | ||
if (target.textContent) { | ||
setTitle(target.textContent); | ||
} | ||
}}></div> | ||
|
||
<div | ||
className="paragraph-input text-start text-xl p-2 bg-gray-700 rounded-lg min-h-[400px] w-full" contentEditable={true} | ||
onInput={ | ||
(e: any) => { | ||
setBlog(e.target.textContent); | ||
(e: React.FormEvent<HTMLDivElement>) => { | ||
const target = e.target as HTMLDivElement; | ||
if (target.textContent) { | ||
setBlog(target.textContent); | ||
} | ||
}}></div> | ||
|
||
<button className="bg-blue-900 w-[200px] self-start" onClick={() => handleBlogPublish(title, blog, props.setBlogs)}>Publish</button> | ||
<button className="bg-blue-900 w-[200px] self-start" onClick={() => handleBlogPublish(title, blog, navigate)}>Publish</button> | ||
</div > | ||
} |