Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Some small changes to showcase a bit of a cleaner API with some err… #2

Merged
merged 2 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/APIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,12 @@ export const lookupTicker = async (
}
};

export const buyAsset = async (symbol: string, shares: number, price:number) => {
export type BuyAssetResponse = {
reshortt marked this conversation as resolved.
Show resolved Hide resolved
successful:boolean;
remainingCash?:number;
}

export const buyAsset = async (symbol: string, shares: number, price:number): Promise<BuyAssetResponse> => {
// TODO: credentials not needed for stocklookup.remove
const requestOptions = createRequestAuthorization();

Expand All @@ -265,13 +270,14 @@ export const buyAsset = async (symbol: string, shares: number, price:number) =>
switch (response.status) {
case 200: {
const userResponseObj = await response.json();
return userResponseObj;
const cashRemainingAfterPurchase: number = userResponseObj.cash;
return {successful: true, remainingCash: cashRemainingAfterPurchase};
}

default:
// 500 is possible for critical server erropr
console.log("unexpected getAssets response");
return false;
console.log("unexpected buyAsset response");
return { successful: false };
}
};

Expand Down
22 changes: 12 additions & 10 deletions src/components/Buy.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChangeEvent, useEffect, useState, useRef } from "react";
import { buyAsset, getCash, getStockPrice, lookupTicker } from "../APIService";
import { buyAsset, BuyAssetResponse, getCash, getStockPrice, lookupTicker } from "../APIService";
import { formatCurrency } from "../Calculations";
import { COMMISSION, StockPrices } from "../types";

Expand Down Expand Up @@ -110,19 +110,21 @@ export default function Buy() {
".";
const isOK: boolean = window.confirm(msg);
if (isOK) {
const response = await buyAsset(
buyAsset(
typedSymbol,
sharesToBuy,
askPrice || 0
);
if (response) {
window.alert("Purchase confirmed. New cash is " + formatCurrency(response.cash))
window.location.assign("/positions")

} else {
window.alert("Purchase failed");
).then((buyAssetResponse: BuyAssetResponse)=>{
const { successful, remainingCash } = buyAssetResponse
if(successful){
window.alert(`Purchase executed! You have ${remainingCash} remaining.`);
}
}, ()=>{
window.alert("Purchase was not executed due to critical server error");
});
}
} else alert("Purchase cancelled");
} else{
window.alert("Purchase cancelled");
}
};

Expand Down