Skip to content

Commit

Permalink
Add some loading spinners
Browse files Browse the repository at this point in the history
  • Loading branch information
h0i5 committed Jul 26, 2024
1 parent 5e652ac commit 2462283
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 51 deletions.
23 changes: 23 additions & 0 deletions app/components/Loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default function Loading() {
return (
<div role="status">
<svg
aria-hidden="true"
className="w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-[#037a68]"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
);
}
11 changes: 10 additions & 1 deletion app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useRouter } from "next/navigation";
var crypto = require("crypto");
import { setCookie } from "cookies-next";
import { NavTransition } from "../components/navbar/NavTransition";
import Loading from "../components/Loading";

export default function LoginPage() {
const router = useRouter();
Expand Down Expand Up @@ -49,6 +50,7 @@ export default function LoginPage() {
theme: "light",
transition: Slide,
});
const [loading, setLoading] = useState(false);

async function loginHandler(e: React.FormEvent) {
e.preventDefault();
Expand All @@ -70,6 +72,7 @@ export default function LoginPage() {
username: username,
password: hashedPassword,
});
setLoading(true);
} catch (err: any) {
results = err.response;
}
Expand Down Expand Up @@ -146,7 +149,13 @@ export default function LoginPage() {
</div>
<div className=" justify-center flex flex-col items-center w-full">
<button className="bg-[#037A68] hover:bg-teal-800 transition transition-all-0.5s text-white font-semibold p-2 mt-4 rounded-md">
Login
{loading ? (
<div className=" w-full h-full flex justify-center items-center">
<Loading /> <span className="ml-2"> Login </span>
</div>
) : (
"Login"
)}
</button>
<p className="text-sm mt-2">
Don&apos;t have an account?{" "}
Expand Down
2 changes: 1 addition & 1 deletion app/portfolio/sections/Networth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default function Networth(props: any) {
</h1>

<div className="ml-2 font-extrabold text-base">
{profitDetails?.overallProfit > 0 ? (
{profitDetails?.overallProfit >= 0 ? (
<span className="green-text ">
{" "}
+{profitDetails?.overallProfit.toFixed(2)}{" "}
Expand Down
16 changes: 14 additions & 2 deletions app/signup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var crypto = require("crypto");
import { Slide, ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import { NavTransition } from "../components/navbar/NavTransition";
import Loading from "../components/Loading";

const notifySuccess = (message: string) =>
toast.success(message, {
Expand Down Expand Up @@ -39,6 +40,8 @@ export default function SignUpPage() {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");

const [loading, setLoading] = useState(false);

async function handleFormSubmit(e: React.FormEvent) {
e.preventDefault();
if (username === "" || email === "" || password === "") {
Expand All @@ -57,11 +60,14 @@ export default function SignUpPage() {
.digest("hex");
let results;
try {
setLoading(true);

results = await axios.post(`${apiURL}/register`, {
username: username,
email: email,
password: hashedPassword,
});
setLoading(false);
} catch (err: any) {
results = err.response;
}
Expand Down Expand Up @@ -139,9 +145,15 @@ export default function SignUpPage() {
onChange={(e) => setPassword(e.target.value)}
/>
</div>
<div className="flex justify-center flex flex-col items-center w-full">
<div className=" justify-center flex flex-col items-center w-full">
<button className="bg-[#037A68] hover:bg-teal-800 transition transition-all-0.5s text-white font-semibold p-2 mt-4 rounded-md">
Register
{loading ? (
<div className=" w-full h-full flex justify-center items-center">
<Loading /> <span className="ml-2"> Register </span>
</div>
) : (
"Register"
)}{" "}
</button>
<p className="text-sm mt-2">
Already have an account?{" "}
Expand Down
11 changes: 10 additions & 1 deletion app/stocks/[...symbol]/components/handlers/BuyPopup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { apiURL } from "@/app/components/apiURL";
import Loading from "@/app/components/Loading";
import axios from "axios";
import { getCookie } from "cookies-next";
import { useState } from "react";
Expand Down Expand Up @@ -45,6 +46,7 @@ export default function BuyPopup(props: any) {
return;
}
}
const [loading, setLoading] = useState(false);

async function handleBuy(e: any) {
e.preventDefault();
Expand All @@ -63,6 +65,7 @@ export default function BuyPopup(props: any) {
quantity: quantity,
},
});
setLoading(false);
} catch (err: any) {
results = err.response;
}
Expand Down Expand Up @@ -100,7 +103,13 @@ export default function BuyPopup(props: any) {
</h1>
<div className="flex justify-center items-center">
<button className="mt-2 flex w-fit px-4 text-xl font-semibold p-2 bg-[#037A68] text-white rounded-md">
Buy
{loading ? (
<div className=" w-full h-full flex justify-center items-center">
<Loading /> <span className="ml-2"> Buy </span>
</div>
) : (
"Buy"
)}
</button>
</div>
</div>
Expand Down
11 changes: 10 additions & 1 deletion app/stocks/[...symbol]/components/handlers/SellPopup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { apiURL } from "@/app/components/apiURL";
import Loading from "@/app/components/Loading";
import axios from "axios";
import { getCookie } from "cookies-next";
import { useState } from "react";
Expand Down Expand Up @@ -43,6 +44,7 @@ export default function SellPopup(props: any) {
return;
}
}
const [loading, setLoading] = useState(false);

async function handleSell(e: any) {
e.preventDefault();
Expand All @@ -61,6 +63,7 @@ export default function SellPopup(props: any) {
quantity: quantity,
},
});
setLoading(false);
} catch (err: any) {
results = err.response;
}
Expand Down Expand Up @@ -97,7 +100,13 @@ export default function SellPopup(props: any) {
</h1>
<div className="flex justify-center items-center">
<button className="mt-2 flex w-fit px-4 text-xl font-semibold p-2 bg-[#037A68] text-white rounded-md">
Sell
{loading ? (
<div className=" w-full h-full flex justify-center items-center">
<Loading /> <span className="ml-2"> Sell </span>
</div>
) : (
"Sell"
)}
</button>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { apiURL } from "@/app/components/apiURL";
import axios from "axios";
import { toast, Slide } from "react-toastify";
import { getCookie } from "cookies-next";
import { useState } from "react";

export default function BuySellWatch(props: any) {
let symbol = props.symbol || "";
Expand Down
82 changes: 52 additions & 30 deletions app/stocks/[...symbol]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Link from "next/link";
import LTP from "./components/sections/LTP";
import Stats from "./components/sections/Statistics/Stats";
import BuySellWatch from "./components/sections/BuySellWatch";
import Loading from "@/app/components/Loading";
export const runtime = "edge";
export default function Page({
params,
Expand Down Expand Up @@ -55,13 +56,16 @@ export default function Page({
items: [],
},
});
const [loading, setLoading] = useState(true);

useEffect(() => {
async function getStockData() {
let data;
try {
data = await axios.post(`${apiURL}/getStockQuote`, {
symbol: btoa(decodeURIComponent(symbol)),
});
setLoading(false);
} catch (err) {
console.error(err);
}
Expand Down Expand Up @@ -121,13 +125,19 @@ export default function Page({
<div className="flex w-full md:flex-row justify-between">
<div className="xl:w-[60%]">
<div>
<LTP
symbol={symbol}
ltp={stockData.ltp}
dayChange={stockData.dayChange}
dayChangePerc={stockData.dayChangePerc}
companyName={companyName}
/>
{loading ? (
<div className="w-full h-full flex justify-center items-center">
<Loading />
</div>
) : (
<LTP
symbol={symbol}
ltp={stockData.ltp}
dayChange={stockData.dayChange}
dayChangePerc={stockData.dayChangePerc}
companyName={companyName}
/>
)}
</div>
<div className="mt-6">
<div className="md:w-full">
Expand All @@ -142,33 +152,45 @@ export default function Page({
/>
</div>
<div className="mt-6">
<Stats
symbol={symbol}
open={stockData.open}
close={stockData.close}
ltp={stockData.ltp}
high={stockData.high}
low={stockData.low}
volume={stockData.volume}
tsInMillis={stockData.tsInMillis}
lowPriceRange={stockData.lowPriceRange}
highPriceRange={stockData.highPriceRange}
totalBuyQty={stockData.totalBuyQty}
totalSellQty={stockData.totalSellQty}
lastTradeQty={stockData.lastTradeQty}
lastTradeTime={stockData.lastTradeTime}
dayChange={stockData.dayChange}
dayChangePerc={stockData.dayChangePerc}
oiDayChange={stockData.oiDayChange}
oiDayChangePerc={stockData.oiDayChangePerc}
lowTradeRange={stockData.lowTradeRange}
highTradeRange={stockData.highTradeRange}
/>
{loading ? (
<div className="w-full h-full flex justify-center items-center">
<Loading />
</div>
) : (
<Stats
symbol={symbol}
open={stockData.open}
close={stockData.close}
ltp={stockData.ltp}
high={stockData.high}
low={stockData.low}
volume={stockData.volume}
tsInMillis={stockData.tsInMillis}
lowPriceRange={stockData.lowPriceRange}
highPriceRange={stockData.highPriceRange}
totalBuyQty={stockData.totalBuyQty}
totalSellQty={stockData.totalSellQty}
lastTradeQty={stockData.lastTradeQty}
lastTradeTime={stockData.lastTradeTime}
dayChange={stockData.dayChange}
dayChangePerc={stockData.dayChangePerc}
oiDayChange={stockData.oiDayChange}
oiDayChangePerc={stockData.oiDayChangePerc}
lowTradeRange={stockData.lowTradeRange}
highTradeRange={stockData.highTradeRange}
/>
)}
</div>
</div>
<div className="hidden grow xl:flex flex-row justify-end">
<div className="flex mb-auto ml-auto">
<TopMoversColumn data={topMovers} />
{loading ? (
<div className="w-full h-full flex justify-center items-center">
<Loading />
</div>
) : (
<TopMoversColumn data={topMovers} />
)}
</div>
</div>
</div>
Expand Down
28 changes: 19 additions & 9 deletions app/topmovers/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import TopVolume from "./components/sections/TopVolume";
import axios from "axios";
import { apiURL } from "../components/apiURL";
import { NavTransition } from "../components/navbar/NavTransition";
import Loading from "../components/Loading";

export default function TopMovers() {
const [topMovers, setTopMovers] = useState({
Expand All @@ -21,13 +22,16 @@ export default function TopMovers() {
},
});

const [loading, setLoading] = useState(true);

useEffect(() => {
async function getTopMoverData() {
let data;
try {
data = await axios.post(`${apiURL}/topmovers`, {
size: 10,
});
setLoading(false);
} catch (err) {
console.error(err);
}
Expand Down Expand Up @@ -87,15 +91,21 @@ export default function TopMovers() {
</div>
</div>
</div>
<div className="mx-6 md:mx-0 mt-8">
{display === "Gainers" ? (
<TopGainers type="Gainers" apiData={topMovers.TOP_GAINERS.items} />
) : display === "Losers" ? (
<TopLosers type="Losers" apiData={topMovers.TOP_LOSERS.items} />
) : (
<TopVolume type="Volume" apiData={topMovers.TOP_VOLUME.items} />
)}
</div>
{loading ? (
<div className=" w-full h-full flex justify-center items-center">
<Loading />
</div>
) : (
<div className="mx-6 md:mx-0 mt-8">
{display === "Gainers" ? (
<TopGainers type="Gainers" apiData={topMovers.TOP_GAINERS.items} />
) : display === "Losers" ? (
<TopLosers type="Losers" apiData={topMovers.TOP_LOSERS.items} />
) : (
<TopVolume type="Volume" apiData={topMovers.TOP_VOLUME.items} />
)}
</div>
)}
</div>
);
}
Loading

0 comments on commit 2462283

Please sign in to comment.