Created
August 19, 2020 20:18
-
-
Save stoneboyindc/5943b3c1297a6de07c7f24c15054ede1 to your computer and use it in GitHub Desktop.
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
import React, { Component } from 'react'; | |
import dataContext from './dataContext'; | |
export default class AddFolder extends Component{ | |
static contextType=dataContext; | |
handleSubmit=e=>{ | |
e.preventDefault() | |
const name=e.target.folder; | |
console.log(name.value,name); | |
let newFolder = { "name" : name.value}; | |
fetch(`http://localhost:9090/folders`,{ | |
method:'POST', | |
body:JSON.stringify(newFolder), | |
headers:{'Content-Type':'application/json'} | |
}).then(res=>{ | |
if(!res.ok){return res.json().then(err=>{throw err})} | |
return res.json() | |
}).then(resJson=>{ | |
console.log(resJson) | |
this.context.addFolder(resJson) | |
}).catch(error=>{console.log(error)}) | |
} | |
render(){ | |
const data = this.context; | |
return( | |
<div className="addFolder"> | |
<form onSubmit={this.handleSubmit}> | |
<label htmlFor="folder" >Folder Name</label> | |
<input type='text' name="folder" | |
id="folder" required/> | |
<button type='submit'> | |
Submit | |
</button> | |
</form> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment