Skip to content

Commit d647b5d

Browse files
committed
fixed edit post
1 parent a691057 commit d647b5d

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

public/js/add-post.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
async function newFormHandler(event) {
22
event.preventDefault();
33

4-
const title = document.querySelector('input[name="post-title"]').value;
4+
const title = document.querySelector('input[name="title"]').value;
55
const post_content = document.querySelector('input[name="post_content"]').value;
66

77
const response = await fetch(`/api/posts`, {

public/js/edit-post.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
async function editFormHandler(event) {
2+
event.preventDefault();
3+
4+
const title = document.querySelector('input[name="title"]').value.trim();
5+
const post_content = document.querySelector('input[name="post_content"]').value.trim();
6+
console.log(title);
7+
console.log(content);
8+
9+
const id = window.location.toString().split('/')[
10+
window.location.toString().split('/').length - 1
11+
12+
];
13+
14+
const response = await fetch(`/api/posts/${id}`, {
15+
method: 'PUT',
16+
body: JSON.stringify({
17+
post_id: id,
18+
title,
19+
post_content
20+
}),
21+
22+
headers: {
23+
'Content-Type': 'application/json'
24+
}
25+
});
26+
27+
28+
if (response.ok) {
29+
document.location.replace('/dashboard/');
30+
} else {
31+
alert(response.statusText);
32+
}
33+
34+
}
35+
36+
document.querySelector('.edit-post-form').addEventListener('submit', editFormHandler);

views/edit-post.handlebars

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<div class="form-group col-11">
77

88
<input
9-
name="post-title"
9+
name="title"
1010
type="text"
1111
value="{{post.title}}"
1212
class="form-input"

0 commit comments

Comments
 (0)