From 8e67c0ca3ffcbc66b7e91864796fc0537823a410 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Fri, 27 Sep 2019 12:05:49 +0100 Subject: [PATCH] End of chapter 20 --- .idea/workspace.xml | 15 ++++++++++++--- src/App.js | 20 ++++++++++++++++++-- src/components/layout/Alert.js | 20 ++++++++++++++++++++ src/components/users/Search.js | 12 +++++++++--- 4 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 src/components/layout/Alert.js diff --git a/.idea/workspace.xml b/.idea/workspace.xml index e8f5c23..f7be7a9 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,8 @@ + + @@ -57,7 +59,7 @@ - + @@ -102,7 +104,7 @@ diff --git a/src/App.js b/src/App.js index 030e4cc..2c1ec25 100644 --- a/src/App.js +++ b/src/App.js @@ -3,6 +3,7 @@ import PropTypes from "prop-types"; import Navbar from "./components/layout/Navbar"; import Users from "./components/users/Users"; import Search from "./components/users/Search"; +import Alert from './components/layout/Alert'; import axios from "axios"; import "./App.css"; @@ -11,7 +12,8 @@ class App extends Component { super(props); this.state = { users: [], - loading: false + loading: false, + alert:null } } @@ -38,6 +40,14 @@ class App extends Component { this.setState({ users: [], loading: false }); }; + // Show an alert + + setAlert = (msg, type) => { + this.setState({alert:{msg, type}}); + + setTimeout(() => this.setState({alert:null}), 5000); + }; + render() { const {users, loading} = this.state; return ( @@ -45,7 +55,13 @@ class App extends Component {
- 0}/> + + 0} + setAlert={this.setAlert} + + />
diff --git a/src/components/layout/Alert.js b/src/components/layout/Alert.js new file mode 100644 index 0000000..ec86080 --- /dev/null +++ b/src/components/layout/Alert.js @@ -0,0 +1,20 @@ +import React from 'react'; +import PropTypes from 'prop-types'; + +const Alert = ({alert}) => { + return ( + alert !== null && ( +
+ {alert.msg} +
+ + ) + + ); +}; + +Alert.propTypes = { + +}; + +export default Alert; diff --git a/src/components/users/Search.js b/src/components/users/Search.js index bdeacda..7dfaf5d 100644 --- a/src/components/users/Search.js +++ b/src/components/users/Search.js @@ -18,8 +18,13 @@ class Search extends Component { onSubmit = e => { e.preventDefault(); - this.props.searchUsers(this.state.text); - this.setState({text:''}); + if (this.state.text === '') { + this.props.setAlert('Please enter something', 'light'); + } else { + this.props.searchUsers(this.state.text); + this.setState({text:''}); + + } }; render() { @@ -43,7 +48,8 @@ class Search extends Component { Search.propTypes = { searchUsers:PropTypes.func.isRequired, clearUsers: PropTypes.func.isRequired, - showClear: PropTypes.bool.isRequired + showClear: PropTypes.bool.isRequired, + setAlert: PropTypes.func.isRequired }; export default Search;