11import React from 'react'
2- import { Link } from 'react-router-dom'
2+ import { Link , Redirect } from 'react-router-dom'
33import { StatusBox } from '../status-box/status-box.component'
44import './add-note.styles.css'
5+ import { Button } from '../button/Button.component'
56
67
78export class AddNote extends React . Component {
@@ -33,6 +34,27 @@ export class AddNote extends React.Component {
3334 this . setState ( { status : 'You must fill out both title and body.' } )
3435 } else {
3536 this . setState ( { status : '' } )
37+ fetch ( "/api/add_note" , {
38+ method : "post" ,
39+ headers : {
40+ 'Accept' : 'application/json' ,
41+ 'Content-Type' : 'application/json'
42+ } ,
43+
44+ //make sure to serialize your JSON body
45+ body : JSON . stringify ( {
46+ title : this . state . title ,
47+ body : this . state . body
48+ } )
49+ } )
50+ . then ( ( response ) => {
51+ //do something awesome that makes the world a better place
52+ console . log ( response )
53+ this . setState ( { status : 'Note Saved Successfully! Returning to home page.' } )
54+ setTimeout ( ( ) => {
55+ window . location . href = '/'
56+ } , 500 )
57+ } ) ;
3658 }
3759 }
3860
@@ -43,25 +65,11 @@ export class AddNote extends React.Component {
4365 < form id = "add-note" onSubmit = { this . handleSubmit } >
4466 < input id = "title" value = { this . state . title } type = 'text' placeholder = 'Enter a Title' onChange = { this . handleTitleChange } > </ input >
4567 < textarea onChange = { this . handleBodyChange } id = "body" placeholder = 'Enter the note' > </ textarea >
46- < button type = "submit" > ADDING NOTES COMING SOON! </ button >
68+ < Button type = "submit" > Add Note </ Button >
4769 </ form >
4870 < StatusBox status = { this . state . status } > </ StatusBox >
49- { /* <div className="status-box">{this.state.status}</div> */ }
5071 < Link to = '/' > Go Back</ Link >
5172 </ >
5273 )
5374 }
54- }
55- // export const AddNote = () => {
56- // return (
57- // <> // <h1>Add Note (To Do)</h1>
58- // <form id="add-note" action='/'>
59- // <input id="title" type='text' placeholder='Enter a Title'></input>
60- // <textarea id="body" placeholder='Enter the note'></textarea>
61- // <button onClick={e => e.preventDefault()} type="submit">ADDING NOTES COMING SOON!</button>
62- // </form>
63- // <Link to='/'>Go Back</Link>
64- // </>
65- // )
66-
67- // }
75+ }
0 commit comments