I'm trying to send update and delete request without actually refreshing my frontend. So, I know that e.preventDefault() will prevent my frontend to refresh whenever I send in fetch request. However, I have to refresh the page to actually change the frontend after the request.
Image after clicking update button on the white card
What I want is to show the change within the frontend without refreshing the page. Also, I would like to pre-fill those textareas with values that already exist (I don't want to use placeholder). Please help.
import { useState } from 'react';
export default function PostContainer({post}){
const [isShown, setIsShown] = useState(false);
const [title, setNewTitle] = useState("");
const [content, setNewContent] = useState("");
const [tags, setNewTags] = useState("");
const userId = post.user_id
console.log(post)
function handleDelete(e){
e.preventDefault();
fetch(`/post/${post.id}`,{
method: "DELETE",
headers: {
'Content-Type':'application/json'
},
body: JSON.stringify(post)
})
.then((r) => r.json())
.then(alert("Post Removed"))
}
// Display update component
const handleUpdate = (e) =>{
e.preventDefault();
setIsShown(current => !current)
}
// Send patch requests
function handleSubmit(e){
e.preventDefault();
const patchedPost = {
title,
content,
userId,
tags_attributes: tags.split(',').map((el) => { return {name: el} })
}
fetch(`/post/${post.id}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(patchedPost)
})
.then((r)=>r.json())
.then((x) => console.log(x))
}
return(
<div>
<div>
{isShown && (
<div>
<form className='update-form' onSubmit={handleSubmit}>
<label>Title</label><br/>
<textarea type='text' placeholder={post.title} value={title} onChange={(e)=>setNewTitle(e.target.value)}></textarea><br/>
<label>Content</label><br/>
<textarea type='text' placeholder={post.content} value={content} onChange={(e)=>setNewContent(e.target.value)}></textarea><br/>
<label>Tags</label><br/>
<textarea type='text' placeholder={post.tags.name} value={tags} onChange={(e)=>setNewTags(e.target.value)}></textarea><br/>
<button type='submit'>Update</button>
<button onClick={(e)=>{e.preventDefault(); setIsShown(false)}}>Cancel</button>
</form>
</div>
)}
</div><br/>
<div className='postContainer'>
<h1>{post.title}</h1>
<p className="users-posts-content">{post.content}</p>
<p>Tags: {post.tags.map((x)=> x.name).join(', ')}</p>
<button onClick={(e)=>handleUpdate(e)}>Update</button>
<button onClick={(e)=>handleDelete(e)}>Delete Post</button>
</div>
</div>
)
}