├── src
├── components
│ ├── index.js
│ ├── Order-comp.js
│ ├── Wishlist-comp.js
│ ├── ProductCard.js
│ ├── Address-comp.js
│ ├── Header.js
│ └── Cart-comp.js
├── utils
│ ├── index.js
│ └── apicall.js
├── pages
│ ├── index.js
│ ├── Home.js
│ ├── Login.js
│ ├── ProductDetail.js
│ └── Profile.js
├── store
│ ├── hooks.js
│ ├── provider.js
│ ├── index.js
│ ├── actions
│ │ ├── index.js
│ │ ├── user-actions.js
│ │ └── shopping-actions.js
│ ├── shpping-slice.js
│ └── user-slice.js
├── setupTests.js
├── App.test.js
├── index.css
├── reportWebVitals.js
├── index.js
├── App.css
├── App.js
└── logo.svg
├── public
├── bg.jpg
├── favicon.ico
├── logo192.png
├── logo512.png
├── placeholder.jpg
├── manifest.json
└── index.html
├── .gitignore
├── package.json
└── README.md
/src/components/index.js:
--------------------------------------------------------------------------------
1 | export * from './Header';
--------------------------------------------------------------------------------
/src/utils/index.js:
--------------------------------------------------------------------------------
1 | export * from './apicall';
--------------------------------------------------------------------------------
/public/bg.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codergogoi/microservice-frontend/HEAD/public/bg.jpg
--------------------------------------------------------------------------------
/src/pages/index.js:
--------------------------------------------------------------------------------
1 | export * from './Login';
2 | export * from './Home';
3 | export * from './Profile';
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codergogoi/microservice-frontend/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codergogoi/microservice-frontend/HEAD/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codergogoi/microservice-frontend/HEAD/public/logo512.png
--------------------------------------------------------------------------------
/public/placeholder.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codergogoi/microservice-frontend/HEAD/public/placeholder.jpg
--------------------------------------------------------------------------------
/src/store/hooks.js:
--------------------------------------------------------------------------------
1 | import { useDispatch, useSelector } from "react-redux";
2 |
3 | export const useAppDispatch = () => useDispatch();
4 | export const useAppSelector = useSelector;
5 |
--------------------------------------------------------------------------------
/src/store/provider.js:
--------------------------------------------------------------------------------
1 | import { store } from "./index";
2 | import { Provider } from "react-redux";
3 | import React from "react";
4 |
5 | export const Providers = ({ children }) => {
6 | return React.createElement(Provider, { store: store }, children);
7 | };
8 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/.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/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/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/store/index.js:
--------------------------------------------------------------------------------
1 | import { configureStore } from "@reduxjs/toolkit";
2 | import { setupListeners } from "@reduxjs/toolkit/query";
3 | import userReducer from "./user-slice";
4 | import shoppingReducer from "./shpping-slice";
5 |
6 | export const store = configureStore({
7 | reducer: {
8 | userReducer,
9 | shoppingReducer,
10 | },
11 | devTools: process.env.NODE_ENV !== "production",
12 | });
13 |
14 | setupListeners(store.dispatch);
15 |
16 | export const RootState = store.getState;
17 | export const AppDispatch = store.dispatch;
18 |
--------------------------------------------------------------------------------
/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/store/actions/index.js:
--------------------------------------------------------------------------------
1 | export * from './user-actions';
2 | export * from './shopping-actions';
3 |
4 | export const Action = {
5 | ERROR: "ERROR",
6 | LOGIN: "LOGIN",
7 | LOGOUT: "LOGOUT",
8 | SIGNUP: "SIGNUP",
9 | PROFILE: "PROFILE",
10 |
11 | DISSMISS: "DISSMISS",
12 |
13 | LANDING_PRODUCTS: "LANDING_PRODUCTS",
14 | PRODUCT_DETAILS: "PRODUCT_DETAILS",
15 | ADD_TO_WISHLIST: "ADD_TO_WISHLIST",
16 | REMOVE_FROM_WISHLIST: "REMOVE_FROM_WISHLIST",
17 |
18 | ADD_TO_CART: "ADD_TO_CART",
19 | REMOVE_FROM_CART: "REMOVE_FROM_CART",
20 |
21 | ADDED_NEW_ADDRESS: "ADDED_NEW_ADDRESS",
22 |
23 | PLACE_ORDER: "PLACE_ORDER"
24 |
25 |
26 | };
--------------------------------------------------------------------------------
/src/store/shpping-slice.js:
--------------------------------------------------------------------------------
1 | import { createSlice } from "@reduxjs/toolkit";
2 |
3 | const initialState = {
4 | products: [],
5 | categories: [],
6 | currentProduct: false,
7 | };
8 |
9 | const shoppingSlice = createSlice({
10 | name: "user",
11 | initialState,
12 | reducers: {
13 | landingProducts(state, action) {
14 | state.products = action.payload.products;
15 | state.categories = action.payload.categories;
16 | },
17 | productDetails(state, action) {
18 | state.currentProduct = action.payload;
19 | },
20 | },
21 | });
22 |
23 | export const { landingProducts, productDetails } = shoppingSlice.actions;
24 | export default shoppingSlice.reducer;
25 |
--------------------------------------------------------------------------------
/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 { Providers } from "./store/provider";
7 |
8 | const root = ReactDOM.createRoot(document.getElementById("root"));
9 | root.render(
10 |
11 |
12 |
13 |
14 |
15 | );
16 |
17 | // If you want to start measuring performance in your app, pass a function
18 | // to log results (for example: reportWebVitals(console.log))
19 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
20 | reportWebVitals();
21 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
5 | .App-logo {
6 | height: 40vmin;
7 | pointer-events: none;
8 | }
9 |
10 | @media (prefers-reduced-motion: no-preference) {
11 | .App-logo {
12 | animation: App-logo-spin infinite 20s linear;
13 | }
14 | }
15 |
16 | .App-header {
17 | background-color: #282c34;
18 | min-height: 100vh;
19 | display: flex;
20 | flex-direction: column;
21 | align-items: center;
22 | justify-content: center;
23 | font-size: calc(10px + 2vmin);
24 | color: white;
25 | }
26 |
27 | .App-link {
28 | color: #61dafb;
29 | }
30 |
31 | @keyframes App-logo-spin {
32 | from {
33 | transform: rotate(0deg);
34 | }
35 | to {
36 | transform: rotate(360deg);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/components/Order-comp.js:
--------------------------------------------------------------------------------
1 |
2 | export const OrderItem = ({ item, onTapViewMore }) => {
3 |
4 | const { _id, orderId, amount, status, items } = item;
5 |
6 |
7 |
8 | return (
9 |
10 |
11 |
12 | Order ID: {orderId}
13 |
14 |
15 | ₹{amount}
16 |
17 |
18 |
19 |
20 |
21 |
27 |
28 |
29 |
30 |
31 |
32 | );
33 |
34 | }
--------------------------------------------------------------------------------
/src/components/Wishlist-comp.js:
--------------------------------------------------------------------------------
1 |
2 | export const WishItem = ({ item, onTapRemove }) => {
3 |
4 | const { _id, name, desc, type, unit, price, banner } = item;
5 |
6 |
7 | return (
8 |
9 |
10 |

11 |
12 |
13 |
{name}
14 |
{desc}
15 |
₹{price}
16 |
17 |
18 |
24 |
25 |
26 |
27 | );
28 |
29 | }
--------------------------------------------------------------------------------
/src/components/ProductCard.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { Link } from 'react-router-dom';
3 |
4 | const ProductCard = ({item}) => {
5 |
6 | const {_id,banner, available, price, name, desc, type} = item;
7 |
8 | return
10 |
11 |

12 |
13 |
{name}
14 |
{desc}
15 |
₹{price}
16 |
17 |
18 |
19 | }
20 |
21 | export { ProductCard };
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "microservice-frontend",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@reduxjs/toolkit": "2.0.1",
7 | "@testing-library/jest-dom": "^5.17.0",
8 | "@testing-library/react": "^13.4.0",
9 | "@testing-library/user-event": "^13.5.0",
10 | "axios": "^1.6.8",
11 | "bootstrap": "^5.3.3",
12 | "moment": "^2.30.1",
13 | "react": "^18.3.1",
14 | "react-dom": "^18.3.1",
15 | "react-redux": "^9.1.2",
16 | "react-router-dom": "^6.23.1",
17 | "react-scripts": "5.0.1",
18 | "redux": "^5.0.1",
19 | "redux-thunk": "^3.1.0",
20 | "web-vitals": "^2.1.4"
21 | },
22 | "scripts": {
23 | "start": "react-scripts start",
24 | "build": "react-scripts build",
25 | "test": "react-scripts test",
26 | "eject": "react-scripts eject"
27 | },
28 | "eslintConfig": {
29 | "extends": [
30 | "react-app",
31 | "react-app/jest"
32 | ]
33 | },
34 | "browserslist": {
35 | "production": [
36 | ">0.2%",
37 | "not dead",
38 | "not op_mini all"
39 | ],
40 | "development": [
41 | "last 1 chrome version",
42 | "last 1 firefox version",
43 | "last 1 safari version"
44 | ]
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/utils/apicall.js:
--------------------------------------------------------------------------------
1 | import api from 'axios';
2 |
3 |
4 | api.defaults.baseURL = "http://localhost:8000/";
5 |
6 | const setHeader = () => {
7 | const token = localStorage.getItem('token');
8 | if(token){
9 | api.defaults.headers.common['Authorization'] = `Bearer ${token}`;
10 | }
11 | }
12 |
13 | export const GetData = async(endPoint,options) => {
14 | try {
15 | setHeader();
16 | const response = await api.get(endPoint);
17 | return response
18 | } catch (err) {
19 | throw err;
20 | }
21 |
22 | }
23 |
24 | export const PostData = async(endPoint,options) => {
25 | try {
26 | setHeader();
27 | const response = await api.post(endPoint, options);
28 | return response
29 | } catch (err) {
30 | throw err;
31 | }
32 | }
33 |
34 | export const PutData = async(endPoint,options) => {
35 |
36 | try {
37 | setHeader();
38 | const response = await api.put(endPoint, options);
39 | return response
40 | } catch (err) {
41 | throw err;
42 | }
43 | }
44 |
45 | export const DeleteData = async(endPoint) => {
46 |
47 | try {
48 | setHeader();
49 | const response = await api.delete(endPoint);
50 | return response
51 | } catch (err) {
52 | throw err;
53 | }
54 | }
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | // import logo from './logo.svg';
2 | // import './App.css';
3 |
4 | // function App() {
5 | // return (
6 | //
22 | // );
23 | // }
24 |
25 | // export default App;
26 |
27 | import "./App.css";
28 |
29 | import { BrowserRouter, Route, Routes } from "react-router-dom";
30 | import { Login } from "./pages";
31 | import { Home } from "./pages";
32 | import { Header } from "./components";
33 | import { ProductDetails } from "./pages/ProductDetail";
34 |
35 | function App() {
36 | return (
37 |
38 |
39 |
40 |
41 | } />
42 | } />
43 | } />
44 |
45 |
46 |
47 | );
48 | }
49 |
50 | export default App;
51 |
--------------------------------------------------------------------------------
/src/components/Address-comp.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 |
3 |
4 | export const AddressComponent = ({ address }) => {
5 |
6 | const addressCard = ({street,postalCode,city,country}) => {
7 | return
8 |
9 |
Default Address
10 |
{street}
11 |
{postalCode},{city},{country}
12 |
13 |
14 |
17 |
20 |
21 |
22 | }
23 |
24 | const listOfAddress = () => {
25 | if(Array.isArray(address)){
26 | return address.map((item) => {
27 | return addressCard(item)
28 | });
29 | }else{
30 | return Address Not available!
31 | }
32 | }
33 |
34 | return
35 | {listOfAddress()}
36 |
37 |
38 |
39 | }
--------------------------------------------------------------------------------
/src/store/actions/user-actions.js:
--------------------------------------------------------------------------------
1 | import { GetData, PostData } from "../../utils";
2 | import { Action } from "../actions";
3 |
4 | export const SetAuthToken = async (token) => {
5 | if (token) {
6 | localStorage.setItem("token", token);
7 | } else {
8 | localStorage.clear();
9 | }
10 | };
11 |
12 | export const onSignup =
13 | ({ email, password, phone }) =>
14 | async (dispatch) => {
15 | try {
16 | const response = await PostData("/customner/signup", {
17 | email,
18 | password,
19 | phone,
20 | });
21 | const { token } = response.data;
22 | await SetAuthToken(token);
23 | return dispatch({ type: Action.SIGNUP, payload: response.data });
24 | } catch (err) {
25 | console.log(err);
26 | }
27 | };
28 |
29 | export const onLogin =
30 | ({ email, password }) =>
31 | async (dispatch) => {
32 | try {
33 | const response = await PostData("/customer/login", {
34 | email,
35 | password,
36 | });
37 |
38 | const { token } = response.data;
39 | await SetAuthToken(token);
40 |
41 | return dispatch({ type: Action.LOGIN, payload: response.data });
42 | } catch (err) {
43 | console.log(err);
44 | }
45 | };
46 |
47 | export const onViewProfile = () => async (dispatch) => {
48 | try {
49 | const response = await GetData("/customer/profile");
50 |
51 | return dispatch({ type: Action.PROFILE, payload: response.data });
52 | } catch (err) {
53 | console.log(err);
54 | }
55 | };
56 |
--------------------------------------------------------------------------------
/src/pages/Home.js:
--------------------------------------------------------------------------------
1 | import React, { useReducer, useState, useEffect } from "react";
2 | import { ProductCard } from "../components/ProductCard";
3 | import { onSignup, onGetProducts } from "../store/actions";
4 | import { ProductDetails } from "./ProductDetail";
5 | import { useAppDispatch, useAppSelector } from "../store/hooks";
6 |
7 | const Home = () => {
8 | const { categories, products } = useAppSelector(
9 | (state) => state.shoppingReducer
10 | );
11 |
12 | const dispatch = useAppDispatch();
13 |
14 | useEffect(() => {
15 | dispatch(onGetProducts());
16 | }, []);
17 |
18 | const listOfcategories = () => {
19 | return (
20 |
21 | {categories.map((item) => {
22 | return (
23 |
36 | );
37 | })}
38 |
39 | );
40 | };
41 |
42 | const listOfProducts = () => {
43 | return products.map((item) => {
44 | return ;
45 | });
46 | };
47 |
48 | return (
49 |
50 |

51 |
61 |
62 | {categories && listOfcategories()}
63 |
64 |
65 |
66 |
67 | {products && listOfProducts()}
68 |
69 |
70 | );
71 | };
72 |
73 | export { Home };
74 |
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/store/user-slice.js:
--------------------------------------------------------------------------------
1 | import { createSlice } from "@reduxjs/toolkit";
2 |
3 | const initialState = {
4 | value: 0,
5 | user: {}, // {id: // token: //}
6 | profile: {}, //
7 | wishlist: [],
8 | cart: [],
9 | orders: [],
10 | };
11 |
12 | const userSlice = createSlice({
13 | name: "counter",
14 | initialState,
15 | reducers: {
16 | userLogin(state, action) {
17 | state.user = action.payload;
18 | },
19 | userSignup(state, action) {
20 | state.user = action.payload;
21 | },
22 | userProfile(state, action) {
23 | state.profile = action.payload;
24 | state.wishlist = action.payload.wishlist;
25 | state.cart = action.payload.cart;
26 | state.address = action.payload.address;
27 | state.orders = action.payload.orders;
28 | },
29 | addNewAddress(state, action) {
30 | state.address = action.payload.address;
31 | },
32 | addToWishlist(state, action) {
33 | state.wishlist = state.wishlist.length
34 | ? [...state.wishlist, action.payload]
35 | : [action.payload];
36 | },
37 | removeFromWishlist(state, action) {
38 | if (state.wishlist.length) {
39 | const existWishlist = state.wishlist.filter(
40 | (item) => item._id !== action.payload._id
41 | );
42 | state.wishlist = existWishlist;
43 | } else {
44 | state.wishlist = [];
45 | }
46 | },
47 | addToCart(state, action) {
48 | let existingCart = state.cart;
49 |
50 | if (existingCart.length) {
51 | const existItem = existingCart.filter(
52 | ({ product }) => product._id === action.payload.product._id
53 | );
54 |
55 | if (existItem.length) {
56 | const index = existingCart.indexOf(existItem[0]);
57 | existingCart[index] = action.payload;
58 |
59 | state.cart = existingCart;
60 | } else {
61 | state.cart = [...state.cart, action.payload];
62 | }
63 | } else {
64 | state.cart = [action.payload];
65 | }
66 | },
67 | removeFromCart(state, action) {
68 | let currentCart = state.cart;
69 | if (currentCart.length) {
70 | const existItem = currentCart.filter(
71 | ({ product }) => product._id !== action.payload.product._id
72 | );
73 |
74 | state.cart = existItem;
75 | } else {
76 | state.cart = [];
77 | }
78 | },
79 | placeOrder(state, action) {
80 | state.orders = [action.payload, ...state.orders];
81 | state.cart = [];
82 | },
83 | },
84 | });
85 |
86 | export const {
87 | userLogin,
88 | userSignup,
89 | userProfile,
90 | addNewAddress,
91 | addToWishlist,
92 | removeFromWishlist,
93 | addToCart,
94 | removeFromCart,
95 | placeOrder,
96 | } = userSlice.actions;
97 | export default userSlice.reducer;
98 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
19 |
25 |
31 |
40 | Online Shopping
41 |
42 |
43 |
44 |
45 |
55 |
60 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/src/components/Header.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { Link } from 'react-router-dom'
3 | import { useDispatch, useSelector } from "react-redux";
4 |
5 | export const Header = () => {
6 |
7 | const { user, profile, cart } = useSelector(state => state.userReducer);
8 |
9 | const { id, token } = user;
10 |
11 | const { address, orders } = profile;
12 |
13 | const cartCount = () => {
14 |
15 | if(Array.isArray(cart)){
16 | return cart.length;
17 | }
18 | return 0;
19 | };
20 |
21 |
22 | const loginProfile = () => {
23 |
24 | if(token){
25 | return (
26 |
27 | -
28 |
29 |
30 | {cartCount()}
31 |
32 |
33 | -
34 |
35 |
36 |
37 |
38 |
39 |
40 | );
41 |
42 | }else{
43 |
44 | return (
45 |
46 | -
47 |
48 |
49 |
50 |
51 | -
52 |
53 |
54 | Login
55 |
56 |
57 |
58 | );
59 |
60 | }
61 |
62 |
63 | }
64 |
65 |
66 | return ( );
79 | }
--------------------------------------------------------------------------------
/src/components/Cart-comp.js:
--------------------------------------------------------------------------------
1 | import React, { useState, useEffect } from "react";
2 |
3 | export const CartItem = (props) => {
4 |
5 | const { item, cart, onAdd, onRemove} = props;
6 |
7 | const { _id } = item.product;
8 |
9 |
10 | const [currentUnit,setCurrentUnit] = useState(0)
11 |
12 | useEffect(() => {
13 | if(Array.isArray(cart) && cart.length){
14 | if(item !== undefined){
15 | const exist = cart.filter(({ product }, unit) => product._id == _id);
16 | if(exist.length){
17 | setCurrentUnit(exist[0].unit)
18 | }
19 | }
20 | }
21 |
22 | },[cart])
23 |
24 | const addCart = () => {
25 |
26 | const newUnit = currentUnit + 1;
27 |
28 | setCurrentUnit(newUnit);
29 | setTimeout(() => {
30 | onAdd({_id: _id, qty: newUnit});
31 | },0);
32 | }
33 |
34 | const removeCart = () => {
35 | if(item !== undefined){
36 | const newUnit = currentUnit - 1;
37 | setCurrentUnit(newUnit);
38 | setTimeout(() => {
39 | if(newUnit > 0){
40 | onAdd({_id: _id, qty: newUnit});
41 | }else{
42 | onRemove({_id})
43 | }
44 | }, 0);
45 |
46 | }
47 | }
48 |
49 | if(item){
50 |
51 | const { product, unit } = item;
52 |
53 | if(product){
54 |
55 | let { name, desc, type, price, banner } = product;
56 |
57 | return (
58 |
59 |
60 |

61 |
62 |
63 |
{name}
64 |
{desc}
65 |
₹{price}
66 |
67 |
68 |
73 | {currentUnit}
74 |
79 |
80 |
81 | );
82 |
83 | }else{
84 | return Not found! { JSON.stringify(item)}
85 | }
86 | }else{
87 | return Not found!
88 | }
89 |
90 | }
--------------------------------------------------------------------------------
/src/pages/Login.js:
--------------------------------------------------------------------------------
1 | import React, { useReducer, useState, useEffect } from "react";
2 | import { useDispatch, useSelector } from "react-redux";
3 | import { onSignup, onLogin, onViewProfile } from "../store/actions";
4 | import { AddressComponent } from "../components/Address-comp";
5 | import { Profile } from "./Profile";
6 | import { useAppDispatch, useAppSelector } from "../store/hooks";
7 |
8 | //load Shopping profile
9 | const Login = () => {
10 | const { user, profile } = useAppSelector((state) => state.userReducer);
11 |
12 | const dispatch = useAppDispatch();
13 |
14 | const { id, token } = user;
15 |
16 | const { address, whishlist, orders } = profile;
17 |
18 | const [isSignup, setSignup] = useState(false);
19 |
20 | const [email, setEmail] = useState("");
21 | const [password, setPassword] = useState("");
22 |
23 | useEffect(() => {
24 | if (token) {
25 | dispatch(onViewProfile());
26 | }
27 | }, [token]);
28 |
29 | const userSignup = () => {
30 | //call Signup
31 | };
32 |
33 | const userLogin = () => {
34 | dispatch(onLogin({ email, password }));
35 | };
36 |
37 | const loginForm = () => {
38 | return (
39 |
83 | );
84 | };
85 |
86 | const signUpForm = () => {
87 | return (
88 |
89 |
Signup
90 |
91 | );
92 | };
93 |
94 | if (token) {
95 | return ;
96 | } else {
97 | return (
98 |
99 | {isSignup ? signUpForm() : loginForm()}
100 |
101 | );
102 | }
103 | };
104 |
105 | export { Login };
106 |
--------------------------------------------------------------------------------
/src/store/actions/shopping-actions.js:
--------------------------------------------------------------------------------
1 | import { DeleteData, GetData, PostData, PutData } from '../../utils'
2 | import { Action } from '.'
3 |
4 |
5 | export const onGetProducts = (payload) => async(dispatch) => {
6 |
7 | try {
8 |
9 | const response = await GetData('/');
10 |
11 | dispatch({ type: Action.LANDING_PRODUCTS, payload: response.data });
12 |
13 |
14 | } catch (err) {
15 | console.log(err)
16 | }
17 |
18 | };
19 |
20 |
21 | export const onGetProductDetails = (id) => async(dispatch) => {
22 |
23 | try {
24 |
25 | const response = await GetData('/'+id);
26 |
27 | dispatch({ type: Action.PRODUCT_DETAILS, payload: response.data });
28 |
29 |
30 | } catch (err) {
31 | console.log(err)
32 | }
33 |
34 | };
35 |
36 | /* ------------------- Wishlist --------------------- */
37 |
38 | export const onAddToWishlist = (_id) => async(dispatch) => {
39 |
40 |
41 | try {
42 |
43 | const response = await PutData('/wishlist', {
44 | _id
45 | });
46 |
47 | dispatch({ type: Action.ADD_TO_WISHLIST, payload: response.data });
48 |
49 |
50 | } catch (err) {
51 | console.log(err)
52 | }
53 |
54 | };
55 |
56 |
57 | export const onRemoveFromWishlist = (_id) => async(dispatch) => {
58 |
59 | try {
60 |
61 | const response = await DeleteData('/wishlist/'+_id);
62 |
63 | dispatch({ type: Action.REMOVE_FROM_WISHLIST, payload: response.data });
64 |
65 | } catch (err) {
66 | console.log(err)
67 | }
68 |
69 | };
70 |
71 |
72 |
73 | /* ------------------- Cart --------------------- */
74 |
75 | export const onAddToCart = ({ _id, qty }) => async(dispatch) => {
76 |
77 | try {
78 |
79 | const response = await PutData('/cart', {
80 | _id,
81 | qty
82 | });
83 |
84 | dispatch({ type: Action.ADD_TO_CART, payload: response.data });
85 |
86 |
87 | } catch (err) {
88 | console.log(err)
89 | }
90 |
91 | };
92 |
93 |
94 | export const onRemoveFromCart = (_id) => async(dispatch) => {
95 |
96 | try {
97 |
98 | const response = await DeleteData('/cart/'+_id);
99 |
100 | dispatch({ type: Action.REMOVE_FROM_CART, payload: response.data });
101 |
102 | } catch (err) {
103 | console.log(err)
104 | }
105 |
106 | };
107 |
108 |
109 | export const onCreateAddress = ({street, postalCode,city,country }) => async(dispatch) => {
110 |
111 | try {
112 |
113 | const response = await PostData('/customer/address/', {
114 | street, postalCode,city,country
115 | });
116 |
117 | dispatch({ type: Action.ADDED_NEW_ADDRESS, payload: response.data });
118 |
119 | } catch (err) {
120 | console.log(err)
121 | }
122 |
123 | };
124 |
125 |
126 | export const onPlaceOrder = ({txnId }) => async(dispatch) => {
127 |
128 | try {
129 |
130 | const response = await PostData('/shopping/order/', {
131 | txnId
132 | });
133 |
134 | console.log(response.data,'ORDER');
135 |
136 | dispatch({ type: Action.PLACE_ORDER, payload: response.data });
137 |
138 | } catch (err) {
139 | console.log(err)
140 | }
141 |
142 | };
143 |
--------------------------------------------------------------------------------
/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 | ### `npm start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13 |
14 | The page will reload when you make changes.\
15 | You may also see any lint errors in the console.
16 |
17 | ### `npm 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 | ### `npm run 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 | ### `npm run 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 | ### `npm run 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 |
--------------------------------------------------------------------------------
/src/pages/ProductDetail.js:
--------------------------------------------------------------------------------
1 | import React, { useState, useEffect } from "react";
2 | import { useParams } from "react-router-dom";
3 | import { useAppDispatch, useAppSelector } from "../store/hooks";
4 |
5 | import {
6 | onGetProductDetails,
7 | onAddToWishlist,
8 | onAddToCart,
9 | onRemoveFromWishlist,
10 | onRemoveFromCart,
11 | } from "../store/actions";
12 |
13 | const ProductDetails = (props) => {
14 | const { id } = useParams();
15 |
16 | const dispatch = useAppDispatch();
17 |
18 | const { currentProduct } = useAppSelector((state) => state.shoppingReducer);
19 | const { wishlist, cart } = useAppSelector((state) => state.userReducer);
20 |
21 | const [currentUnit, setCurrentUnit] = useState(0);
22 |
23 | const { _id, banner, available, price, name, desc, type } = currentProduct;
24 |
25 | useEffect(() => {
26 | dispatch(onGetProductDetails(id));
27 | }, [id]);
28 |
29 | useEffect(() => {
30 | if (Array.isArray(cart) && cart.length) {
31 | const exist = cart.filter(({ product }, unit) => product._id == _id);
32 | if (exist.length) {
33 | setCurrentUnit(exist[0].unit);
34 | }
35 | }
36 | }, [currentProduct]);
37 |
38 | const setImage = () => {
39 | if (banner) {
40 | return banner;
41 | } else {
42 | return "placeholder.jpg";
43 | }
44 | };
45 |
46 | const addCart = () => {
47 | const newUnit = currentUnit + 1;
48 |
49 | setCurrentUnit(newUnit);
50 | setTimeout(() => {
51 | dispatch(onAddToCart({ _id, qty: newUnit }));
52 | }, 0);
53 | };
54 |
55 | const removeCart = () => {
56 | if (currentUnit > 0) {
57 | const newUnit = currentUnit - 1;
58 | setCurrentUnit(newUnit);
59 | setTimeout(() => {
60 | if (newUnit > 0) {
61 | dispatch(onAddToCart({ _id, qty: newUnit }));
62 | } else {
63 | dispatch(onRemoveFromCart(_id));
64 | }
65 | }, 0);
66 | }
67 | };
68 |
69 | const checkWishListExistence = () => {
70 | if (Array.isArray(wishlist) && wishlist.length) {
71 | const exist = wishlist.filter((item) => item._id == _id);
72 |
73 | if (exist.length > 0) {
74 | return (
75 |
83 | );
84 | } else {
85 | return (
86 |
94 | );
95 | }
96 | } else {
97 | return (
98 |
106 | );
107 | }
108 | };
109 |
110 | const checkCartExistence = () => {
111 | if (Array.isArray(cart) && cart.length) {
112 | const exist = cart.filter(({ product }) => product._id == _id);
113 |
114 | if (exist.length > 0) {
115 | return (
116 |
125 |
128 |
129 | {currentUnit}
130 |
131 |
134 |
135 | );
136 | } else {
137 | return (
138 |
139 |
143 |
144 | );
145 | }
146 | } else {
147 | return (
148 |
149 |
153 |
154 | );
155 | }
156 | };
157 |
158 | return (
159 |
160 |
161 |
162 |
})
163 |
164 |
165 |
166 |
167 | Category →{" "}
168 | {type}
169 |
170 |
171 |
172 |
173 |
174 | {" "}
175 | Price
176 |
180 | ₹{price}
181 |
182 |
183 |
184 |
187 |
188 |
189 | Type of Product: {type}
190 |
191 |
192 |
193 |
194 | {" "}
195 | *Product will be available through standard delivery channel
196 |
197 |
198 |
199 | {checkCartExistence()}
200 |
208 | {checkWishListExistence()}
209 |
210 |
211 |
212 |
213 |
214 | );
215 | };
216 |
217 | export { ProductDetails };
218 |
--------------------------------------------------------------------------------
/src/pages/Profile.js:
--------------------------------------------------------------------------------
1 | import React, { useState, useEffect } from "react";
2 | import {
3 | onRemoveFromWishlist,
4 | onViewProfile,
5 | onAddToCart,
6 | onRemoveFromCart,
7 | onCreateAddress,
8 | onPlaceOrder,
9 | } from "../store/actions";
10 | import { AddressComponent } from "../components/Address-comp";
11 | import { CartItem } from "../components/Cart-comp";
12 | import { WishItem } from "../components/Wishlist-comp";
13 | import { OrderItem } from "../components/Order-comp";
14 |
15 | import { useAppDispatch, useAppSelector } from "../store/hooks";
16 |
17 | //load Shopping profile
18 |
19 | const Profile = () => {
20 | const { user, wishlist, cart, orders, address } = useAppSelector(
21 | (state) => state.userReducer
22 | );
23 | const dispatch = useAppDispatch();
24 |
25 | const [street, setStreet] = useState();
26 | const [city, setCity] = useState();
27 | const [state, setState] = useState();
28 | const [postalCode, setPostalCode] = useState();
29 | const [country, setCountry] = useState();
30 |
31 | const { id, token } = user;
32 |
33 | useEffect(() => {
34 | if (token) {
35 | dispatch(onViewProfile());
36 | }
37 | }, [token]);
38 |
39 | const onAdd = ({ _id, qty }) => {
40 | console.log(_id, qty);
41 |
42 | dispatch(onAddToCart({ _id, qty: qty }));
43 | };
44 |
45 | const onRemove = ({ _id }) => {
46 | dispatch(onRemoveFromCart(_id));
47 | };
48 |
49 | const removeFromWishlist = (_id) => {
50 | dispatch(onRemoveFromWishlist(_id));
51 | };
52 |
53 | const viewCart = () => {
54 | if (Array.isArray(cart) && cart.length) {
55 | return (
56 |
57 | {cart.map((item) => {
58 | if (item) {
59 | return (
60 |
66 | );
67 | }
68 | })}
69 |
70 | );
71 | } else {
72 | return (
73 |
77 | Your Cart is Empty!
78 |
79 | );
80 | }
81 | };
82 |
83 | const viewWishlist = () => {
84 | if (Array.isArray(wishlist) && wishlist.length) {
85 | return (
86 |
87 | {wishlist.map((item) => {
88 | return ;
89 | })}
90 |
91 | );
92 | } else {
93 | return (
94 |
98 | Your Wishlist is Empty!
99 |
100 | );
101 | }
102 | };
103 |
104 | const viewOrders = () => {
105 | if (Array.isArray(orders) && orders.length) {
106 | return (
107 |
108 | {orders.map((item) => {
109 | return {}} />;
110 | })}
111 |
112 | );
113 | } else {
114 | return (
115 |
119 | Your Order List is Empty!
120 |
121 | );
122 | }
123 | };
124 |
125 | const onTapPlaceOrder = () => {
126 | // Perform Payment
127 | dispatch(onPlaceOrder({ txnId: "72365ffdds" }));
128 | };
129 |
130 | const viewPlaceOrder = () => {
131 | if (Array.isArray(cart) && cart.length) {
132 | let totalAmount = 0;
133 |
134 | cart.map(({ unit, product }) => {
135 | totalAmount += unit * product.price;
136 | });
137 |
138 | return (
139 |
140 |
141 |
142 | {" "}
143 | Total Amount:{" "}
144 |
148 | {totalAmount}
149 |
150 |
151 |
152 |
153 |
159 |
160 |
161 | );
162 | }
163 | };
164 |
165 | const addNewAddress = () => {
166 | dispatch(
167 | onCreateAddress({
168 | street,
169 | postalCode,
170 | city,
171 | country,
172 | })
173 | );
174 | };
175 |
176 | const handleAddress = () => {
177 | if (Array.isArray(address) && address.length) {
178 | return (
179 |
180 |
181 |
Shopping Profile
182 |
183 |
184 |
185 |
186 |
187 |
188 | );
189 | } else {
190 | return (
191 |
255 | );
256 | }
257 | };
258 |
259 | const shoppingProfile = () => {
260 | return (
261 |
262 | {handleAddress()}
263 |
264 |
272 |
273 |
277 | {" "}
278 | Shopping Cart
279 |
280 |
281 |
323 |
324 |
328 |
329 |
335 | {cart && viewCart()}
336 |
337 |
343 | {wishlist && viewWishlist()}
344 |
345 |
351 | {orders && viewOrders()}
352 |
353 |
354 |
355 |
356 | {viewPlaceOrder()}
357 |
358 | );
359 | };
360 |
361 | return {shoppingProfile()}
;
362 | };
363 |
364 | export { Profile };
365 |
--------------------------------------------------------------------------------