generated from S3ak/fed2-js2-ca-react-template-mo
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(posts, creating post, updating and deleting): fetch and edit p…
…osts fetching posts, editing, deleting and creating new post
- Loading branch information
Showing
10 changed files
with
275 additions
and
188 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,122 @@ | ||
import { useState } from "react"; | ||
import { API_URL } from "../../lib/constants"; | ||
import { PostShape } from "../../lib/types"; | ||
|
||
export default function ManipulatePost({ id = "no id" }) { | ||
const [isEditing, setIsEditing] = useState(false); | ||
const accessToken = localStorage.getItem("access_token"); | ||
|
||
async function deletePost(postId) { | ||
try { | ||
const response = await fetch(`${API_URL}/social/posts/${postId}`, { | ||
method: "DELETE", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}); | ||
|
||
const json = await response.json(); | ||
console.log("deleted post>", json); | ||
} catch (error) { | ||
console.warn("deletePost error", error); | ||
} finally { | ||
null; | ||
} | ||
} | ||
|
||
async function handleOnEdit() { | ||
setIsEditing(true); | ||
} | ||
async function handleOnDelete() { | ||
deletePost(id); | ||
} | ||
async function editPost(event) { | ||
event.preventDefault(); | ||
handleOnEdit(); | ||
|
||
const { body, title, postId } = event.target.elements; | ||
|
||
const formattedPostId = Number(postId.value); | ||
|
||
const payload = { | ||
title: title.value, | ||
body: body.value, | ||
}; | ||
|
||
try { | ||
const response = await fetch( | ||
`${API_URL}/social/posts/${formattedPostId}`, | ||
{ | ||
method: "PUT", | ||
headers: { | ||
"Content-Type": "application/json; charset=UTF-8", | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
body: JSON.stringify(payload), | ||
} | ||
); | ||
|
||
const json = await response.json(); | ||
|
||
console.warn("Success, updated post!", json); | ||
} catch (error) { | ||
console.warn("Couldn't update post", error); | ||
} finally { | ||
null; | ||
} | ||
} | ||
return ( | ||
<> | ||
{isEditing ? ( | ||
<> | ||
<form | ||
className="flex gap-2" | ||
onSubmit={(event) => editPost(event, setIsEditing)} | ||
> | ||
<input type="hidden" name="postId" id="postId" value={id} /> | ||
<input | ||
className="bg-blue-100 px-1 hover:bg-blue-200 rounded-sm" | ||
name="title" | ||
id="title" | ||
type="text" | ||
placeholder="Title" | ||
/> | ||
<input | ||
className="bg-blue-100 px-1 hover:bg-blue-200 rounded-sm" | ||
name="body" | ||
id="body" | ||
type="text" | ||
placeholder="Body" | ||
/> | ||
<input | ||
className="bg-blue-300 hover:bg-blue-400 px-3 py-1 cursor-pointer rounded-sm " | ||
type="submit" | ||
value="Update" | ||
/> | ||
<button | ||
className="bg-red-100 px-2 hover:bg-red-300 rounded-sm" | ||
onClick={() => setIsEditing(false)} | ||
> | ||
X | ||
</button> | ||
</form> | ||
</> | ||
) : ( | ||
<button | ||
className="bg-blue-300 hover:bg-blue-500 rounded-sm px-3 " | ||
onClick={editPost} | ||
> | ||
Edit | ||
</button> | ||
)} | ||
<button | ||
onClick={handleOnDelete} | ||
className="bg-red-200 rounded-sm self-end px-3 hover:bg-red-500" | ||
> | ||
Delete | ||
</button> | ||
</> | ||
); | ||
} | ||
ManipulatePost.propTypes = PostShape; |
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,52 @@ | ||
import { useEffect, useState } from "react"; | ||
import { API_URL } from "../lib/constants"; | ||
|
||
const initialPostState = { | ||
title: "No post found", | ||
body: "Nothing to see here", | ||
userId: null, | ||
id: null, | ||
}; | ||
|
||
export default function PostPage() { | ||
const [post, setPost] = useState(initialPostState); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [error, setError] = useState(null); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
try { | ||
const params = new URLSearchParams(window.location.search); | ||
const postId = params.get("id"); | ||
const accessToken = localStorage.getItem("access_token"); | ||
const resp = await fetch(`${API_URL}/social/posts/${postId}`, { | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
}); | ||
const data = await resp.json(); | ||
setPost(data); | ||
console.log(data); | ||
} catch (error) { | ||
setError(error); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
fetchData(); | ||
}, []); | ||
|
||
if (isLoading) return <h1>Loading...</h1>; | ||
if (error) return <h1>Something went wrong! {error?.message}</h1>; | ||
|
||
return ( | ||
<> | ||
<section> | ||
<h2>{post?.title}</h2> | ||
<p>{post?.body}</p> | ||
<img className="max-w-lg" src={post.media} alt="" /> | ||
</section> | ||
</> | ||
); | ||
} |
Oops, something went wrong.