From 686b75d1ee04805eb31b02bcbef79005fdaf7faa Mon Sep 17 00:00:00 2001 From: Girma Ebssa Date: Fri, 26 Feb 2021 13:17:37 -0800 Subject: [PATCH 1/2] fixes nickname bug and shows success and error message on transactions --- client/src/components/Form/index.js | 21 ++++++++++++--------- client/src/components/Nav/index.js | 6 +++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/client/src/components/Form/index.js b/client/src/components/Form/index.js index abc272c..5e5ba9b 100644 --- a/client/src/components/Form/index.js +++ b/client/src/components/Form/index.js @@ -14,6 +14,8 @@ export default function PurchaseForm() { const [currentPrice, setCurrentPrice] = useState(); const [buyOrSell, setbuyOrSell] = useState("buy"); const [error, setError] = useState(); + const [success, setSuccess] = useState(); + const [message, setMessage] = useState(); const [userBalance, setUserBalance] = useState(0) const [stocksPortfolio, setStocksPortfolio] = useState([]) const [foundStock, setfoundStock] = useState("") @@ -41,7 +43,7 @@ export default function PurchaseForm() { setFormObject({ ...formObject, [name]: value }) // GetCurrentPrice(formObject.symbol) let newArr = stocksPortfolio.filter(stock => { - console.log(stock.symbol === formObject.symbol.toUpperCase().trim()) + // console.log(stock.symbol === formObject.symbol.toUpperCase().trim()) return stock.symbol === formObject.symbol.toUpperCase().trim() }); if (newArr.length) { @@ -78,7 +80,7 @@ export default function PurchaseForm() { console.log(stocksPortfolio) if (buyOrSell === "buy") { if (calculateTotal() > userBalance) { - setError(`You don't have enough money. Your balance is ${userBalance}`) + setMessage(`You don't have enough money. Your balance is ${userBalance}`) } else { API.saveBuyTransaction({ @@ -90,17 +92,18 @@ export default function PurchaseForm() { .then(res => console.log(res)) .catch(err => console.log(err)) - history.push("/gamestock/user") + setMessage(`Successfully bought ${formObject.amount} ${formObject.symbol.toUpperCase().trim()} shares`) + // history.push("/gamestock/user") } } if (buyOrSell === "sell") { console.log(foundStock) console.log(numberOfStocks) if (foundStock === "") { - setError(`You don't own any ${formObject.symbol.toUpperCase().trim()} shares`) + setMessage(`You don't own any ${formObject.symbol.toUpperCase().trim()} shares`) } else if (parseInt(formObject.amount) > parseInt(numberOfStocks)) { - setError(`You only have ${numberOfStocks} ${formObject.symbol.toUpperCase().trim()} shares to sell`) + setMessage(`You only have ${numberOfStocks} ${formObject.symbol.toUpperCase().trim()} shares to sell`) } else { API.saveSellTransaction({ email: currentUser.email, @@ -110,8 +113,8 @@ export default function PurchaseForm() { }) .then(data => console.log("selling info", data)) .catch(err => console.log(err)) - - history.push("/gamestock/user") + setMessage(`Successfully sold ${formObject.amount} ${formObject.symbol.toUpperCase().trim()} shares`) + // history.push("/gamestock/user") } } } @@ -121,7 +124,7 @@ export default function PurchaseForm() {

Trading Desk: Trade Shares Here

- {error && {error}} + {message && {message}}
Symbol @@ -137,7 +140,7 @@ export default function PurchaseForm() { name="amount" onChange={handleInputChange} /> -
Total: {calculateTotal()}
+
Total: {formObject.symbol && formObject.amount ? calculateTotal(): ""}