├── css
└── style.css
├── js
└── app.js
└── index.html
/css/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | overflow-x: hidden;
3 | font-family: "Montserrat", sans-serif;
4 | }
5 | .search-field {
6 | border: none;
7 | border-radius: 50px;
8 | padding: 5px;
9 | outline: none;
10 | }
11 | .search-field:hover {
12 | outline: none;
13 | border: none;
14 | }
15 | .search-box {
16 | border: 1px solid rgb(3, 118, 211);
17 | width: 281px;
18 | border-radius: 5px;
19 | margin: auto;
20 | margin-top: 50px;
21 | border-right: none;
22 | }
23 | .search-button {
24 | padding: 10px 50px;
25 | border-radius: 25px;
26 | margin-left: 20px;
27 | }
28 | #all-products {
29 | display: grid;
30 | grid-template-columns: repeat(3, 1fr);
31 | /* border: 1px solid black; */
32 | text-align: center;
33 | width: 100%;
34 | margin: auto;
35 | align-items: center;
36 | justify-content: center;
37 | }
38 | .single-product {
39 | padding: 10px;
40 | transition: 0.6s;
41 | }
42 | .single-product:hover {
43 | box-shadow: 4px 4px 7px gray;
44 | border-radius: 10px;
45 | border: 1px solid cyan;
46 | }
47 | .product-image {
48 | width: 150px;
49 | height: 150px;
50 | }
51 | .cart {
52 | width: 350px;
53 | /* border: 1px solid green; */
54 | padding: 10px;
55 | box-shadow: 10px 10px 20px gray;
56 | border-radius: 5px;
57 | margin-right: 30px;
58 | position: fixed;
59 | }
60 | .cart-main {
61 | padding-right: 30px;
62 | }
63 | .circle {
64 | width: 28px;
65 | height: 28px;
66 | display: flex;
67 | justify-content: center;
68 | align-items: center;
69 | border-radius: 50%;
70 | background-color: green;
71 | }
72 |
73 | @media only screen and (max-width: 768px) {
74 | #all-products {
75 | display: grid;
76 | grid-template-columns: repeat(1, 1fr);
77 | }
78 | .cart {
79 | width: 300px;
80 | /* border: 1px solid green; */
81 | padding: 10px;
82 | box-shadow: 10px 10px 20px gray;
83 | border-radius: 5px;
84 | margin-right: 30px;
85 | position: fixed;
86 | }
87 | .cart-main {
88 | padding-right: 10px;
89 | }
90 | }
91 | @media only screen and (max-width: 468px) {
92 | #all-products {
93 | display: grid;
94 | grid-template-columns: repeat(1, 1fr);
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/js/app.js:
--------------------------------------------------------------------------------
1 | const arr = [];
2 |
3 | const loadProducts = (url) => {
4 | fetch(url)
5 | .then((res) => res.json())
6 | .then((data) => {
7 | arr.push(data);
8 | showProducts(data);
9 | });
10 | };
11 |
12 | loadProducts('https://fakestoreapi.com/products');
13 |
14 | // show all product in UI
15 | const showProducts = (products) => {
16 |
17 | setInnerText('total_products', products.length);
18 |
19 | document.getElementById("all-products").innerHTML = "";
20 |
21 | const allProducts = products.slice(0, 10).map((pd) => pd);
22 | for (const product of allProducts) {
23 | const image = product.images;
24 | const div = document.createElement('div');
25 | div.classList.add('product');
26 | div.innerHTML = `
27 |
28 |

29 |
30 |
${product.title}
31 |
Category: ${product.category}
32 |
Price: $ ${product.price}
33 |
34 |
36 |
37 |
38 | `;
39 | document.getElementById('all-products').appendChild(div);
40 | }
41 | };
42 |
43 | let count = 0;
44 |
45 | const addToCart = (id, price) => {
46 | count = count + 1;
47 | updatePrice('price', value);
48 |
49 | updateTaxAndCharge();
50 | document.getElementById('total-Products').innerText = count;
51 | };
52 |
53 | const showProductDetails = (product_id) => {
54 | console.log(product_id);
55 | fetch(`https://fakestoreapi.com/products/${product_id}`)
56 | .then((res) => res.json())
57 | .then((data) => showProductDetailsInModal(data));
58 | };
59 |
60 | const showProductDetailsInModal = (product_details) => {
61 | console.log(product_details);
62 | setInnerText('exampleModalLabel', product_details.title);
63 | setInnerText('product_id', product_details.id);
64 | setInnerText('modal_body', product_details.description);
65 | setInnerText('rating', product_details.rating.rate);
66 | };
67 |
68 | const getInputValue = (id) => {
69 | const element = document.getElementById(id).innerText;
70 | const converted = parseInt(element);
71 | return converted;
72 | };
73 |
74 | // main price update function
75 | const updatePrice = (id, value) => {
76 | const convertedOldPrice = getInputValue(id);
77 | const convertPrice = parseInt(value);
78 | const total = convertedOldPrice + convertPrice;
79 | document.getElementById(id).innerText = Math.round(total);
80 | };
81 |
82 | // set innerText function
83 | const setInnerText = (id, value) => {
84 | document.getElementById(id).innerText = Math.round(value);
85 | };
86 |
87 | // update delivery charge and total Tax
88 | const updateTaxAndCharge = () => {
89 | const priceConverted = getInputValue('price');
90 | if (priceConverted > 200) {
91 | setInnerText('delivery-charge', 30);
92 | setInnerText('total-tax', priceConverted * 0.2);
93 | }
94 | if (priceConverted > 400) {
95 | setInnerText('delivery-charge', 50);
96 | setInnerText('total-tax', priceConverted * 0.3);
97 | }
98 | if (priceConverted > 500) {
99 | setInnerText('delivery-charge', 60);
100 | setInnerText('total-tax', priceConverted * 0.4);
101 | }
102 | };
103 |
104 | //grandTotal update function
105 | const updateTotal = () => {
106 | const grandTotal =
107 | getInputValue('price') +
108 | getInputValue('delivery-charge') +
109 | getInputValue('total-tax');
110 | document.getElementById('total').innerText = grandTotal;
111 | };
112 |
113 | // search by category
114 | document.getElementById("search-btn").addEventListener("click", function () {
115 | const inputField = document.getElementById("input-value").value;
116 | const searchedProduct = arr[0].find((p) =>
117 | p.category.startsWith(`${inputField}`)
118 | );
119 | showProducts(searchedProduct);
120 | });
121 |
122 |
123 |
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
FakeStore
8 |
9 |
10 |
12 |
13 |
14 |
20 |
21 |
25 |
26 |
27 |
28 |
29 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
49 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 | Total Products:
63 |
64 |
65 |
66 |
67 |
72 |
73 |
74 |
75 |
76 |
77 |
80 | My-Cart
81 |
82 |
83 | |
84 | |
85 | |
86 | |
87 |
88 |
89 |
90 |
91 | | Total Added-Products: |
92 |
93 | $0 |
94 | |
95 | |
96 |
97 |
98 | | Price: |
99 | $ 0 |
100 | |
101 | |
102 |
103 |
104 | | Delivery-Charge: |
105 | $ 20 |
106 | |
107 | |
108 |
109 |
110 | | Total-Tax: |
111 | $ 0 |
112 | |
113 | |
114 |
115 |
116 | | Total |
117 | $ 0 |
118 | |
119 |
120 |
121 |
122 |
123 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
142 |
143 |
144 |
155 |
156 | Product id:
157 |
158 |
159 |
160 |
161 | Rating
162 |
166 |
167 |
176 |
177 |
178 |
179 |
180 |
181 |
186 |
187 |
188 |
189 |
190 |
--------------------------------------------------------------------------------