adding realtime price chart to stocks

This commit is contained in:
MuslemRahimi 2024-10-16 23:27:45 +02:00
parent 331ca49658
commit aa8882ffbf
2 changed files with 19 additions and 33 deletions

View File

@ -2,7 +2,7 @@ import { error, fail, redirect } from "@sveltejs/kit";
import { validateData } from "$lib/utils";
import { loginUserSchema, registerUserSchema } from "$lib/schemas";
export const load = async ({ locals, setHeaders }) => {
export const load = async ({ locals }) => {
const { apiKey, apiURL } = locals;
const getDashboard = async () => {
@ -16,8 +16,6 @@ export const load = async ({ locals, setHeaders }) => {
const output = await response?.json();
setHeaders({ "cache-control": "public, max-age=300" });
return output;
};

View File

@ -207,36 +207,24 @@ function handleTypeOfTrade(state:string)
const data = event.data;
//console.log('Received message:', data);
try {
$realtimePrice =
typeof JSON.parse(data)?.lp !== "undefined"
? JSON.parse(data)?.lp
: null;
$wsBidPrice =
typeof JSON.parse(data)?.bp !== "undefined"
? JSON.parse(data)?.bp
: null;
$wsAskPrice =
typeof JSON.parse(data)?.ap !== "undefined"
? JSON.parse(data)?.ap
: null;
//console.log("Received message:", JSON.parse(data));
$priceChartData = {
time:
typeof JSON.parse(data)?.time !== "undefined"
? JSON.parse(data)?.time
: null,
price:
typeof JSON.parse(data)?.lp !== "undefined"
? Number(JSON.parse(data)?.lp)
: null,
};
//console.log($priceChartData);
shouldUpdatePriceChart.set(true);
if ($realtimePrice > previousRealtimePrice) {
$priceIncrease = true;
previousRealtimePrice = $realtimePrice;
} else if ($realtimePrice < previousRealtimePrice) {
$priceIncrease = false;
const parsedData = JSON.parse(data);
const { type, lp, time, bp, ap } = parsedData || {};
if (type === "T") {
$realtimePrice = lp;
$priceChartData = {
time: time,
price: Number(lp),
};
shouldUpdatePriceChart.set(true);
} else if (type === "Q") {
$wsBidPrice = bp;
$wsAskPrice = ap;
}
// Update price increase state
if ($realtimePrice !== previousRealtimePrice) {
$priceIncrease = $realtimePrice > previousRealtimePrice;
previousRealtimePrice = $realtimePrice;
}