Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
QuizMaker/client/src/components/header/Header.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
91 lines (68 sloc)
2.65 KB
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 from 'react'; | |
//import React, { Component } from 'react'; | |
import './Header.css'; | |
import react_logo from '../../quizLogo.svg'; | |
class Header extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
message: this.props.message, | |
formMessage: '' | |
} | |
} | |
handleSearchSubmit = (event) => { | |
//prevent the form to be submitted to its action url | |
event.preventDefault(); | |
console.log("search button was clicked"); | |
console.log('Received values of form: ', this.state.formMessage); | |
if (this.state.formMessage !== '') { | |
fetch('http://localhost:3000/api/v1.0/', { | |
method: 'POST', | |
headers: { | |
'Accent': 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify({ message: this.state.formMessage }) | |
}).then( | |
response => { | |
if (response.status !== 200) { | |
console.log('Looks like there was a problem. Status Code: ' + | |
response.status); | |
return; | |
} | |
// Examine the text in the response | |
response.json().then(data => { | |
console.log(data['message']); | |
this.setState({ message: data['message'] }) | |
}); | |
} | |
) | |
.catch(function (err) { | |
console.log('Fetch Error :-S', err); | |
}); | |
this.setState({ formMessage: '' }) | |
} | |
} | |
handleInputChange = (event) => { | |
this.setState({ formMessage: event.target.value }) | |
} | |
render() { | |
return ( | |
//this is JSX code which is very similar to HTML we already know | |
<div className="header" style={{zIndex: '100'}} > | |
<img src={react_logo} alt="React logo" /><a href="#default" className="logo"> {this.state.message}</a> | |
<div className="header-right"> | |
<div className="search-container"> | |
{/*} | |
<form action=""> | |
<input type="text" placeholder="New title..." name="txtSearch" value={this.state.formMessage} onChange={this.handleInputChange} /> | |
<button type="submit" onClick={this.handleSearchSubmit}>New Title</button> | |
</form> | |
*/} | |
</div> | |
</div> | |
</div> | |
); | |
} | |
} | |
export default Header; |