diff --git a/src/actions/search.js b/src/actions/search.js new file mode 100644 index 0000000..54c5f23 --- /dev/null +++ b/src/actions/search.js @@ -0,0 +1,22 @@ +import { TOGGLE_SEARCHBAR, SEARCH_PRODUCTS, IS_SEARCHING } from './types'; +import axios from 'axios'; + +export const toggleSearchbar = () => (dispatch) => { + dispatch({ type: TOGGLE_SEARCHBAR }); +}; + +export const searchProducts = (searchInput) => async (dispatch) => { + const { data } = await axios.get('http://localhost:5005/data/all'); + + const filteredData = data.filter((obj) => + Object.keys(obj).some((key) => + obj[key].toLowerCase().includes(searchInput.toLowerCase()) + ) + ); + + dispatch({ type: SEARCH_PRODUCTS, payload: filteredData }); +}; + +export const searchStateToggle = () => (dispatch) => { + dispatch({ type: IS_SEARCHING }); +}; diff --git a/src/actions/types.js b/src/actions/types.js index 4036d6b..921254d 100644 --- a/src/actions/types.js +++ b/src/actions/types.js @@ -10,3 +10,6 @@ export const AUTH_ERROR = 'AUTH_ERROR'; export const REGISTER_FAIL = 'REGISTER_FAIL'; export const LOGIN_SUCCESS = 'LOGIN_SUCCESS'; export const LOGIN_FAIL = 'LOGIN_FAIL'; +export const TOGGLE_SEARCHBAR = 'TOGGLE_SEARCHBAR'; +export const SEARCH_PRODUCTS = 'SEARCH_PRODUCTS'; +export const IS_SEARCHING = 'IS_SEARCHING'; diff --git a/src/components/App.js b/src/components/App.js index 7c92aba..7ff68e5 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -62,6 +62,7 @@ class App extends React.Component { render() { // Grab counter from redux state // const counter = useSelector(state => state.isLogged); + const { isSearching, searchResults } = this.props; return ( {/*

{this.counter}

@@ -70,9 +71,15 @@ class App extends React.Component {
- {this.state.data.map((info) => ( - - ))} + {isSearching && searchResults.length === 0 &&

No matches!

} + + {isSearching + ? searchResults.map((info) => ( + + )) + : this.state.data.map((info) => ( + + ))}