From f75dd3a41d8c53b00fb6c8a80098c7d33f39342e Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Mon, 9 Sep 2024 21:24:00 +0200 Subject: [PATCH] fix change --- src/routes/crypto/[tickerID]/+page.svelte | 130 ++++++++++------------ src/routes/etf/[tickerID]/+page.svelte | 95 ++++++++-------- 2 files changed, 106 insertions(+), 119 deletions(-) diff --git a/src/routes/crypto/[tickerID]/+page.svelte b/src/routes/crypto/[tickerID]/+page.svelte index cc24f86b..e918b065 100644 --- a/src/routes/crypto/[tickerID]/+page.svelte +++ b/src/routes/crypto/[tickerID]/+page.svelte @@ -3,25 +3,12 @@ import {AreaSeries, Chart, PriceLine, CandlestickSeries} from 'svelte-lightweight-charts'; import { TrackingModeExitMode } from 'lightweight-charts'; - import {setCache, getCache, screenWidth, taRatingComponent, displayCompanyName, numberOfUnreadNotification, globalForm, priceAnalysisComponent, trendAnalysisComponent, sentimentComponent, varComponent, userRegion, isCrosshairMoveActive, realtimePrice, priceIncrease, currentPortfolioPrice, currentPrice, clientSideCache, cryptoTicker} from '$lib/store'; - import { onDestroy, onMount } from 'svelte'; + import {setCache, getCache, screenWidth, taRatingComponent, displayCompanyName, numberOfUnreadNotification, globalForm, priceAnalysisComponent, trendAnalysisComponent, sentimentComponent, varComponent, isCrosshairMoveActive, realtimePrice, priceIncrease, currentPortfolioPrice, currentPrice, cryptoTicker} from '$lib/store'; + import { onDestroy } from 'svelte'; import CryptoKeyInformation from '$lib/components/CryptoKeyInformation.svelte'; import Lazy from '$lib/components/Lazy.svelte'; + import { Button } from "$lib/components/shadcn/button/index.js"; - - const usRegion = ['cle1','iad1','pdx1','sfo1']; - - let apiURL; -let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY; - - userRegion?.subscribe(value => { - if (usRegion?.includes(value)) { - apiURL = import.meta.env.VITE_USEAST_API_URL; - } else { - apiURL = import.meta.env.VITE_EU_API_URL; - } - }); - export let data; export let form; @@ -50,55 +37,56 @@ let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY; //==========================// -$: { - if (output !==null) - { - - //Bug value is NaN - if (displayData === '1D') - { - const length = oneDayPrice?.length; - for (let i = length - 1; i >= 0; i--) { - if (!isNaN(oneDayPrice[i]?.close)) { - currentDataRow = oneDayPrice[i]; - break; + $: { + if (output !== null) { + //Bug value is NaN + let change; + + if (displayData === "1D") { + const length = oneDayPrice?.length; + for (let i = length - 1; i >= 0; i--) { + if (!isNaN(oneDayPrice[i]?.close)) { + currentDataRow = oneDayPrice[i]; + break; + } } + } else if (displayData === "6M") { + currentDataRow = sixMonthPrice?.slice(-1)?.at(0); } + + //currentDataRow = oneWeekPrice.slice(-1)[0] + if(!$isCrosshairMoveActive && $realtimePrice !== null) { + change = (($realtimePrice/previousClose-1)*100)?.toFixed(2) + } else { + change = displayData === "1D" ? (((currentDataRow?.close ?? currentDataRow?.value) / previousClose - 1) * 100)?.toFixed(2) : (((currentDataRow?.close ?? currentDataRow?.value) / displayLastLogicalRangeValue - 1) * 100)?.toFixed(2); + } + + const date = new Date(currentDataRow?.time); + + const options = { + day: "2-digit", + month: "short", + year: "numeric", + hour: "numeric", + minute: "2-digit", + }; + + //const formattedDate = (displayData === '1D' || displayData === '1W' || displayData === '1M') ? date.toLocaleString('en-GB', options).replace(/\//g, '.') : date.toLocaleDateString('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric' }).replace(/\//g, '.'); + const formattedDate = displayData === "1D" || displayData === "1W" || displayData === "1M" + ? date.toLocaleString("en-US", options) + : date.toLocaleDateString("en-US", { day: "2-digit", month: "short", year: "numeric" }); + + const safeFormattedDate = formattedDate === "Invalid Date" ? convertTimestamp(data?.getStockQuote?.timestamp) : formattedDate; + displayLegend = { + close: currentDataRow?.value === '-' && currentDataRow?.close === undefined + ? data?.getStockQuote?.price + : (currentDataRow?.close ?? currentDataRow?.value), + date: safeFormattedDate, + change: change + }; + } - else if (displayData === '6M') { - currentDataRow = sixMonthPrice?.slice(-1)[0]; - } - - - //currentDataRow = oneWeekPrice.slice(-1)[0] - const change = (displayData === '1D') - ? (((currentDataRow?.close ?? currentDataRow?.value)/previousClose -1 )*100)?.toFixed(2) - : (((currentDataRow?.close ?? currentDataRow?.value)/displayLastLogicalRangeValue -1 )*100)?.toFixed(2) - - const date = new Date(currentDataRow?.time) - - const options = { - day: '2-digit', - month: 'short', - year: 'numeric', - hour: 'numeric', - minute: '2-digit', - }; - - - //const formattedDate = (displayData === '1D' || displayData === '1W' || displayData === '1M') ? date.toLocaleString('en-GB', options).replace(/\//g, '.') : date.toLocaleDateString('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric' }).replace(/\//g, '.'); - const formattedDate = (displayData === '1D' || displayData === '1W' || displayData === '1M') - ? date.toLocaleString('en-US', options) - : date.toLocaleDateString('en-US', { day: '2-digit', month: 'short', year: 'numeric' }); - - - displayLegend = {'close': currentDataRow?.close ?? currentDataRow?.value, 'date': formattedDate, 'change': change}; - - - } - -} //==========================// @@ -303,10 +291,10 @@ $: { timePeriod: timePeriod, }; - const response = await fetch(apiURL+'/historical-price', { + const response = await fetch(data?.apiURL+'/historical-price', { method: 'POST', headers: { - "Content-Type": "application/json", "X-API-KEY": apiKey + "Content-Type": "application/json", "X-API-KEY": data?.apiKey }, body: JSON.stringify(postData) }); @@ -795,14 +783,14 @@ afterUpdate(async () => { - - + + diff --git a/src/routes/etf/[tickerID]/+page.svelte b/src/routes/etf/[tickerID]/+page.svelte index f8732624..4e965ac6 100644 --- a/src/routes/etf/[tickerID]/+page.svelte +++ b/src/routes/etf/[tickerID]/+page.svelte @@ -79,57 +79,56 @@ //==========================// - $: { - if (output !==null) - { - - //Bug value is NaN - if (displayData === '1D') - { - const length = oneDayPrice?.length; - for (let i = length - 1; i >= 0; i--) { - if (!isNaN(oneDayPrice[i]?.close ?? oneDayPrice[i]?.value)) { - currentDataRow = oneDayPrice[i]; - break; - } - } + $: { + if (output !== null) { + //Bug value is NaN + let change; + + if (displayData === "1D") { + const length = oneDayPrice?.length; + for (let i = length - 1; i >= 0; i--) { + if (!isNaN(oneDayPrice[i]?.close)) { + currentDataRow = oneDayPrice[i]; + break; } - else if (displayData === '6M') { - currentDataRow = sixMonthPrice?.slice(-1)[0]; - } - - - //currentDataRow = oneWeekPrice.slice(-1)[0] - - const change = (displayData === '1D') - ? (((currentDataRow?.close ?? currentDataRow?.value)/previousClose -1 )*100)?.toFixed(2) - : (((currentDataRow?.close ?? currentDataRow?.value)/displayLastLogicalRangeValue -1 )*100)?.toFixed(2) - - - const date = new Date(currentDataRow?.time); - - - const options = { - day: '2-digit', - month: '2-digit', - year: 'numeric', - hour: '2-digit', - minute: '2-digit', - }; - - //const formattedDate = ( displayData === '1W' || displayData === '1M' ) ? date.toLocaleDateString('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric',hour: '2-digit', minute: '2-digit' }).replace(/\//g, '.') : date.toLocaleDateString('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric' }).replace(/\//g, '.'); - - const formattedDate = (displayData === '1D' || displayData === '1W' || displayData === '1M') ? date.toLocaleString('en-GB', options).replace(/\//g, '.') : date.toLocaleDateString('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric' }).replace(/\//g, '.'); - - - - - displayLegend = {'close': (currentDataRow?.close ?? currentDataRow?.value) , 'date': formattedDate, 'change': change}; - - } - + } else if (displayData === "6M") { + currentDataRow = sixMonthPrice?.slice(-1)?.at(0); } + + //currentDataRow = oneWeekPrice.slice(-1)[0] + if(!$isCrosshairMoveActive && $realtimePrice !== null) { + change = (($realtimePrice/previousClose-1)*100)?.toFixed(2) + } else { + change = displayData === "1D" ? (((currentDataRow?.close ?? currentDataRow?.value) / previousClose - 1) * 100)?.toFixed(2) : (((currentDataRow?.close ?? currentDataRow?.value) / displayLastLogicalRangeValue - 1) * 100)?.toFixed(2); + } + + const date = new Date(currentDataRow?.time); + + const options = { + day: "2-digit", + month: "short", + year: "numeric", + hour: "numeric", + minute: "2-digit", + }; + + //const formattedDate = (displayData === '1D' || displayData === '1W' || displayData === '1M') ? date.toLocaleString('en-GB', options).replace(/\//g, '.') : date.toLocaleDateString('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric' }).replace(/\//g, '.'); + const formattedDate = displayData === "1D" || displayData === "1W" || displayData === "1M" + ? date.toLocaleString("en-US", options) + : date.toLocaleDateString("en-US", { day: "2-digit", month: "short", year: "numeric" }); + + const safeFormattedDate = formattedDate === "Invalid Date" ? convertTimestamp(data?.getStockQuote?.timestamp) : formattedDate; + displayLegend = { + close: currentDataRow?.value === '-' && currentDataRow?.close === undefined + ? data?.getStockQuote?.price + : (currentDataRow?.close ?? currentDataRow?.value), + date: safeFormattedDate, + change: change + }; + + } + } //==========================//