├── README.md
├── src
├── Point of Sale
│ ├── PointOfSaleData.js
│ ├── Quantity.jsx
│ └── PointOfSale.jsx
├── views
│ ├── Home.jsx
│ ├── Features.jsx
│ ├── Pricing.jsx
│ ├── FormState.jsx
│ └── Form.jsx
├── components
│ ├── Todo app
│ │ ├── Todoo.jsx
│ │ ├── Todo.jsx
│ │ ├── TodoApp.jsx
│ │ └── NewTodo.jsx
│ ├── pricing
│ │ ├── PricingInfo.jsx
│ │ ├── PricingDetails.jsx
│ │ └── PricingCard.jsx
│ ├── mock-data
│ │ └── pricing-card-data.js
│ └── Header.jsx
├── setupTests.js
├── App.test.js
├── App.js
├── reportWebVitals.js
├── index.js
├── Todo App
│ ├── Todo.jsx
│ └── Index.jsx
├── index.css
└── CounterApp
│ ├── Counter.jsx
│ └── Counters.jsx
├── public
├── robots.txt
├── favicon.ico
├── logo192.png
├── logo512.png
├── manifest.json
└── index.html
├── test.js
├── .gitignore
├── .vscode
└── launch.json
└── package.json
/README.md:
--------------------------------------------------------------------------------
1 | # My React App For Pracctice
--------------------------------------------------------------------------------
/src/Point of Sale/PointOfSaleData.js:
--------------------------------------------------------------------------------
1 | const PointOfSaleData = [
2 | {
3 |
4 | }
5 | ]
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/masud-pervez/react-basic-problem/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/masud-pervez/react-basic-problem/HEAD/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/masud-pervez/react-basic-problem/HEAD/public/logo512.png
--------------------------------------------------------------------------------
/test.js:
--------------------------------------------------------------------------------
1 | // const maxId = Math.max(...[{id: 1},{id: 1},{id: 1},{id: 1},{id: 1},{id: 1},{id: 2}].map(item => item.id));
2 |
3 | // console.log(maxId+1)
--------------------------------------------------------------------------------
/src/views/Home.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 |
3 | const Home = () => {
4 | return (
5 |
home
6 | )
7 | }
8 |
9 | export default Home
--------------------------------------------------------------------------------
/src/components/Todo app/Todoo.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 |
3 | export default function Todoo({todo}) {
4 | return (
5 | {todo}
6 | )
7 | }
8 |
--------------------------------------------------------------------------------
/src/views/Features.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 |
3 | const Features = () => {
4 | return (
5 | features
6 | )
7 | }
8 |
9 | export default Features
--------------------------------------------------------------------------------
/src/setupTests.js:
--------------------------------------------------------------------------------
1 | // jest-dom adds custom jest matchers for asserting on DOM nodes.
2 | // allows you to do things like:
3 | // expect(element).toHaveTextContent(/react/i)
4 | // learn more: https://github.com/testing-library/jest-dom
5 | import '@testing-library/jest-dom';
6 |
--------------------------------------------------------------------------------
/src/App.test.js:
--------------------------------------------------------------------------------
1 | import { render, screen } from '@testing-library/react';
2 | import App from './App';
3 |
4 | test('renders learn react link', () => {
5 | render( );
6 | const linkElement = screen.getByText(/learn react/i);
7 | expect(linkElement).toBeInTheDocument();
8 | });
9 |
--------------------------------------------------------------------------------
/src/components/Todo app/Todo.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import Todoo from './Todoo';
3 |
4 | export default function Todo(props) {
5 | return (
6 |
7 | {props.todos.map((todo ,inx) => (
8 |
9 | ))}
10 |
11 | )
12 | }
13 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import 'bootstrap/dist/css/bootstrap.min.css';
2 | import 'antd/dist/antd.css';
3 | import PointOfSale from './Point of Sale/PointOfSale';
4 |
5 |
6 | function App() {
7 | return (
8 |
13 | );
14 | }
15 |
16 | export default App;
17 |
--------------------------------------------------------------------------------
/src/views/Pricing.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import PricingDetails from '../components/pricing/PricingDetails'
3 | import PricingInfo from '../components/pricing/PricingInfo'
4 |
5 | const Pricing = () => {
6 | return (
7 |
8 |
9 |
10 |
11 | )
12 | }
13 |
14 | export default Pricing
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/src/reportWebVitals.js:
--------------------------------------------------------------------------------
1 | const reportWebVitals = onPerfEntry => {
2 | if (onPerfEntry && onPerfEntry instanceof Function) {
3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
4 | getCLS(onPerfEntry);
5 | getFID(onPerfEntry);
6 | getFCP(onPerfEntry);
7 | getLCP(onPerfEntry);
8 | getTTFB(onPerfEntry);
9 | });
10 | }
11 | };
12 |
13 | export default reportWebVitals;
14 |
--------------------------------------------------------------------------------
/src/components/pricing/PricingInfo.jsx:
--------------------------------------------------------------------------------
1 | import React from "react"
2 | export default function PricingInfo(){
3 | return (
4 |
5 |
6 |
Pricing
7 |
Quickly build an effective pricing table for your potential customers with this Bootstrap example. It’s built with default Bootstrap components and utilities with little customization.
8 |
9 |
10 | )
11 | }
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // Use IntelliSense to learn about possible attributes.
3 | // Hover to view descriptions of existing attributes.
4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5 | "version": "0.2.0",
6 | "configurations": [
7 | {
8 | "type": "pwa-chrome",
9 | "request": "launch",
10 | "name": "Launch Chrome against localhost",
11 | "url": "http://localhost:8080",
12 | "webRoot": "${workspaceFolder}"
13 | }
14 | ]
15 | }
--------------------------------------------------------------------------------
/src/components/pricing/PricingDetails.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import PricingCard from './PricingCard'
3 | import { pricingData } from '../../mock-data/pricing-card-data'
4 |
5 | export default function PricingDetails() {
6 |
7 | return (
8 |
9 |
10 | {
11 | pricingData.map(item => (
12 |
13 | ))
14 | }
15 |
16 |
17 | )
18 | }
19 |
--------------------------------------------------------------------------------
/src/components/Todo app/TodoApp.jsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react'
2 | import NewTodo from './NewTodo'
3 | import Todo from './Todo'
4 |
5 | // const dummytodos = ["todo1","todo2"]
6 |
7 | export default function TodoApp() {
8 |
9 | const [todos,setTodos] = useState([]);
10 |
11 | const handleNewTodo =(newTodo) =>{
12 | setTodos([...todos , newTodo]);
13 | };
14 |
15 | return (
16 |
17 |
18 |
19 |
20 | )
21 | }
22 |
--------------------------------------------------------------------------------
/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 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom/client';
3 | import './index.css';
4 | import App from './App';
5 | import reportWebVitals from './reportWebVitals';
6 | // import MyApp from './MyApp';
7 |
8 | const root = ReactDOM.createRoot(document.getElementById('root'));
9 | root.render(
10 |
11 |
12 |
13 | );
14 |
15 | // If you want to start measuring performance in your app, pass a function
16 | // to log results (for example: reportWebVitals(console.log))
17 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
18 | reportWebVitals();
19 |
--------------------------------------------------------------------------------
/src/components/Todo app/NewTodo.jsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from "react";
2 |
3 | export default function NewTodo({onTodo}) {
4 |
5 | const [todo, setTodo] = useState("");
6 |
7 | const handleInputChange = (event) =>{
8 | setTodo(event.target.value)
9 | }
10 |
11 | const handlesubmit = (event)=>{
12 | event.preventDefault();
13 | onTodo(todo);
14 | }
15 |
16 | return (
17 |
32 | );
33 | }
34 |
--------------------------------------------------------------------------------
/src/Point of Sale/Quantity.jsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from "react";
2 |
3 | export default function Quantity({childToParent}) {
4 | const [quantity, setQuantity] = useState(1);
5 |
6 | const handleIncrement = ()=>{
7 | setQuantity(quantity + 1)
8 | childToParent(quantity)
9 | };
10 | const handleDecrement = ()=>{
11 | setQuantity(quantity - 1)
12 | childToParent(quantity)
13 | };
14 |
15 |
16 | return (
17 |
18 |
23 | +
24 |
25 |
26 |
31 | -
32 |
33 |
34 | );
35 | }
36 |
--------------------------------------------------------------------------------
/src/Todo App/Todo.jsx:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | import { DeleteOutlined, EditOutlined } from "@ant-design/icons";
3 | import { Tooltip } from "antd";
4 | export default function Todo({value , idx, handleDelete ,handleEdit}) {
5 | return (
6 |
7 |
8 | {value}
9 |
10 |
11 | handleDelete(idx)}/>
12 |
13 |
14 |
15 |
16 | handleEdit(idx)}/>
17 |
18 |
19 |
20 |
21 | );
22 | }
23 |
--------------------------------------------------------------------------------
/src/components/pricing/PricingCard.jsx:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 |
3 | const PricingCard = ({ data }) => {
4 | return (
5 |
6 |
7 |
8 | {data.headerTitle}
9 |
10 |
11 |
${data.price} /mo
12 |
13 |
{data.users} users included
14 |
{data.storage} GB of storage
15 |
{data.support}
16 | {data.helpCenterAccess &&
Help center access
}
17 |
18 |
19 | {data.buttonTitle}
20 |
21 |
22 |
23 |
24 | )
25 | }
26 |
27 | export default PricingCard
--------------------------------------------------------------------------------
/src/components/mock-data/pricing-card-data.js:
--------------------------------------------------------------------------------
1 | export const pricingData = [
2 | {
3 | id: 1,
4 | headerTitle: "Free",
5 | headerStyle: "",
6 | price: 0,
7 | users: 10,
8 | storage: 2,
9 | support: "Email support",
10 | helpCenterAccess: true,
11 | buttonTitle: "Sign up for Free",
12 | buttonStyle: "btn-outline-primary",
13 | borderStyle: ""
14 | },
15 | {
16 | id: 2,
17 | headerTitle: "Pro",
18 | headerStyle: "",
19 | price: 15,
20 | users: 20,
21 | storage: 10,
22 | support: "Priority Email",
23 | helpCenterAccess: true,
24 | buttonTitle: "Get Started",
25 | buttonStyle: "btn-primary",
26 | borderStyle: ""
27 | },
28 | {
29 | id: 3,
30 | headerTitle: "Enterprize",
31 | headerStyle: "bg-primary text-light",
32 | price: 29,
33 | users: 30,
34 | storage: 15,
35 | support: "Phone and email support",
36 | helpCenterAccess: true,
37 | buttonTitle: "Contact Us",
38 | buttonStyle: "btn-primary",
39 | borderStyle: "border border-primary"
40 | }
41 | ]
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "myapp",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^5.16.4",
7 | "@testing-library/react": "^13.1.1",
8 | "@testing-library/user-event": "^13.5.0",
9 | "antd": "^4.20.7",
10 | "aos": "^2.3.4",
11 | "bootstrap": "^5.1.3",
12 | "bootstrap-icons": "^1.8.3",
13 | "react": "^18.0.0",
14 | "react-dom": "^18.0.0",
15 | "react-icons": "^4.4.0",
16 | "react-scripts": "5.0.1",
17 | "uuid": "^8.3.2",
18 | "web-vitals": "^2.1.4"
19 | },
20 | "scripts": {
21 | "start": "react-scripts start",
22 | "build": "react-scripts build",
23 | "test": "react-scripts test",
24 | "eject": "react-scripts eject"
25 | },
26 | "eslintConfig": {
27 | "extends": [
28 | "react-app",
29 | "react-app/jest"
30 | ]
31 | },
32 | "browserslist": {
33 | "production": [
34 | ">0.2%",
35 | "not dead",
36 | "not op_mini all"
37 | ],
38 | "development": [
39 | "last 1 chrome version",
40 | "last 1 firefox version",
41 | "last 1 safari version"
42 | ]
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5 | sans-serif;
6 | -webkit-font-smoothing: antialiased;
7 | -moz-osx-font-smoothing: grayscale;
8 | background: linear-gradient(10deg, rgba(18,222,224,1) 0%, rgba(122,226,170,1) 100%);
9 | background-repeat: no-repeat;
10 | height: 100vh;
11 |
12 | }
13 |
14 | code {
15 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
16 | monospace;
17 | }
18 |
19 | .delete-btn{
20 | padding: 1px;
21 | border: 1px solid #055160;
22 | border-radius: 2px;
23 | transition: all 0.1s;
24 | color: #055160;
25 | cursor:pointer;
26 | }
27 | .delete-btn:hover{
28 | border-color: #F92F60;
29 | color: #F92F60;
30 | }
31 |
32 | .edit-btn{
33 | padding: 1px;
34 | border: 1px solid #055160;
35 | border-radius: 2px;
36 | transition: all 0.1s;
37 | color: #055160;
38 | cursor:pointer;
39 | }
40 | .edit-btn:hover{
41 | border-color: #2cc331;
42 | color: #2cc331;
43 | }
44 |
45 | .display-content:hover{
46 | background: linear-gradient(90deg, rgba(122,226,170,1) 0%, rgba(18,222,224,1) 100%);
47 | transition:all 0.5s;
48 | }
--------------------------------------------------------------------------------
/src/components/Header.jsx:
--------------------------------------------------------------------------------
1 | import React from "react";
2 |
3 | export default function Header(){
4 | return (
5 |
6 |
7 |
8 |
Navbar
9 |
10 |
11 |
12 |
28 |
29 |
30 |
31 | )
32 | }
--------------------------------------------------------------------------------
/src/CounterApp/Counter.jsx:
--------------------------------------------------------------------------------
1 | import React from "react";
2 |
3 | export default function Counter( { handleClick , idx , item={} , handleDelete} ) {
4 | return (
5 |
6 |
7 |
8 | #{item.id}
9 | handleDelete(idx)}
12 | >
13 |
14 |
15 |
16 |
17 | 0 ? "primary" : "danger"
20 | }`}
21 | >
22 | {item.value}
23 |
24 |
25 |
26 | handleClick(idx,"increment")}
29 | className="btn btn-primary"
30 | >
31 | Increment
32 |
33 | handleClick(idx,"reset")}
36 | className="btn btn-secondary mx-4"
37 | >
38 | Reset
39 |
40 | handleClick(idx,"decrement")}
43 | className="btn btn-danger"
44 | >
45 | Decrement
46 |
47 |
48 |
49 |
50 |
51 | );
52 | }
53 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 | You need to enable JavaScript to run this app.
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/src/CounterApp/Counters.jsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from "react";
2 | import Counter from "./Counter";
3 |
4 | export default function Counters() {
5 | const [counters, setCounters] = useState([]);
6 |
7 | const handleClick = (id, type) => {
8 | const newCounters = [...counters];
9 | type === "increment"
10 | ? newCounters[id].value++
11 | : type === "decrement"
12 | ? newCounters[id].value--
13 | : (newCounters[id].value = 0);
14 | setCounters(newCounters);
15 | };
16 |
17 | const handleDelete = (id) => {
18 | const newCounter = counters.filter((hg, indx) => indx !== id);
19 | setCounters(newCounter);
20 | };
21 |
22 | const handleResetAll = () => {
23 | const newCounter = counters.map(( item ) => ({ ...item , value: 0 }));
24 | setCounters(newCounter);
25 | };
26 |
27 | const handleClear = () => {
28 | setCounters([]);
29 | };
30 |
31 | const handleAdd = () => {
32 | const maxId =
33 | counters.length < 1
34 | ? 0
35 | : Math.max(...[...counters].map((item) => item.id));
36 | setCounters([...counters, { id: maxId + 1, value: 0 }]);
37 | };
38 |
39 | return (
40 |
41 |
42 |
43 |
48 | Add Counter
49 |
50 |
55 | Clear
56 |
57 |
58 |
63 | Rest All
64 |
65 |
66 |
67 |
68 |
69 | {counters.length > 0 ? (
70 | counters.map((item, idx) => (
71 |
78 | ))
79 | ) : (
80 |
81 |
82 | Please add a counter
83 |
84 |
85 | )}
86 |
87 |
88 | );
89 | }
90 |
--------------------------------------------------------------------------------
/src/views/FormState.jsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react'
2 |
3 | export default function Form() {
4 | const [formData, setFormData] = useState({})
5 |
6 | const handleChange = ({target}) => {
7 | const newFormData = {...formData, [target.id]:target.value}
8 | // console.log(target.id, "===",target.value);
9 | setFormData(newFormData)
10 | }
11 |
12 | const handleSubmit = (v) => {
13 | v.preventDefault();
14 | console.log("Form submited" ,v);
15 | }
16 |
17 | return (
18 |
19 |
20 |
61 |
62 |
63 |
64 |
65 |
Detail
66 |
67 |
68 |
Name {formData.name}
69 |
Email {formData.email}
70 |
Password {formData.password}
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 | )
80 | }
81 |
--------------------------------------------------------------------------------
/src/views/Form.jsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react'
2 |
3 |
4 | export default function Form() {
5 |
6 | const [formData, setFormData] = useState({});
7 |
8 | const handleChange = ({target}) => {
9 | const newData = { ...formData, [target.id]: target.value};
10 | setFormData(newData)
11 | }
12 |
13 | const handleSubmit = (e) => {
14 | e.preventDefault();
15 | console.log("form is submited", formData);
16 | }
17 |
18 | return (
19 |
20 |
21 |
63 |
64 |
65 |
66 |
67 |
Detail
68 |
69 |
70 |
Name {formData.name}
71 |
Email {formData.email}
72 |
Password {formData.password}
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 | )
82 | }
83 |
--------------------------------------------------------------------------------
/src/Todo App/Index.jsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from "react";
2 | import Todo from "./Todo";
3 | import { v4 as uuidv4 } from "uuid";
4 | import { Button, Tooltip } from "antd";
5 | import { FiSend } from "react-icons/fi";
6 | import { GrUpdate } from "react-icons/gr";
7 |
8 | export default function Index() {
9 | // all state defined here
10 | const [content, setContent] = useState("");
11 | const [items, setItems] = useState([]);
12 | const [toggleSubmit, setToggleSubmit] = useState(true);
13 | const [edited, setEdited] = useState(null);
14 |
15 | // all handle proseser will work here in the main root file
16 | const handleChange = (event) => {
17 | setContent(event.target.value);
18 | };
19 |
20 | const handleAdd = (event) => {
21 |
22 | if(!toggleSubmit){
23 | setItems(
24 | items.filter((elem) => {
25 | if (elem.id === edited) {
26 | return (elem.name = content);
27 | }
28 | return elem;
29 | })
30 | );
31 | setContent("");
32 | }else {
33 | setItems([...items, { id: uuidv4(), name: content }]);
34 | setContent("");
35 | }
36 | setToggleSubmit(true);
37 | event.preventDefault();
38 | };
39 |
40 |
41 | const handleDelete = (idx) => {
42 | const newItems = items.filter((item) => idx !== item.id);
43 | setItems(newItems);
44 | };
45 |
46 | const handleRemoveAll = () => {
47 | setItems([]);
48 | setToggleSubmit(true);
49 | };
50 |
51 | const handleEdit = (item) => {
52 | const newEditItem = items.find((elem) => {
53 | return elem.id === item;
54 | });
55 | setToggleSubmit(false);
56 | setContent(newEditItem.name);
57 | setEdited(item);
58 |
59 | };
60 | // return the DOM item
61 | return (
62 |
66 |
67 |
68 |
69 |
📝 Add your list here
70 |
71 |
72 |
99 |
100 | {items.length > 0 &&
101 | items.map((item) => (
102 |
109 | ))}
110 |
111 |
112 |
113 | Remove All
114 |
115 |
116 |
117 |
118 |
119 |
120 | );
121 | }
122 |
--------------------------------------------------------------------------------
/src/Point of Sale/PointOfSale.jsx:
--------------------------------------------------------------------------------
1 | import React, { useState } from "react";
2 | import { AiOutlineDelete } from "react-icons/ai";
3 | import Quantity from "./Quantity";
4 |
5 | export default function PointOfSale() {
6 | const [product, setProduct] = useState("");
7 | const [productDetails, setProductDetails] = useState([]);
8 | const [quantity,setQuantity] = useState(1)
9 | const handleChange = ({ target }) => {
10 | setProduct(target.value);
11 | };
12 |
13 | const handleSubmit = (e) => {
14 | e.preventDefault();
15 | const maxId =
16 | productDetails.length < 1
17 | ? 0
18 | : Math.max(...[...productDetails].map((item) => item.id));
19 |
20 | if (product === "Ice-Cream") {
21 | const data = {
22 | id: maxId + 1,
23 | name: "Ice-Cream",
24 | cost: 15,
25 | code: "A4254",
26 | price: 0,
27 | quantity
28 | };
29 | const newProductDetails = [...productDetails, data];
30 | setProductDetails(newProductDetails);
31 | }
32 |
33 | if (product === "Cocolate") {
34 | const data = {
35 | id: maxId + 1,
36 | name: "Cocolate",
37 | cost: 10,
38 | code: "A4424",
39 | price: 0,
40 | quantity
41 | };
42 | const newProductDetails = [...productDetails, data];
43 | setProductDetails(newProductDetails);
44 | }
45 |
46 | if (product === "Cold-Drinks") {
47 | const data = {
48 | id: maxId + 1,
49 | name: "Cold-Drinks",
50 | cost: 25,
51 | code: "A4224",
52 | price: 0,
53 | quantity
54 | };
55 | const newProductDetails = [...productDetails, data];
56 | setProductDetails(newProductDetails);
57 | }
58 | setProduct("");
59 | // setQuantity(1)
60 | };
61 |
62 | const childToParent = (data) => {
63 | setQuantity(data);
64 | }
65 | const handleDelete = (id) => {
66 | const newProductDetails = productDetails.filter((item, idx) => idx !== id);
67 | setProductDetails(newProductDetails);
68 | };
69 |
70 | return (
71 |
72 |
76 |
77 |
78 |
79 |
97 | {productDetails.length > 0 ? (
98 |
99 |
100 |
101 | #ID
102 | Product Name
103 | Product Code
104 | Cost
105 | Quantity
106 | Price
107 | Option
108 |
109 |
110 |
111 | {productDetails.map((item, idx) => (
112 |
113 | {item.id}
114 | {item.name}
115 | {item.code}
116 | {item.cost}
117 | {item.quantity}
118 | {item.cost * item.quantity}
119 |
120 | handleDelete(idx)}
123 | />
124 |
125 |
126 | ))}
127 |
128 |
129 | ) : (
130 |
131 | Please Select Items
132 |
133 | )}
134 |
135 |
136 |
137 |
138 |
139 | );
140 | }
141 |
--------------------------------------------------------------------------------