Giter Club home page Giter Club logo

reactshop's Introduction

ReactShop.

React + Typescript, Firebase DB and FakeStore API.

Main goal -> working with API, react-context and send data from UI to DB.


Alt Text


A piece of code

export const cartReducer = (state: ICartContext, action: Action) => {
  if (action.type === 'ADD') {
    const updatedTotalAmount = state.totalAmount + action.payload!.price * action.payload!.amount;

    const existingCartProductIndex = state.products.findIndex(
      (item) => item.id === action.payload!.id
    );
    const existingCartProduct = state.products[existingCartProductIndex];

    let updatedProducts;

    if (existingCartProduct) {
      const updatedProduct = {
        ...existingCartProduct,
        amount: existingCartProduct.amount + action.payload!.amount,
      };
      updatedProducts = [...state.products];
      updatedProducts[existingCartProductIndex] = updatedProduct;
    } else {
      updatedProducts = state.products.concat(action.payload!);
    }

    return {
      products: updatedProducts,
      totalAmount: updatedTotalAmount,
    };
  }
  if (action.type === 'REMOVE') {
    const existingProductIndex = state.products.findIndex((item) => item.id === action.payload!.id);
    const existingProduct = state.products[existingProductIndex];
    const updatedTotalAmount = state.totalAmount - existingProduct.price;

    let updatedProducts;
    if (existingProduct.amount === 1) {
      updatedProducts = state.products.filter((item) => item.id !== action.payload!.id);
    } else {
      const updatedProduct = { ...existingProduct, amount: existingProduct.amount - 1 };
      updatedProducts = [...state.products];
      updatedProducts[existingProductIndex] = updatedProduct;
    }

    return {
      products: updatedProducts,
      totalAmount: updatedTotalAmount,
    };
  }
  if (action.type === 'CLEAN') {
    return initialState;
  }

  return initialState;
};

Download & Installation

git clone https://github.com/cold-world/ReactShop.git
cd <project-dir>
npm install
npm run dev

reactshop's People

Contributors

cold-world avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.