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

View File

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