+ );
+}
+
+export default Product;
\ No newline at end of file
diff --git a/Amazon-clone/StateProvider.js b/Amazon-clone/StateProvider.js
new file mode 100644
index 0000000..5370a2f
--- /dev/null
+++ b/Amazon-clone/StateProvider.js
@@ -0,0 +1,14 @@
+import React, { createContext, useContext, useReducer } from "react";
+
+// Prepares the dataLayer
+export const StateContext = createContext();
+
+// Wrap our app and provide the Data layer
+export const StateProvider = ({ reducer, initialState, children }) => (
+
+ {children}
+
+);
+
+// Pull information from the data layer
+export const useStateValue = () => useContext(StateContext);
\ No newline at end of file
diff --git a/Amazon-clone/Subtotal.css b/Amazon-clone/Subtotal.css
new file mode 100644
index 0000000..06b089e
--- /dev/null
+++ b/Amazon-clone/Subtotal.css
@@ -0,0 +1,31 @@
+.subtotal {
+ display: flex;
+ flex-direction: column;
+ justify-content: space-between;
+ width: 300px;
+ height: 100px;
+ padding: 20px;
+ background-color: #f3f3f3;
+ border: 1px solid #dddddd;
+ border-radius: 3px;
+ }
+
+ .subtotal__gift {
+ display: flex;
+ align-items: center;
+ }
+
+ .subtotal__gift > input {
+ margin-right: 5px;
+ }
+
+ .subtotal > button {
+ background: #f0c14b;
+ border-radius: 2px;
+ width: 100%;
+ height: 30px;
+ border: 1px solid;
+ margin-top: 10px;
+ border-color: #a88734 #9c7e31 #846a29;
+ color: #111;
+ }
\ No newline at end of file
diff --git a/Amazon-clone/Subtotal.js b/Amazon-clone/Subtotal.js
new file mode 100644
index 0000000..df0afcb
--- /dev/null
+++ b/Amazon-clone/Subtotal.js
@@ -0,0 +1,38 @@
+import React from "react";
+import "./Subtotal.css";
+import CurrencyFormat from "react-currency-format";
+import { useStateValue } from "./StateProvider";
+import { getBasketTotal } from "./reducer";
+import { useHistory } from "react-router-dom";
+
+function Subtotal() {
+ const history = useHistory();
+ const [{ basket }, dispatch] = useStateValue();
+
+ return (
+
+ (
+ <>
+
+ {/* Part of the homework */}
+ Subtotal ({basket.length} items): {value}
+
+
+ This order contains a gift
+
+ >
+ )}
+ decimalScale={2}
+ value={getBasketTotal(basket)} // Part of the homework
+ displayType={"text"}
+ thousandSeparator={true}
+ prefix={"$"}
+ />
+
+
+
+ );
+}
+
+export default Subtotal;
diff --git a/Amazon-clone/index.css b/Amazon-clone/index.css
new file mode 100644
index 0000000..b026f20
--- /dev/null
+++ b/Amazon-clone/index.css
@@ -0,0 +1,31 @@
+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;
+}
+* {
+ margin: 0;
+}
+
+body {
+ background-color: rgb(234, 237, 237);
+ 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;
+}
\ No newline at end of file
diff --git a/Amazon-clone/index.js b/Amazon-clone/index.js
new file mode 100644
index 0000000..60f595d
--- /dev/null
+++ b/Amazon-clone/index.js
@@ -0,0 +1,22 @@
+
+import React from "react";
+import ReactDOM from "react-dom";
+import "./index.css";
+import App from "./App";
+import * as serviceWorker from "./serviceWorker";
+import reducer, { initialState } from "./reducer";
+import { StateProvider } from "./StateProvider";
+
+ReactDOM.render(
+
+
+
+
+ ,
+ 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();
\ No newline at end of file
diff --git a/Amazon-clone/logo.svg b/Amazon-clone/logo.svg
new file mode 100644
index 0000000..6b60c10
--- /dev/null
+++ b/Amazon-clone/logo.svg
@@ -0,0 +1,7 @@
+
diff --git a/Amazon-clone/reducer.js b/Amazon-clone/reducer.js
new file mode 100644
index 0000000..c509885
--- /dev/null
+++ b/Amazon-clone/reducer.js
@@ -0,0 +1,56 @@
+export const initialState = {
+ basket: [],
+ user: null
+ };
+
+ // Selector
+ export const getBasketTotal = (basket) =>
+ basket?.reduce((amount, item) => item.price + amount, 0);
+
+ const reducer = (state, action) => {
+ console.log(action);
+ switch (action.type) {
+ case "ADD_TO_BASKET":
+ return {
+ ...state,
+ basket: [...state.basket, action.item],
+ };
+
+ case 'EMPTY_BASKET':
+ return {
+ ...state,
+ basket: []
+ }
+
+ case "REMOVE_FROM_BASKET":
+ const index = state.basket.findIndex(
+ (basketItem) => basketItem.id === action.id
+ );
+ let newBasket = [...state.basket];
+
+ if (index >= 0) {
+ newBasket.splice(index, 1);
+
+ } else {
+ console.warn(
+ `Cant remove product (id: ${action.id}) as its not in basket!`
+ )
+ }
+
+ return {
+ ...state,
+ basket: newBasket
+ }
+
+ case "SET_USER":
+ return {
+ ...state,
+ user: action.user
+ }
+
+ default:
+ return state;
+ }
+ };
+
+ export default reducer;
\ No newline at end of file
diff --git a/Amazon-clone/serviceWorker.js b/Amazon-clone/serviceWorker.js
new file mode 100644
index 0000000..b04b771
--- /dev/null
+++ b/Amazon-clone/serviceWorker.js
@@ -0,0 +1,141 @@
+// This optional code is used to register a service worker.
+// register() is not called by default.
+
+// This lets the app load faster on subsequent visits in production, and gives
+// it offline capabilities. However, it also means that developers (and users)
+// will only see deployed updates on subsequent visits to a page, after all the
+// existing tabs open on the page have been closed, since previously cached
+// resources are updated in the background.
+
+// To learn more about the benefits of this model and instructions on how to
+// opt-in, read https://bit.ly/CRA-PWA
+
+const isLocalhost = Boolean(
+ window.location.hostname === 'localhost' ||
+ // [::1] is the IPv6 localhost address.
+ window.location.hostname === '[::1]' ||
+ // 127.0.0.0/8 are considered localhost for IPv4.
+ window.location.hostname.match(
+ /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
+ )
+);
+
+export function register(config) {
+ if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
+ // The URL constructor is available in all browsers that support SW.
+ const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
+ if (publicUrl.origin !== window.location.origin) {
+ // Our service worker won't work if PUBLIC_URL is on a different origin
+ // from what our page is served on. This might happen if a CDN is used to
+ // serve assets; see https://github.com/facebook/create-react-app/issues/2374
+ return;
+ }
+
+ window.addEventListener('load', () => {
+ const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
+
+ if (isLocalhost) {
+ // This is running on localhost. Let's check if a service worker still exists or not.
+ checkValidServiceWorker(swUrl, config);
+
+ // Add some additional logging to localhost, pointing developers to the
+ // service worker/PWA documentation.
+ navigator.serviceWorker.ready.then(() => {
+ console.log(
+ 'This web app is being served cache-first by a service ' +
+ 'worker. To learn more, visit https://bit.ly/CRA-PWA'
+ );
+ });
+ } else {
+ // Is not localhost. Just register service worker
+ registerValidSW(swUrl, config);
+ }
+ });
+ }
+}
+
+function registerValidSW(swUrl, config) {
+ navigator.serviceWorker
+ .register(swUrl)
+ .then(registration => {
+ registration.onupdatefound = () => {
+ const installingWorker = registration.installing;
+ if (installingWorker == null) {
+ return;
+ }
+ installingWorker.onstatechange = () => {
+ if (installingWorker.state === 'installed') {
+ if (navigator.serviceWorker.controller) {
+ // At this point, the updated precached content has been fetched,
+ // but the previous service worker will still serve the older
+ // content until all client tabs are closed.
+ console.log(
+ 'New content is available and will be used when all ' +
+ 'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
+ );
+
+ // Execute callback
+ if (config && config.onUpdate) {
+ config.onUpdate(registration);
+ }
+ } else {
+ // At this point, everything has been precached.
+ // It's the perfect time to display a
+ // "Content is cached for offline use." message.
+ console.log('Content is cached for offline use.');
+
+ // Execute callback
+ if (config && config.onSuccess) {
+ config.onSuccess(registration);
+ }
+ }
+ }
+ };
+ };
+ })
+ .catch(error => {
+ console.error('Error during service worker registration:', error);
+ });
+}
+
+function checkValidServiceWorker(swUrl, config) {
+ // Check if the service worker can be found. If it can't reload the page.
+ fetch(swUrl, {
+ headers: { 'Service-Worker': 'script' },
+ })
+ .then(response => {
+ // Ensure service worker exists, and that we really are getting a JS file.
+ const contentType = response.headers.get('content-type');
+ if (
+ response.status === 404 ||
+ (contentType != null && contentType.indexOf('javascript') === -1)
+ ) {
+ // No service worker found. Probably a different app. Reload the page.
+ navigator.serviceWorker.ready.then(registration => {
+ registration.unregister().then(() => {
+ window.location.reload();
+ });
+ });
+ } else {
+ // Service worker found. Proceed as normal.
+ registerValidSW(swUrl, config);
+ }
+ })
+ .catch(() => {
+ console.log(
+ 'No internet connection found. App is running in offline mode.'
+ );
+ });
+}
+
+export function unregister() {
+ if ('serviceWorker' in navigator) {
+ navigator.serviceWorker.ready
+ .then(registration => {
+ registration.unregister();
+ })
+ .catch(error => {
+ console.error(error.message);
+ });
+ }
+}
diff --git a/Amazon-clone/setupTests.js b/Amazon-clone/setupTests.js
new file mode 100644
index 0000000..74b1a27
--- /dev/null
+++ b/Amazon-clone/setupTests.js
@@ -0,0 +1,5 @@
+// jest-dom adds custom jest matchers for asserting on DOM nodes.
+// allows you to do things like:
+// expect(element).toHaveTextContent(/react/i)
+// learn more: https://github.com/testing-library/jest-dom
+import '@testing-library/jest-dom/extend-expect';