├── .gitignore
├── README.md
├── package-lock.json
├── package.json
├── public
├── favicon.ico
├── images
│ ├── add.png
│ ├── arrow.png
│ ├── cart.png
│ ├── decr.png
│ ├── delete.png
│ ├── incr.png
│ ├── minus.png
│ └── remove.png
├── index.html
├── manifest.json
└── robots.txt
├── src
├── App.css
├── App.js
├── App.test.js
├── component
│ ├── Cart.js
│ ├── ContextCart.js
│ ├── Items.js
│ ├── Navbar.js
│ ├── cart.css
│ ├── product.js
│ └── reducer.js
├── components
│ ├── Cart.js
│ ├── ContextCart.js
│ ├── Items.js
│ ├── cart.css
│ ├── products.js
│ └── reducer.js
├── index.css
├── index.js
├── logo.svg
├── reportWebVitals.js
└── setupTests.js
└── yarn.lock
/.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 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `yarn start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13 |
14 | The page will reload if you make edits.\
15 | You will also see any lint errors in the console.
16 |
17 | ### `yarn test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `yarn build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `yarn eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35 |
36 | 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.
37 |
38 | 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.
39 |
40 | 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.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
48 | ### Code Splitting
49 |
50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51 |
52 | ### Analyzing the Bundle Size
53 |
54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55 |
56 | ### Making a Progressive Web App
57 |
58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59 |
60 | ### Advanced Configuration
61 |
62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63 |
64 | ### Deployment
65 |
66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67 |
68 | ### `yarn build` fails to minify
69 |
70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
71 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "addtocart",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^5.11.4",
7 | "@testing-library/react": "^11.1.0",
8 | "@testing-library/user-event": "^12.1.10",
9 | "react": "^17.0.2",
10 | "react-custom-scrollbars-2": "^4.4.0",
11 | "react-dom": "^17.0.2",
12 | "react-scripts": "4.0.3",
13 | "web-vitals": "^1.0.1"
14 | },
15 | "scripts": {
16 | "start": "react-scripts start",
17 | "build": "react-scripts build",
18 | "test": "react-scripts test",
19 | "eject": "react-scripts eject"
20 | },
21 | "eslintConfig": {
22 | "extends": [
23 | "react-app",
24 | "react-app/jest"
25 | ]
26 | },
27 | "browserslist": {
28 | "production": [
29 | ">0.2%",
30 | "not dead",
31 | "not op_mini all"
32 | ],
33 | "development": [
34 | "last 1 chrome version",
35 | "last 1 firefox version",
36 | "last 1 safari version"
37 | ]
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thapatechnical/addtocartreact/9661ee52cd2edf4adc046a627c1bac637480bfee/public/favicon.ico
--------------------------------------------------------------------------------
/public/images/add.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thapatechnical/addtocartreact/9661ee52cd2edf4adc046a627c1bac637480bfee/public/images/add.png
--------------------------------------------------------------------------------
/public/images/arrow.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thapatechnical/addtocartreact/9661ee52cd2edf4adc046a627c1bac637480bfee/public/images/arrow.png
--------------------------------------------------------------------------------
/public/images/cart.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thapatechnical/addtocartreact/9661ee52cd2edf4adc046a627c1bac637480bfee/public/images/cart.png
--------------------------------------------------------------------------------
/public/images/decr.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thapatechnical/addtocartreact/9661ee52cd2edf4adc046a627c1bac637480bfee/public/images/decr.png
--------------------------------------------------------------------------------
/public/images/delete.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thapatechnical/addtocartreact/9661ee52cd2edf4adc046a627c1bac637480bfee/public/images/delete.png
--------------------------------------------------------------------------------
/public/images/incr.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thapatechnical/addtocartreact/9661ee52cd2edf4adc046a627c1bac637480bfee/public/images/incr.png
--------------------------------------------------------------------------------
/public/images/minus.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thapatechnical/addtocartreact/9661ee52cd2edf4adc046a627c1bac637480bfee/public/images/minus.png
--------------------------------------------------------------------------------
/public/images/remove.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thapatechnical/addtocartreact/9661ee52cd2edf4adc046a627c1bac637480bfee/public/images/remove.png
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
21 |
30 | React App
31 |
32 |
33 | You need to enable JavaScript to run this app.
34 |
35 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thapatechnical/addtocartreact/9661ee52cd2edf4adc046a627c1bac637480bfee/src/App.css
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import React from "react";
2 | // import Cart from "./component/Cart";
3 | import Cart from "./components/Cart";
4 |
5 | const App = () => {
6 | return (
7 | <>
8 |
9 | >
10 | );
11 | };
12 |
13 | export default App;
14 |
--------------------------------------------------------------------------------
/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/component/Cart.js:
--------------------------------------------------------------------------------
1 | import React, { createContext, useContext, useReducer } from "react";
2 | import "./cart.css";
3 | import { products } from "./product";
4 | import ContextCart from "./ContextCart";
5 | import reducer from "./reducer";
6 |
7 | // create a context
8 | export const CartContext = createContext();
9 |
10 | const initialState = {
11 | item: products,
12 | totalAmount: 25600,
13 | totalItems: 0,
14 | quantity: 1,
15 | };
16 |
17 | const Cart = () => {
18 | // inPlace of useState we will use the useReducer Hook
19 | // const [item, setItem] = useState(products);
20 |
21 | const [state, dispatch] = useReducer(reducer, initialState);
22 |
23 | const clearCart = () => {
24 | return dispatch({ type: "CLEAR_CART" });
25 | };
26 |
27 | const removeItem = (id) => {
28 | return dispatch({
29 | type: "REMOVE_ITEM",
30 | payload: id,
31 | });
32 | };
33 |
34 | const increment = (id) => {
35 | return dispatch({
36 | type: "INCREMENT",
37 | payload: id,
38 | });
39 | };
40 |
41 | const decrement = (id) => {
42 | return dispatch({
43 | type: "DECREMENT",
44 | payload: id,
45 | });
46 | };
47 |
48 | return (
49 | <>
50 |
52 |
53 |
54 | >
55 | );
56 | };
57 |
58 | // custom Hook
59 | export const useGlobalContext = () => {
60 | return useContext(CartContext);
61 | };
62 |
63 | export default Cart;
64 |
--------------------------------------------------------------------------------
/src/component/ContextCart.js:
--------------------------------------------------------------------------------
1 | import React, { useContext } from "react";
2 | import Items from "./Items";
3 | import Navbar from "./Navbar";
4 | import { Scrollbars } from "react-custom-scrollbars-2";
5 | import { CartContext } from "./Cart";
6 |
7 | const ContextCart = () => {
8 | // cosumer
9 | const { item, totalAmount, totalItems, clearCart } = useContext(CartContext);
10 | {
11 | if (item.length === 0) {
12 | return (
13 | <>
14 |
15 |
16 | shopping Cart
17 |
18 | you have {totalItems}
19 | items in shopping cart
20 |
21 |
22 |
23 |
24 |
25 | Empty Cart
26 |
27 |
28 |
29 |
30 | >
31 | );
32 | } else {
33 | return (
34 | <>
35 |
36 |
37 | shopping Cart
38 |
39 | you have {totalItems}
40 | items in shopping cart
41 |
42 |
43 |
44 |
45 |
46 | {item.map((curItem) => {
47 | return ;
48 | })}
49 |
50 |
51 |
52 |
53 |
54 | card total: {totalAmount}₹
55 |
56 | CheckOut
57 | Clear Cart
58 |
59 |
60 | >
61 | );
62 | }
63 | }
64 | };
65 |
66 | export default ContextCart;
67 |
--------------------------------------------------------------------------------
/src/component/Items.js:
--------------------------------------------------------------------------------
1 | import React, { useContext } from "react";
2 | import { CartContext } from "./Cart";
3 |
4 | const Items = ({ id, title, description, price, img, quantity }) => {
5 | const { removeItem, increment, decrement } = useContext(CartContext);
6 | return (
7 | <>
8 |
9 |
10 |
11 |
12 |
13 |
14 |
{title}
15 |
{description}
16 |
17 |
18 | decrement(id)}>
19 |
20 | increment(id)}>
21 |
22 |
23 |
{price}
24 |
25 |
26 | removeItem(id)}>
29 |
30 |
31 |
32 | >
33 | );
34 | };
35 |
36 | export default Items;
37 |
--------------------------------------------------------------------------------
/src/component/Navbar.js:
--------------------------------------------------------------------------------
1 | import React, { useContext } from "react";
2 | import { CartContext } from "./Cart";
3 |
4 | const Navbar = () => {
5 | const { totalItems } = useContext(CartContext);
6 | return (
7 | <>
8 |
9 |
10 |
11 |
continue shoping
12 |
13 |
14 |
15 |
16 |
{totalItems}
17 |
18 |
19 | >
20 | );
21 | };
22 |
23 | export default Navbar;
24 |
--------------------------------------------------------------------------------
/src/component/cart.css:
--------------------------------------------------------------------------------
1 | @import url("https://fonts.googleapis.com/css2?family=Mulish:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;1,200&display=swap");
2 |
3 | * {
4 | margin: 0;
5 | padding: 0;
6 | box-sizing: border-box;
7 | font-family: Mulish;
8 | }
9 |
10 | html {
11 | font-size: 55.5%;
12 | }
13 |
14 | :root {
15 | --main-color: #333;
16 | --primary-color: #acacac;
17 | --icon-color: #525252;
18 | }
19 |
20 | i {
21 | width: 2rem;
22 | height: 2rem;
23 | }
24 |
25 | .fa,
26 | .fas {
27 | font-weight: 900;
28 | display: grid;
29 | place-items: center;
30 | font-size: 1.6rem;
31 | cursor: pointer;
32 | }
33 |
34 | body {
35 | width: 80%;
36 | height: 100vh;
37 | margin: auto;
38 | }
39 |
40 | header {
41 | width: 100%;
42 | line-height: 10rem;
43 | display: grid;
44 | grid-template-columns: 1fr 1fr;
45 | border-bottom: 0.1rem solid #9a9191;
46 | transform: rotate(-0.05deg);
47 | margin-bottom: 7rem;
48 | }
49 |
50 | .continue-shopping {
51 | display: flex;
52 | height: inherit;
53 | align-items: center;
54 | }
55 |
56 | .continue-shopping .arrow-icon {
57 | width: 2.2rem;
58 | height: 2.2rem;
59 | }
60 |
61 | .continue-shopping h3 {
62 | font-size: 2rem;
63 | text-transform: capitalize;
64 | color: var(--main-color);
65 | margin-left: 1.5rem;
66 | font-weight: 400;
67 | }
68 |
69 | .cart-icon {
70 | display: flex;
71 | text-align: end;
72 | margin-right: 2rem;
73 | align-items: center;
74 | position: relative;
75 | justify-content: flex-end;
76 | }
77 |
78 | .cart-icon img {
79 | width: 5rem;
80 | height: 5rem;
81 | color: #2f80ed;
82 | }
83 |
84 | .cart-icon p {
85 | position: absolute;
86 | width: 4rem;
87 | height: 4rem;
88 | right: -1.2rem;
89 | top: 0.9rem;
90 | border-radius: 50%;
91 | background: #99cbf7;
92 | color: #fff;
93 | box-sizing: border-box;
94 | font-size: 1.6rem;
95 | display: flex;
96 | justify-content: center;
97 | align-items: center;
98 | color: var(--main-color);
99 | }
100 |
101 | .main-cart-section h1 {
102 | font-style: normal;
103 | font-weight: bold;
104 | font-size: 3rem;
105 | text-transform: capitalize;
106 | color: rgba(41, 41, 41, 1);
107 | margin-bottom: 1rem;
108 | }
109 |
110 | .main-cart-section .total-items {
111 | font-style: normal;
112 | font-weight: normal;
113 | font-size: 1.8rem;
114 | text-transform: capitalize;
115 | color: var(--main-color);
116 | margin-bottom: 5rem;
117 | }
118 |
119 | .total-items-count {
120 | font-weight: bold;
121 | color: var(--main-color);
122 | }
123 |
124 | /* cart main div start */
125 |
126 | .main-cart-section .cart-items {
127 | width: 100%;
128 | height: 62rem;
129 | background: linear-gradient(
130 | 103.49deg,
131 | #ffffff -28.13%,
132 | rgba(242, 247, 255, 0.5) 116.84%
133 | );
134 | box-shadow: rgba(0, 0, 0, 0.08) 0rem 0.4rem 1.2rem;
135 | border-radius: 2rem;
136 | margin: auto;
137 | margin: auto;
138 | display: grid;
139 | place-items: center;
140 | }
141 |
142 | .cart-items hr {
143 | margin: 2rem auto;
144 | overflow-y: scroll;
145 | }
146 |
147 | .cart-items-container {
148 | width: 90%;
149 | height: 56rem;
150 | }
151 |
152 | .items-info {
153 | width: 100%;
154 | height: 11rem;
155 | /* background-color: lavender; */
156 | margin: auto;
157 | display: grid;
158 | grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
159 | }
160 |
161 | .items-info .product-img img {
162 | width: 16rem;
163 | height: 11rem;
164 | filter: drop-shadow(0rem 0.4rem 1rem #f1f7ff);
165 | border-radius: 1rem;
166 | }
167 |
168 | .items-info .title {
169 | display: flex;
170 | justify-content: center;
171 | flex-direction: column;
172 | }
173 |
174 | .items-info .title h2 {
175 | font-style: normal;
176 | font-weight: bold;
177 | font-size: 2.2rem;
178 | display: flex;
179 | align-items: center;
180 | text-transform: capitalize;
181 | color: var(--main-color);
182 | }
183 |
184 | .items-info .title p {
185 | font-style: normal;
186 | font-weight: normal;
187 | font-size: 1.8rem;
188 | display: flex;
189 | align-items: center;
190 | text-transform: capitalize;
191 | text-align: left;
192 | color: var(--primary-color);
193 | }
194 |
195 | .add-minus-quantity {
196 | display: flex;
197 | justify-content: flex-end;
198 | align-items: center;
199 | }
200 |
201 | .add-minus-quantity button {
202 | border: none;
203 | background-color: transparent;
204 | outline: none;
205 | cursor: pointer;
206 | }
207 |
208 | .add-minus-quantity input {
209 | width: 6rem;
210 | height: 3rem;
211 | border: 0.141rem solid var(--primary-color);
212 | box-sizing: border-box;
213 | font-style: normal;
214 | font-weight: normal;
215 | font-size: 1.6rem;
216 | text-align: center;
217 | text-transform: capitalize;
218 | color: var(--primary-color);
219 | margin: 0 1rem;
220 | border-radius: 0.5rem;
221 | outline: none;
222 | background-color: transparent;
223 | }
224 |
225 | .add-minus-quantity input:focus {
226 | outline: none;
227 | }
228 |
229 | .price {
230 | display: flex;
231 | justify-content: flex-end;
232 | align-items: center;
233 | }
234 |
235 | .price h3 {
236 | font-style: normal;
237 | font-weight: bold;
238 | font-size: 2rem;
239 | text-transform: capitalize;
240 | color: var(--main-color);
241 | }
242 |
243 | .remove-item {
244 | display: flex;
245 | justify-content: flex-end;
246 | align-items: center;
247 | margin-right: 5rem;
248 | }
249 |
250 | .remove-item button {
251 | border: none;
252 | background-color: transparent;
253 | }
254 |
255 | .card-total {
256 | width: 95%;
257 | margin-top: 4rem;
258 | text-align: right;
259 | }
260 |
261 | .card-total h3 {
262 | font-style: 200;
263 | font-size: 2.58rem;
264 | line-height: 3.2rem;
265 | text-transform: capitalize;
266 | color: #606166;
267 | }
268 |
269 | .card-total h3 span {
270 | font-style: normal;
271 | font-weight: bold;
272 | font-size: 2.8rem;
273 | line-height: 3.2rem;
274 | text-transform: capitalize;
275 | color: #000000;
276 | margin-left: 1rem;
277 | }
278 |
279 | .card-total button {
280 | border: none;
281 | font-size: 1.6rem;
282 | padding: 1rem 3rem;
283 | color: #fff;
284 | background-color: #349bf3;
285 | text-transform: uppercase;
286 | margin-top: 1rem;
287 | border-radius: 0.5rem;
288 | cursor: pointer;
289 | letter-spacing: 1px;
290 | }
291 |
292 | /* hover effects */
293 | .fa-trash-alt {
294 | color: #ed4848;
295 | }
296 |
297 | .fa-plus:hover {
298 | color: #42c157;
299 | }
300 |
301 | .fa-minus:hover {
302 | color: #ffb800;
303 | }
304 |
305 | .fa-plus,
306 | .fa-minus {
307 | color: var(--icon-color);
308 | }
309 |
310 | .card-total button:hover {
311 | background-color: #0077dc;
312 | }
313 |
314 | /* responsive media queries */
315 |
316 | @media (max-width: 968px) {
317 | html {
318 | font-size: 50%;
319 | }
320 | }
321 |
322 | @media (max-width: 768px) {
323 | .continue-shopping h3 {
324 | margin-left: 0;
325 | }
326 |
327 | .items-info {
328 | width: 100%;
329 | height: auto;
330 | display: grid;
331 | grid-template-columns: 1fr;
332 | }
333 |
334 | .title,
335 | .add-minus-quantity,
336 | .price,
337 | .remove-item {
338 | padding-left: 2rem;
339 | }
340 |
341 | .items-info .product-img {
342 | width: 100%;
343 | text-align: center;
344 | margin-bottom: 2rem;
345 | }
346 |
347 | .add-minus-quantity {
348 | margin: 2rem 0 2rem 0;
349 | justify-content: flex-start;
350 | }
351 |
352 | .price {
353 | justify-content: flex-start;
354 | margin-bottom: 2rem;
355 | }
356 |
357 | .price h3::before {
358 | content: "Price: ";
359 | }
360 |
361 | .remove-item {
362 | justify-content: flex-start;
363 | }
364 |
365 | .card-total {
366 | margin-bottom: 2rem;
367 | text-align: center;
368 | }
369 |
370 | .card-total button {
371 | margin-bottom: 3rem;
372 | }
373 | }
374 |
--------------------------------------------------------------------------------
/src/component/product.js:
--------------------------------------------------------------------------------
1 | export const products = [
2 | {
3 | id: 1,
4 | title: "Samsung S21",
5 | description: "black in color",
6 | price: "2500₹",
7 | img: "https://images.pexels.com/photos/404280/pexels-photo-404280.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
8 | quantity: 1,
9 | },
10 | {
11 | id: 2,
12 | title: "Samsung M21",
13 | description: "white in color",
14 | price: "2300₹",
15 | img: "https://images.pexels.com/photos/47261/pexels-photo-47261.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
16 | quantity: 1,
17 | },
18 | {
19 | id: 3,
20 | title: "Redmi 9",
21 | description: "black in color",
22 | price: "3500₹",
23 | img: "https://images-na.ssl-images-amazon.com/images/I/71A9Vo1BatL._SL1500_.jpg",
24 | quantity: 1,
25 | },
26 | {
27 | id: 4,
28 | title: "Iphone 12",
29 | description: "Best mobile ever",
30 | price: "90500₹",
31 | img: "https://images-na.ssl-images-amazon.com/images/I/71hIfcIPyxS._SL1500_.jpg",
32 | quantity: 1,
33 | },
34 | {
35 | id: 5,
36 | title: "Samsung S21",
37 | description: "black in color",
38 | price: "2500₹",
39 | img: "https://images.pexels.com/photos/404280/pexels-photo-404280.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
40 | quantity: 1,
41 | },
42 | {
43 | id: 6,
44 | title: "Redmi 9",
45 | description: "black in color",
46 | price: "3500₹",
47 | img: "https://images-na.ssl-images-amazon.com/images/I/71A9Vo1BatL._SL1500_.jpg",
48 | quantity: 1,
49 | },
50 | {
51 | id: 7,
52 | title: "Samsung S21",
53 | description: "black in color",
54 | price: "2500₹",
55 | img: "https://images.pexels.com/photos/404280/pexels-photo-404280.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
56 | quantity: 1,
57 | },
58 | {
59 | id: 8,
60 | title: "Iphone 12",
61 | description: "Best mobile ever",
62 | price: "90500₹",
63 | img: "https://images-na.ssl-images-amazon.com/images/I/71hIfcIPyxS._SL1500_.jpg",
64 | quantity: 1,
65 | },
66 | {
67 | id: 9,
68 | title: "Samsung S21",
69 | description: "black in color",
70 | price: "2500₹",
71 | img: "https://images.pexels.com/photos/404280/pexels-photo-404280.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
72 | quantity: 1,
73 | },
74 | ];
75 |
--------------------------------------------------------------------------------
/src/component/reducer.js:
--------------------------------------------------------------------------------
1 | const reducer = (state, action) => {
2 | if (action.type === "CLEAR_CART") {
3 | return { ...state, item: [] };
4 | }
5 |
6 | if (action.type === "REMOVE_ITEM") {
7 | return {
8 | ...state,
9 | item: state.item.filter((curElem) => {
10 | return curElem.id !== action.payload;
11 | }),
12 | };
13 | }
14 |
15 | if (action.type === "INCREMENT") {
16 | // we need to find out which item is clicked
17 | let updatedCart = state.item.map((curElem) => {
18 | if (curElem.id === action.payload) {
19 | return { ...curElem, quantity: curElem.quantity + 1 };
20 | }
21 | return curElem;
22 | });
23 | return { ...state, item: updatedCart };
24 | }
25 |
26 | if (action.type === "DECREMENT") {
27 | // we need to find out which item is clicked
28 | let updatedCart = state.item
29 | .map((curElem) => {
30 | if (curElem.id === action.payload) {
31 | return { ...curElem, quantity: curElem.quantity - 1 };
32 | }
33 | return curElem;
34 | })
35 | .filter((curElem) => curElem.quantity !== 0);
36 | return { ...state, item: updatedCart };
37 | }
38 |
39 | return state;
40 | };
41 |
42 | export default reducer;
43 |
--------------------------------------------------------------------------------
/src/components/Cart.js:
--------------------------------------------------------------------------------
1 | import React, { createContext, useReducer, useEffect } from "react";
2 | import "./cart.css";
3 | import { products } from "./products";
4 | import ContextCart from "./ContextCart";
5 | import { reducer } from "./reducer";
6 |
7 | export const CartContext = createContext();
8 |
9 | const initialState = {
10 | item: products,
11 | totalAmount: 0,
12 | totalItem: 0,
13 | };
14 |
15 | const Cart = () => {
16 | // const [item, setItem] = useState(products);
17 | const [state, dispatch] = useReducer(reducer, initialState);
18 |
19 | // to delete the indv. elements from an Item Cart
20 | const removeItem = (id) => {
21 | return dispatch({
22 | type: "REMOVE_ITEM",
23 | payload: id,
24 | });
25 | };
26 |
27 | // clear the cart
28 | const clearCart = () => {
29 | return dispatch({ type: "CLEAR_CART" });
30 | };
31 |
32 | // increment the item
33 | const increment = (id) => {
34 | return dispatch({
35 | type: "INCREMENT",
36 | payload: id,
37 | });
38 | };
39 |
40 | // decrement the item
41 | const decrement = (id) => {
42 | return dispatch({
43 | type: "DECREMENT",
44 | payload: id,
45 | });
46 | };
47 |
48 | // we will use the useEffect to update the data
49 | useEffect(() => {
50 | dispatch({ type: "GET_TOTAL" });
51 | // console.log("Awesome");
52 | }, [state.item]);
53 |
54 | return (
55 |
57 |
58 |
59 | );
60 | };
61 |
62 | export default Cart;
63 |
--------------------------------------------------------------------------------
/src/components/ContextCart.js:
--------------------------------------------------------------------------------
1 | import React, { useContext } from "react";
2 | import { Scrollbars } from "react-custom-scrollbars-2";
3 | import Items from "./Items";
4 | import { CartContext } from "./Cart";
5 |
6 | const ContextCart = () => {
7 | const { item, clearCart, totalItem, totalAmount } = useContext(CartContext);
8 |
9 | if (item.length === 0) {
10 | return (
11 | <>
12 |
13 |
14 |
15 |
continue shopping
16 |
17 |
18 |
19 |
20 |
{totalItem}
21 |
22 |
23 |
24 |
25 | shopping Cart
26 |
27 | you have {totalItem} {" "}
28 | items in shopping cart
29 |
30 |
31 | >
32 | );
33 | }
34 |
35 | return (
36 | <>
37 |
38 |
39 |
40 |
continue shopping
41 |
42 |
43 |
44 |
45 |
{totalItem}
46 |
47 |
48 |
49 |
50 | shopping Cart
51 |
52 | you have {totalItem} items
53 | in shopping cart
54 |
55 |
56 |
57 |
58 |
59 | {item.map((curItem) => {
60 | return ;
61 | })}
62 |
63 |
64 |
65 |
66 |
67 |
68 | Cart Total : {totalAmount}₹
69 |
70 | checkout
71 |
72 | Clear Cart
73 |
74 |
75 |
76 | >
77 | );
78 | };
79 |
80 | export default ContextCart;
81 |
--------------------------------------------------------------------------------
/src/components/Items.js:
--------------------------------------------------------------------------------
1 | import React, { useContext } from "react";
2 | import { CartContext } from "./Cart";
3 |
4 | const Items = ({ id, description, title, img, price, quantity }) => {
5 | const { removeItem, increment, decrement } = useContext(CartContext);
6 |
7 | return (
8 | <>
9 |
10 |
11 |
12 |
13 |
14 |
15 |
{title}
16 |
{description}
17 |
18 |
19 |
20 | decrement(id)}>
21 |
22 | increment(id)}>
23 |
24 |
25 |
26 |
{price}₹
27 |
28 |
29 |
30 | removeItem(id)}>
33 |
34 |
35 |
36 |
37 | >
38 | );
39 | };
40 |
41 | export default Items;
42 |
--------------------------------------------------------------------------------
/src/components/cart.css:
--------------------------------------------------------------------------------
1 | @import url("https://fonts.googleapis.com/css2?family=Mulish:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;1,200&display=swap");
2 |
3 | * {
4 | margin: 0;
5 | padding: 0;
6 | box-sizing: border-box;
7 | font-family: Mulish;
8 | }
9 |
10 | html {
11 | font-size: 55.5%;
12 | }
13 |
14 | :root {
15 | --main-color: #333;
16 | --primary-color: #acacac;
17 | --icon-color: #525252;
18 | }
19 |
20 | i {
21 | width: 2rem;
22 | height: 2rem;
23 | }
24 |
25 | .fa,
26 | .fas {
27 | font-weight: 900;
28 | display: grid;
29 | place-items: center;
30 | font-size: 1.6rem;
31 | cursor: pointer;
32 | }
33 |
34 | body {
35 | width: 80%;
36 | height: 100vh;
37 | margin: auto;
38 | }
39 |
40 | header {
41 | width: 100%;
42 | line-height: 10rem;
43 | display: grid;
44 | grid-template-columns: 1fr 1fr;
45 | border-bottom: 0.1rem solid #9a9191;
46 | transform: rotate(-0.05deg);
47 | margin-bottom: 7rem;
48 | }
49 |
50 | .continue-shopping {
51 | display: flex;
52 | height: inherit;
53 | align-items: center;
54 | }
55 |
56 | .continue-shopping .arrow-icon {
57 | width: 2.2rem;
58 | height: 2.2rem;
59 | }
60 |
61 | .continue-shopping h3 {
62 | font-size: 2rem;
63 | text-transform: capitalize;
64 | color: var(--main-color);
65 | margin-left: 1.5rem;
66 | font-weight: 400;
67 | }
68 |
69 | .cart-icon {
70 | display: flex;
71 | text-align: end;
72 | margin-right: 2rem;
73 | align-items: center;
74 | position: relative;
75 | justify-content: flex-end;
76 | }
77 |
78 | .cart-icon img {
79 | width: 5rem;
80 | height: 5rem;
81 | color: #2f80ed;
82 | }
83 |
84 | .cart-icon p {
85 | position: absolute;
86 | width: 4rem;
87 | height: 4rem;
88 | right: -1.2rem;
89 | top: 0.9rem;
90 | border-radius: 50%;
91 | background: #99cbf7;
92 | color: #fff;
93 | box-sizing: border-box;
94 | font-size: 1.6rem;
95 | display: flex;
96 | justify-content: center;
97 | align-items: center;
98 | color: var(--main-color);
99 | }
100 |
101 | .main-cart-section h1 {
102 | font-style: normal;
103 | font-weight: bold;
104 | font-size: 3rem;
105 | text-transform: capitalize;
106 | color: rgba(41, 41, 41, 1);
107 | margin-bottom: 1rem;
108 | }
109 |
110 | .main-cart-section .total-items {
111 | font-style: normal;
112 | font-weight: normal;
113 | font-size: 1.8rem;
114 | text-transform: capitalize;
115 | color: var(--main-color);
116 | margin-bottom: 5rem;
117 | }
118 |
119 | .total-items-count {
120 | font-weight: bold;
121 | color: var(--main-color);
122 | }
123 |
124 | /* cart main div start */
125 |
126 | .main-cart-section .cart-items {
127 | width: 100%;
128 | height: 62rem;
129 | background: linear-gradient(
130 | 103.49deg,
131 | #ffffff -28.13%,
132 | rgba(242, 247, 255, 0.5) 116.84%
133 | );
134 | box-shadow: rgba(0, 0, 0, 0.08) 0rem 0.4rem 1.2rem;
135 | border-radius: 2rem;
136 | margin: auto;
137 | margin: auto;
138 | display: grid;
139 | place-items: center;
140 | }
141 |
142 | .cart-items hr {
143 | margin: 2rem auto;
144 | overflow-y: scroll;
145 | }
146 |
147 | .cart-items-container {
148 | width: 90%;
149 | height: 56rem;
150 | }
151 |
152 | .items-info {
153 | width: 100%;
154 | height: 11rem;
155 | /* background-color: lavender; */
156 | margin: auto;
157 | display: grid;
158 | grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
159 | }
160 |
161 | .items-info .product-img img {
162 | width: 16rem;
163 | height: 11rem;
164 | filter: drop-shadow(0rem 0.4rem 1rem #f1f7ff);
165 | border-radius: 1rem;
166 | }
167 |
168 | .items-info .title {
169 | display: flex;
170 | justify-content: center;
171 | flex-direction: column;
172 | }
173 |
174 | .items-info .title h2 {
175 | font-style: normal;
176 | font-weight: bold;
177 | font-size: 2.2rem;
178 | display: flex;
179 | align-items: center;
180 | text-transform: capitalize;
181 | color: var(--main-color);
182 | }
183 |
184 | .items-info .title p {
185 | font-style: normal;
186 | font-weight: normal;
187 | font-size: 1.8rem;
188 | display: flex;
189 | align-items: center;
190 | text-transform: capitalize;
191 | text-align: left;
192 | color: var(--primary-color);
193 | }
194 |
195 | .add-minus-quantity {
196 | display: flex;
197 | justify-content: flex-end;
198 | align-items: center;
199 | }
200 |
201 | .add-minus-quantity button {
202 | border: none;
203 | background-color: transparent;
204 | outline: none;
205 | cursor: pointer;
206 | }
207 |
208 | .add-minus-quantity input {
209 | width: 6rem;
210 | height: 3rem;
211 | border: 0.141rem solid var(--primary-color);
212 | box-sizing: border-box;
213 | font-style: normal;
214 | font-weight: normal;
215 | font-size: 1.6rem;
216 | text-align: center;
217 | text-transform: capitalize;
218 | color: var(--primary-color);
219 | margin: 0 1rem;
220 | border-radius: 0.5rem;
221 | outline: none;
222 | background-color: transparent;
223 | }
224 |
225 | .add-minus-quantity input:focus {
226 | outline: none;
227 | }
228 |
229 | .price {
230 | display: flex;
231 | justify-content: flex-end;
232 | align-items: center;
233 | }
234 |
235 | .price h3 {
236 | font-style: normal;
237 | font-weight: bold;
238 | font-size: 2rem;
239 | text-transform: capitalize;
240 | color: var(--main-color);
241 | }
242 |
243 | .remove-item {
244 | display: flex;
245 | justify-content: flex-end;
246 | align-items: center;
247 | margin-right: 5rem;
248 | }
249 |
250 | .remove-item button {
251 | border: none;
252 | background-color: transparent;
253 | }
254 |
255 | .card-total {
256 | width: 95%;
257 | margin-top: 4rem;
258 | text-align: right;
259 | }
260 |
261 | .card-total h3 {
262 | font-style: 200;
263 | font-size: 2.58rem;
264 | line-height: 3.2rem;
265 | text-transform: capitalize;
266 | color: #606166;
267 | }
268 |
269 | .card-total h3 span {
270 | font-style: normal;
271 | font-weight: bold;
272 | font-size: 2.8rem;
273 | line-height: 3.2rem;
274 | text-transform: capitalize;
275 | color: #000000;
276 | margin-left: 1rem;
277 | }
278 |
279 | .card-total button {
280 | border: none;
281 | font-size: 1.6rem;
282 | padding: 1rem 3rem;
283 | color: #fff;
284 | background-color: #349bf3;
285 | text-transform: uppercase;
286 | margin-top: 1rem;
287 | border-radius: 0.5rem;
288 | cursor: pointer;
289 | letter-spacing: 1px;
290 | }
291 |
292 | /* hover effects */
293 | .fa-trash-alt {
294 | color: #ed4848;
295 | }
296 |
297 | .fa-plus:hover {
298 | color: #42c157;
299 | }
300 |
301 | .fa-minus:hover {
302 | color: #ffb800;
303 | }
304 |
305 | .fa-plus,
306 | .fa-minus {
307 | color: var(--icon-color);
308 | }
309 |
310 | .card-total button:hover {
311 | background-color: #0077dc;
312 | }
313 |
314 | .card-total .clear-cart {
315 | margin-left: 3rem;
316 | background-color: rgb(209, 61, 61);
317 | }
318 |
319 | .card-total .clear-cart:hover {
320 | background-color: rgb(197, 5, 5);
321 | }
322 |
323 | /* responsive media queries */
324 |
325 | @media (max-width: 968px) {
326 | html {
327 | font-size: 50%;
328 | }
329 | }
330 |
331 | @media (max-width: 768px) {
332 | .continue-shopping h3 {
333 | margin-left: 0;
334 | font-size: 1.85rem;
335 | }
336 |
337 | .items-info {
338 | width: 100%;
339 | height: auto;
340 | display: grid;
341 | grid-template-columns: 1fr;
342 | }
343 |
344 | .title,
345 | .add-minus-quantity,
346 | .price,
347 | .remove-item {
348 | padding-left: 2rem;
349 | }
350 |
351 | .items-info .product-img {
352 | width: 100%;
353 | text-align: center;
354 | margin-bottom: 2rem;
355 | }
356 |
357 | .add-minus-quantity {
358 | margin: 2rem 0 2rem 0;
359 | justify-content: flex-start;
360 | }
361 |
362 | .price {
363 | justify-content: flex-start;
364 | margin-bottom: 2rem;
365 | }
366 |
367 | .price h3::before {
368 | content: "Price: ";
369 | }
370 |
371 | .remove-item {
372 | justify-content: flex-start;
373 | }
374 |
375 | .card-total {
376 | margin-bottom: 2rem;
377 | text-align: center;
378 | }
379 |
380 | .card-total button {
381 | margin-bottom: 3rem;
382 | }
383 | }
384 |
--------------------------------------------------------------------------------
/src/components/products.js:
--------------------------------------------------------------------------------
1 | export const products = [
2 | {
3 | id: 1,
4 | title: "Samsung S21",
5 | description: "black in color",
6 | price: "2500",
7 | img: "https://images.pexels.com/photos/404280/pexels-photo-404280.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
8 | quantity: 1,
9 | },
10 | {
11 | id: 2,
12 | title: "Samsung M21",
13 | description: "white in color",
14 | price: "2300",
15 | img: "https://images.pexels.com/photos/47261/pexels-photo-47261.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
16 | quantity: 1,
17 | },
18 | {
19 | id: 3,
20 | title: "Redmi 9",
21 | description: "black in color",
22 | price: "3500",
23 | img: "https://images-na.ssl-images-amazon.com/images/I/71A9Vo1BatL._SL1500_.jpg",
24 | quantity: 1,
25 | },
26 | {
27 | id: 4,
28 | title: "Iphone 12",
29 | description: "Best mobile ever",
30 | price: "90500",
31 | img: "https://images-na.ssl-images-amazon.com/images/I/71hIfcIPyxS._SL1500_.jpg",
32 | quantity: 1,
33 | },
34 | {
35 | id: 5,
36 | title: "Samsung S21",
37 | description: "black in color",
38 | price: "2500",
39 | img: "https://images.pexels.com/photos/404280/pexels-photo-404280.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
40 | quantity: 1,
41 | },
42 | {
43 | id: 6,
44 | title: "Redmi 9",
45 | description: "black in color",
46 | price: "3500",
47 | img: "https://images-na.ssl-images-amazon.com/images/I/71A9Vo1BatL._SL1500_.jpg",
48 | quantity: 1,
49 | },
50 | {
51 | id: 7,
52 | title: "Samsung S21",
53 | description: "black in color",
54 | price: "2500",
55 | img: "https://images.pexels.com/photos/404280/pexels-photo-404280.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
56 | quantity: 1,
57 | },
58 | {
59 | id: 8,
60 | title: "Iphone 12",
61 | description: "Best mobile ever",
62 | price: "90500",
63 | img: "https://images-na.ssl-images-amazon.com/images/I/71hIfcIPyxS._SL1500_.jpg",
64 | quantity: 1,
65 | },
66 | {
67 | id: 9,
68 | title: "Samsung S21",
69 | description: "black in color",
70 | price: "2500",
71 | img: "https://images.pexels.com/photos/404280/pexels-photo-404280.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
72 | quantity: 1,
73 | },
74 | ];
75 |
--------------------------------------------------------------------------------
/src/components/reducer.js:
--------------------------------------------------------------------------------
1 | export const reducer = (state, action) => {
2 | if (action.type === "REMOVE_ITEM") {
3 | return {
4 | ...state,
5 | item: state.item.filter((curElem) => {
6 | return curElem.id !== action.payload;
7 | }),
8 | };
9 | }
10 |
11 | if (action.type === "CLEAR_CART") {
12 | return { ...state, item: [] };
13 | }
14 |
15 | if (action.type === "INCREMENT") {
16 | const updatedCart = state.item.map((curElem) => {
17 | if (curElem.id === action.payload) {
18 | return { ...curElem, quantity: curElem.quantity + 1 };
19 | }
20 | return curElem;
21 | });
22 |
23 | return { ...state, item: updatedCart };
24 | }
25 |
26 | if (action.type === "DECREMENT") {
27 | const updatedCart = state.item
28 | .map((curElem) => {
29 | if (curElem.id === action.payload) {
30 | return { ...curElem, quantity: curElem.quantity - 1 };
31 | }
32 | return curElem;
33 | })
34 | .filter((curElem) => curElem.quantity !== 0);
35 | return { ...state, item: updatedCart };
36 | }
37 |
38 | if (action.type === "GET_TOTAL") {
39 | let { totalItem, totalAmount } = state.item.reduce(
40 | (accum, curVal) => {
41 | let { price, quantity } = curVal;
42 |
43 | let updatedTotalAmount = price * quantity;
44 | accum.totalAmount += updatedTotalAmount;
45 |
46 | accum.totalItem += quantity;
47 | return accum;
48 | },
49 | {
50 | totalItem: 0,
51 | totalAmount: 0,
52 | }
53 | );
54 | return { ...state, totalItem, totalAmount };
55 | }
56 | return state;
57 | };
58 |
--------------------------------------------------------------------------------
/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 | }
9 |
10 | code {
11 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
12 | monospace;
13 | }
14 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import './index.css';
4 | import App from './App';
5 | import reportWebVitals from './reportWebVitals';
6 |
7 | ReactDOM.render(
8 |
9 |
10 | ,
11 | document.getElementById('root')
12 | );
13 |
14 | // If you want to start measuring performance in your app, pass a function
15 | // to log results (for example: reportWebVitals(console.log))
16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
17 | reportWebVitals();
18 |
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/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/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 |
--------------------------------------------------------------------------------