File tree 3 files changed +38
-2
lines changed
3 files changed +38
-2
lines changed Original file line number Diff line number Diff line change 1
1
async function newFormHandler ( event ) {
2
2
event . preventDefault ( ) ;
3
3
4
- const title = document . querySelector ( 'input[name="post- title"]' ) . value ;
4
+ const title = document . querySelector ( 'input[name="title"]' ) . value ;
5
5
const post_content = document . querySelector ( 'input[name="post_content"]' ) . value ;
6
6
7
7
const response = await fetch ( `/api/posts` , {
Original file line number Diff line number Diff line change
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 ) ;
Original file line number Diff line number Diff line change 6
6
<div class =" form-group col-11" >
7
7
8
8
<input
9
- name =" post- title"
9
+ name =" title"
10
10
type =" text"
11
11
value =" {{ post.title }} "
12
12
class =" form-input"
You can’t perform that action at this time.
0 commit comments