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" $priceChartData = {
? JSON.parse(data)?.bp time: time,
: null; price: Number(lp),
$wsAskPrice = };
typeof JSON.parse(data)?.ap !== "undefined" shouldUpdatePriceChart.set(true);
? JSON.parse(data)?.ap } else if (type === "Q") {
: null; $wsBidPrice = bp;
//console.log("Received message:", JSON.parse(data)); $wsAskPrice = ap;
$priceChartData = { }
time:
typeof JSON.parse(data)?.time !== "undefined" // Update price increase state
? JSON.parse(data)?.time if ($realtimePrice !== previousRealtimePrice) {
: null, $priceIncrease = $realtimePrice > previousRealtimePrice;
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;
previousRealtimePrice = $realtimePrice; previousRealtimePrice = $realtimePrice;
} }