├── .env
├── .eslintrc.js
├── .gitignore
├── README.md
├── notes
├── create-react-app-notes.md
├── picture-1.png
├── picture-2.png
└── picture-3.png
├── package-lock.json
├── package.json
├── public
├── favicon.ico
├── index.html
└── manifest.json
├── src
├── App.css
├── App.jsx
├── App.test.js
├── components
│ ├── button.css
│ ├── button.jsx
│ ├── item.css
│ ├── item.jsx
│ └── price.jsx
├── index.css
├── index.jsx
├── inventory.js
├── logo.svg
├── map-filter-reduce.js
└── serviceWorker.js
└── yarn.lock
/.env:
--------------------------------------------------------------------------------
1 | SKIP_PREFLIGHT_CHECK=true
--------------------------------------------------------------------------------
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | "extends": "airbnb",
3 | "parser": "babel-eslint"
4 | };
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # React Product List Challenge
2 |
3 | This is a starter project for a React challenge. The goal is to create a page that displays a list of **products** and a list of **categories** for those products. Clicking one of the category buttons should filter the list of products display to only display products in that category.
4 |
5 | Use components to your advantage for this assignment. Whenever possible make a component to simplify your work.
6 |
7 | The starter project provides a `categories` array and an `inventory` array in `inventory.js`. You can import these into any module with:
8 |
9 | `import inventory, { categories } from './inventory'`
10 |
11 | - `categories`: `[String]` an Array of category name Strings
12 | - `inventory`: `[Object]` an Array of Objects with the following properties
13 | - `id`: `Number` a unique number id
14 | - `name`: `String` a String name of product
15 | - `description`: `String` a String description of product
16 | - `price`: `String` a _String_ price with two decimal places
17 | - `category`: `String` a String category name
18 |
19 | For example, the first product in the Array looks like this:
20 |
21 | ```JS
22 | {
23 | 'id':1,
24 | 'name':'Duobam',
25 | 'description':'Implemented even-keeled info-mediaries',
26 | 'price':'7.77',
27 | 'category':'Outdoors'
28 | }
29 | ```
30 |
31 | ## Getting functional
32 |
33 | Besides using React you will also explore and practice functional programming concepts with `Array.map()`, `Array.filter()`, and `Array.reduce`.
34 |
35 | You will use [`Array.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) to transform the inventory Objects into a JSX/Components to be displayed by React.
36 |
37 | You will use [`Array.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) to filter the list of products displayed by category.
38 |
39 | The stretch challenges use [`Array.reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce).
40 |
41 | ## Challenges
42 |
43 | Your goal is to follow the steps below and solve the challenges.
44 |
45 | You will fork this repo and start working on your fork.
46 |
47 | **You must commit each time you sit down to work on this project!**
48 |
49 | This challenge should take about 3 hours. Be sure to plan that amount of time to spend on the challenges here.
50 |
51 | **Getting Started**
52 |
53 | 1. Fork this Repo.
54 | 1. Post a link to the progress tracker for class.
55 | 1. `npm install` to install dependencies
56 | 1. `npm start` to run the project at [http://localhost:3000](http://localhost:3000)
57 |
58 | From here as you work you should see changes refresh in the browser as you save files. If there is an error you will see this in the browser.
59 |
60 | This project was bootstrapped with Create React App see the notes [here](notes/create-react-app-notes.md) for more information.
61 |
62 | ## Coding Challenges
63 |
64 | **Level 1 challenge**
65 |
66 | Display the categories and products.
67 |
68 | 1. Challenge: List all of the categories at the top of the page.
69 | - Display the categories as buttons.
70 | - Use `Array.map()` to transform the `category` array into an array of JSX/Components
71 | - You can import categories into any module with `import { categories } from './inventory'`
72 | 1. Challenge: List all of products below the categories.
73 | - Each Product should display with it's name, category, and price. How these are displayed is up to you.
74 | - If you add a class name to a JSX element use `className` in place of `class` for example `
`. See the documentation for [`className`](https://reactjs.org/docs/faq-styling.html) for more information.
75 | - You can import the inventory Array into any module with `import inventory from './inventory'`.
76 | - `inventory` is an Array of Objects with properties: id, name, description, price, and category. See the notes above for more details.
77 |
78 |
79 |
80 | **Level 2 Challenge**
81 |
82 | Add some interaction and functionality. The goal here is to click on a category button to filter the list of products so only products in the chosen category are displayed.
83 |
84 | 1. Challenge: Clicking a category should display only products in that category.
85 | - The parent component, that is the component that is parent to both the product list and the category list, should define the current category on `this.state`.
86 | - Define state as an object in the constructor
87 | - Set a property on the state object, something like: `currentCategory`
88 | - Give it a sensible default value: `this.state = { currentCategory: null }`
89 | - Add an `onClick` handler for each category button. This should:
90 | - Pass the category String/name of the button to the handler.
91 | - Set `currentCategory` on state with `this.setState({ currentCategory: newCategory })` or something similar.
92 | - Use `Array.filter()` to display only products in `inventory` where the category matches. Something like:
93 | ```js
94 | inventory.filter((item) => {
95 | return item.category === this.state.currentCategory || this.state.currentCategory === null
96 | })
97 | ```
98 |
99 | **Level 3 Challenges**
100 |
101 | Use components! Whenever possible you should use a component. React uses a component architecture. The component architectrure is a really good thing it makes your projects easier to manage, keeps your code [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself), and makes your code more portable.
102 |
103 | 1. Make a component that is a category button.
104 | - Define this in a module/JS file. Something like: `category-button.js`
105 | - Be sure to export this. Something like: `export default CategoryButton`
106 | - Set the label and click function as props, something like: ` clickCategory(name) } />`
107 | 1. Define a component that is a product.
108 | - The product component should take in it's id, name, description, and price as props. Alternately it could take an object with these properties.
109 | - The product component should display these bits of information in a reasonable way.
110 |
111 | The category button component might look like this. These are inclomplete! You'll need to retool these to fit your project.
112 |
113 | ```JS
114 | import { Component } from 'react'
115 |
116 | class CategoryButton extends Component {
117 | render() {
118 | return
119 | }
120 | }
121 | ```
122 |
123 | The product component might look like this:
124 |
125 | ```JS
126 | import { Component } from 'react'
127 |
128 | class Product extends Component {
129 | render() {
130 | return (
131 |
132 |
{this.props.name}
133 |
{this.props.name}
134 |
${this.props.price}
135 |
136 | )
137 | }
138 | }
139 | ```
140 |
141 |
142 | **Level 4 Challenges**
143 |
144 | Unless you went rogue, the page is probably looking pretty bland. Better add some styles!
145 |
146 | 1. Style the category buttons. Make them look like something people will want to click on.
147 | - Use Flex box to put them all in a row. It's okay if they wrap, there are many categories.
148 | 1. Style the products in the list.
149 | - Use CSS Grid. You can just set the number of columns with: `grid-template-columns` this should be enough to get all your pro**ducks** in a row so to speak.
150 |
151 | **Level 5 Challenges**
152 |
153 | Handling the details. If you've got the items above worked out you'll realize the interface is not very satisfying. You can make it better!
154 |
155 | 1. Display All category
156 | - Add one more button to the list of category buttons. It's label should "All".
157 | - Clicking this button should display all products.
158 | 2. We need to know which category is currently selected. The buttons should reflect.
159 | - Define a style to make the currently selected category stand apart from the other buttons.
160 | - When generating the category elements check the category name against `this.currentCategory` if the names match assign a class to that element, something like `selected-category` remember to use `className` not `class`!
161 | - You'll need to take into account that the "All" button is it's own category and this category should display all the products!
162 |
163 | Level 6 Challenges
164 |
165 | Okay so you did all of the other challenges and you need something more to do, good for you!
166 |
167 | 1. Use `Array.reduce()` to get the sum of all of the prices for all Products.
168 | - Remember the prices are stored as Strings you'll need to convert these to numbers. Something like: `Number(item.price)` should work.
169 | - Display this somewhere on the page. If you got this far I don't need to add too much explaination here.
170 | 2. Using `Array.reduce()` again, sum the total for currently selected products. In other the sum of all the prices for the products in the currently selected category.
171 | 3. Use `Array.reduce()` to count the number of products in each category.
172 | - Display count for each category as "badge" next to the category label in each category button.
173 |
174 | ## Some Visuals
175 |
176 | Some people like pictures. Here are a few images showing what the project might look like when you are finished, with some notes.
177 |
178 | 
179 |
180 | 
181 |
182 | 
--------------------------------------------------------------------------------
/notes/create-react-app-notes.md:
--------------------------------------------------------------------------------
1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2 |
3 | ## Available Scripts
4 |
5 | In the project directory, you can run:
6 |
7 | ### `npm start`
8 |
9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11 |
12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console.
14 |
15 | ### `npm test`
16 |
17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19 |
20 | ### `npm run build`
21 |
22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance.
24 |
25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed!
27 |
28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29 |
30 | ### `npm run eject`
31 |
32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33 |
34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35 |
36 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37 |
38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39 |
40 | ## Learn More
41 |
42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43 |
44 | To learn React, check out the [React documentation](https://reactjs.org/).
45 |
--------------------------------------------------------------------------------
/notes/picture-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LucHighwalker/react-product-list/b1e9b0d574f2698a981124668170d1be2e6fd1f4/notes/picture-1.png
--------------------------------------------------------------------------------
/notes/picture-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LucHighwalker/react-product-list/b1e9b0d574f2698a981124668170d1be2e6fd1f4/notes/picture-2.png
--------------------------------------------------------------------------------
/notes/picture-3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LucHighwalker/react-product-list/b1e9b0d574f2698a981124668170d1be2e6fd1f4/notes/picture-3.png
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-product-list",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "lodash": "^4.17.11",
7 | "prop-types": "^15.6.2",
8 | "react": "^16.7.0",
9 | "react-dom": "^16.7.0",
10 | "react-scripts": "^2.1.3"
11 | },
12 | "scripts": {
13 | "start": "react-scripts start",
14 | "build": "react-scripts build",
15 | "test": "react-scripts test",
16 | "eject": "react-scripts eject"
17 | },
18 | "eslintConfig": {
19 | "extends": "react-app"
20 | },
21 | "browserslist": [
22 | ">0.2%",
23 | "not dead",
24 | "not ie <= 11",
25 | "not op_mini all"
26 | ],
27 | "devDependencies": {
28 | "babel-eslint": "^10.0.1",
29 | "eslint": "^5.13.0",
30 | "eslint-config-airbnb": "^17.1.0",
31 | "eslint-plugin-import": "^2.16.0",
32 | "eslint-plugin-jsx-a11y": "^6.2.1",
33 | "eslint-plugin-react": "^7.12.4"
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LucHighwalker/react-product-list/b1e9b0d574f2698a981124668170d1be2e6fd1f4/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
22 | React App
23 |
24 |
25 |
28 |
29 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | }
10 | ],
11 | "start_url": ".",
12 | "display": "standalone",
13 | "theme_color": "#000000",
14 | "background_color": "#ffffff"
15 | }
16 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 |
3 | }
4 |
--------------------------------------------------------------------------------
/src/App.jsx:
--------------------------------------------------------------------------------
1 | import React, { Component } from 'react';
2 | import inventory, { categories } from './inventory';
3 |
4 | import Button from './components/button';
5 | import Item from './components/item';
6 |
7 | import './App.css';
8 |
9 | class App extends Component {
10 | state = {
11 | currentCat: [],
12 | };
13 |
14 | getCategories() {
15 | return categories.map(cat => (
16 |
17 |
23 | ));
24 | }
25 |
26 | getInventory() {
27 | const { currentCat } = this.state;
28 |
29 | return inventory
30 | .filter((item) => {
31 | let selected = false;
32 | if (currentCat.length === 0) {
33 | selected = true;
34 | } else {
35 | currentCat.forEach((cat) => {
36 | if (cat === item.category) {
37 | selected = true;
38 | }
39 | });
40 | }
41 | return selected;
42 | })
43 | .map(({
44 | id, name, price, description,
45 | }) => (
46 |
47 | ));
48 | }
49 |
50 | buttonClasses(cat) {
51 | let active = false;
52 | const { currentCat } = this.state;
53 |
54 | currentCat.forEach((c) => {
55 | if (c === cat) {
56 | active = true;
57 | }
58 | });
59 |
60 | return active ? 'button active' : 'button';
61 | }
62 |
63 | allButtonClasses() {
64 | const { currentCat } = this.state;
65 | return currentCat.length === 0 ? 'button active' : 'button';
66 | }
67 |
68 | changeCategory(cat) {
69 | let { currentCat } = this.state;
70 | let found = false;
71 |
72 | if (cat !== 'All') {
73 | for (let i = 0; i < currentCat.length; i += 1) {
74 | if (currentCat[i] === cat) {
75 | found = true;
76 | currentCat.splice(i, 1);
77 | }
78 | }
79 |
80 | if (!found) {
81 | currentCat.push(cat);
82 | }
83 | } else {
84 | currentCat = [];
85 | }
86 |
87 | this.setState({
88 | currentCat,
89 | });
90 | }
91 |
92 | render() {
93 | return (
94 |