Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React day 1 #139

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions react-exercises/grocery-list-react/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Instructions

In this exercises, you'll will make a reactive grocery list.

## How to do the question
- Run `npm install` in the `grocery-list-react` folder.
- Run `npm run start` to start the development server.

## Requirements
- Users should see a grocery list items a couple of button to control the behavior of the list.
- User should be able to add more grocery items through an input box and click a `Add` button to add it to the list displaying the item.
- There should be a button to clear all the grocery list items at once.
- If there is an item already, and the same item is added again, show `2` now beside that grocery item. This number is going to increase everytime the same item is added to the grocery list.
- Clicking on a grocery item should change its color to red. Clicking again should change
it back to black. Red means the item has been purchased.
- You can add any other control as you prefer.
- **The UI is totally up to you. Make it as beautiful as you can by using any CSS that you can write. No libraries allowed of any kind.**
- Make sure to make your as readable, maintainable as possible.

## Restrictions
- You are not allowed to use any extra library at all for `CSS` or any other thing.
34 changes: 34 additions & 0 deletions react-exercises/grocery-list-react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "grocery-list",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file not shown.
43 changes: 43 additions & 0 deletions react-exercises/grocery-list-react/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions react-exercises/grocery-list-react/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions react-exercises/grocery-list-react/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
38 changes: 38 additions & 0 deletions react-exercises/grocery-list-react/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
54 changes: 54 additions & 0 deletions react-exercises/grocery-list-react/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';
import GroceryList from './components/GroceryList';

function App() {
const [groceryItems, setGroceryItems] = React.useState([]);
const [inputBoxValue, setInputBoxValue] = React.useState('');

const groceryItemExists = newGroceryItem => {
return groceryItems.findIndex(groceryItem => groceryItem.name === newGroceryItem) > -1;
};

const addGroceryItem = e => {
e.preventDefault();
if (!inputBoxValue) return;
const newGroceryItem = inputBoxValue;
let newGroceryItems = [];
if (groceryItemExists(inputBoxValue)) {
const duplicateGroceryItemIndex = groceryItems.findIndex(
groceryItem => groceryItem.name === newGroceryItem
);
newGroceryItems = [...groceryItems];
newGroceryItems[duplicateGroceryItemIndex].quantity += 1;
} else {
newGroceryItems = [
...groceryItems,
{ name: inputBoxValue, clicked: 0, id: groceryItems.length, quantity: 1 }
];
}
setGroceryItems(newGroceryItems);
setInputBoxValue('');
};

const handleGroceryItemClick = groceryItemId => {
const newGroceryItems = [...groceryItems];
newGroceryItems[groceryItemId].clicked += 1;
setGroceryItems(newGroceryItems);
};

return (
<div className="App">
<form onSubmit={addGroceryItem}>
<input
type="text"
value={inputBoxValue}
onChange={e => setInputBoxValue(e.target.value)}
></input>
<button type="submit">Add</button>
</form>
<GroceryList items={groceryItems} handleClick={handleGroceryItemClick}></GroceryList>
</div>
);
}

export default App;
9 changes: 9 additions & 0 deletions react-exercises/grocery-list-react/src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import { render } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
const { getByText } = render(<App />);
const linkElement = getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
17 changes: 17 additions & 0 deletions react-exercises/grocery-list-react/src/components/GroceryItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';

const GroceryItem = ({ item, handleClick }) => {
const redText = {
color: 'red'
};
const blackText = {
color: 'black'
};
return (
<p onClick={() => handleClick(item.id)} style={item.clicked > 0 ? redText : blackText}>
{item.name} <span>{item.quantity}</span>
</p>
);
};

export default GroceryItem;
14 changes: 14 additions & 0 deletions react-exercises/grocery-list-react/src/components/GroceryList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import GroceryItem from './GroceryItem';

const GroceryList = ({ items, handleClick }) => {
return (
<React.Fragment>
{items.map(item => (
<GroceryItem item={item} key={item.id} handleClick={handleClick}></GroceryItem>
))}
</React.Fragment>
);
};

export default GroceryList;
13 changes: 13 additions & 0 deletions react-exercises/grocery-list-react/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
17 changes: 17 additions & 0 deletions react-exercises/grocery-list-react/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
7 changes: 7 additions & 0 deletions react-exercises/grocery-list-react/src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading