SELL ACTION WINDOW AND IT'S BACKEND

 WHENEVER, SELL BUTTON IS CLICKED, A WINDOW IS GENERATED THAT ENABLES THE USER TO SELL THAT PARTICULAR STOCK AT WHATEVER PRICE HE DECIDES.

IT IS SIMILAR TO THAT OF BUY WINDOW.

SELLACTIONWINDOW.JS

import React, { useState, useContext } from "react";
import { Link } from "react-router-dom";
import GeneralContext from "./GeneralContext";
import "./BuyActionWindow.css";
import axios from "axios";

export default function SellActionWindow({ uid, price }) {
  const [stockQty, setStockQty] = useState(1);
  const [stockPrice, setStockPrice] = useState(price);
  const generalContext = useContext(GeneralContext);
  const handleSellClick = () => {
    axios.post("http://localhost:3002/sellOrder", {
      name: uid,
      qty: stockQty,
      price: stockPrice,
      mode: "SELL",
    });
    console.log(uid);
    generalContext.closeSellWindow();
  };
  const handleCancelClick = () => {
    generalContext.closeSellWindow();
  };
  return (
    <div className="container" id="buy-window" draggable="true">
      <div className="regular-order">
        <div className="inputs">
          <fieldset>
            <legend>Qty.</legend>
            <input
              type="number"
              name="qty"
              id="qty"
              onChange={(e) => {
                setStockQty(e.target.value);
              }}
              value={stockQty}
            />
          </fieldset>
          <fieldset>
            <legend>Price</legend>
            <input
              type="number"
              name="price"
              id="price"
              step="0.05"
              onChange={(e) => {
                setStockPrice(e.target.value);
              }}
              value={stockPrice}
            />
          </fieldset>
        </div>
      </div>

      <div className="buttons">
      {uid}
        <span>Selling starts at:{price}</span>
        <div>
          <Link className="btn btn-error" onClick={handleSellClick}>
           Sell
          </Link>
          <Link to="" className="btn btn-grey" onClick={handleCancelClick}>
            Cancel
          </Link>
        </div>
      </div>
    </div>
  );
}

IN WATCHLIST.JS

  const handleSellClick = () => {
    generalContext.openSellWindow(uid, price);
  };


            <button className="sell" onClick={handleSellClick}>Sell</button>


IN INDEX.JS

app.post("/sellOrder", async (req, res) => {
  console.log(req.body);
  let newOrder = new OrderModel({
    name: req.body.name,
    qty: req.body.qty,
    price: req.body.price,
    mode: req.body.mode,
  });
  await newOrder.save();
});










Comments

Popular posts from this blog

CONNECTING MONGODB WITH EXPRESS

SETTING UP REACT ROUTER

*********UPDATING THE FRONTEND*********