-
-
Save codecademydev/c75cc8a768c9a83a274388e4b1431fb1 to your computer and use it in GitHub Desktop.
Codecademy export
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 { Inventory } from '../features/inventory/Inventory.js'; | |
import { CurrencyFilter } from '../features/currencyFilter/CurrencyFilter.js'; | |
// Import the Cart component here. | |
import { Cart } from '../features/cart/Cart.js'; | |
import { SearchTerm } from '../features/searchTerm/SearchTerm.js'; | |
// Render the Cart component below <Inventory /> | |
export const App = (props) => { | |
const { state, dispatch } = props; | |
return ( | |
<div> | |
<CurrencyFilter | |
currencyFilter={state.currencyFilter} | |
dispatch={dispatch} | |
/> | |
<SearchTerm | |
searchTerm={state.searchTerm} | |
dispatch={dispatch} | |
/> | |
<Inventory | |
inventory={state.inventory} | |
currencyFilter={state.currencyFilter} | |
dispatch={dispatch} | |
/> | |
<Cart | |
cart={state.cart} | |
currencyFilter={state.currencyFilter} | |
dispatch={dispatch} | |
/> | |
</div> | |
); | |
}; |
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 { setSearchTerm, clearSearchTerm } from './searchTermSlice.js'; | |
const searchIconUrl = | |
'https://static-assets.codecademy.com/Courses/Learn-Redux/Recipes-App/icons/search.svg'; | |
const clearIconUrl = | |
'https://static-assets.codecademy.com/Courses/Learn-Redux/Recipes-App/icons/clear.svg'; | |
export const SearchTerm = (props) => { | |
const { searchTerm, dispatch } = props; | |
const onSearchTermChangeHandler = (e) => { | |
const userInput = e.target.value; | |
dispatch(setSearchTerm(userInput)); | |
}; | |
const onClearSearchTermHandler = () => { | |
dispatch(clearSearchTerm()); | |
}; | |
return ( | |
<div id="search-container"> | |
<img id="search-icon" alt="" src={searchIconUrl} /> | |
<input | |
id="search" | |
type="text" | |
value={searchTerm} | |
onChange={onSearchTermChangeHandler} | |
placeholder="Search products" | |
/> | |
{searchTerm.length > 0 && ( | |
<button | |
onClick={onClearSearchTermHandler} | |
type="button" | |
id="search-clear-button" | |
> | |
<img src={clearIconUrl} alt="" /> | |
</button> | |
)} | |
</div> | |
); | |
}; |
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
/* | |
Extra Credit: | |
1. Create a function called searchTermReducer that can handle the following action types: | |
* 'searchTerm/setSearchTerm' | |
* 'searchTerm/clearSearchTerm' | |
* Don't forget to set the initial state and return state by default! | |
2. Create a function called setSearchTerm | |
* It has one parameter, term | |
* It returns an action object whose payload is the term value | |
* See SearchTerm.js for how this will be used. | |
3. Create a function called clearSearchTerm | |
* It returns an action object with no payload | |
* See SearchTerm.js for how this will be used. | |
*/ | |
const initialState = ''; | |
const searchTermReducer = (state = initialState, action) => { | |
switch (action.type) { | |
case 'searchTerm/setSearchTerm': | |
return action.payload; | |
case 'searchTerm/clearSearchTerm': | |
return ''; | |
default: { | |
return state; | |
} | |
} | |
} | |
export const setSearchTerm = (term) => { | |
return { | |
type: 'searchTerm/setSearchTerm', | |
payload: term | |
} | |
} | |
export const clearSearchTerm = () => { | |
return { | |
type: 'searchTerm/clearSearchTerm' | |
} | |
} | |
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 createStore and combineReducers here. | |
import { createStore, combineReducers } from 'redux'; | |
// Import the slice reducers here. | |
import { inventoryReducer } from '../features/inventory/inventorySlice.js'; | |
import { cartReducer } from '../features/cart/cartSlice.js'; | |
import { currencyFilterReducer } from '../features/currencyFilter/currencyFilterSlice.js'; | |
import { searchTermReducer } from '../features/searchTerm/searchTermSlice.js'; | |
// Create and export the store here. | |
export const store = createStore(combineReducers({ | |
inventory: inventoryReducer, | |
cart: cartReducer, | |
currencyFilter: currencyFilterReducer, | |
searchTerm: searchTermReducer | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment