lazy load dcf component

This commit is contained in:
MuslemRahimi 2024-07-26 16:01:07 +02:00
parent e409213327
commit 1f153f055f
5 changed files with 76 additions and 41 deletions

View File

@ -2,17 +2,16 @@
import { Chart } from 'svelte-echarts'
import InfoModal from '$lib/components/InfoModal.svelte';
import {currentPortfolioPrice, stockTicker, screenWidth} from '$lib/store';
import {dcfComponent, stockTicker, screenWidth, getCache, setCache} from '$lib/store';
import Lazy from 'svelte-lazy';
//export let quantData;
export let fairPrice;
export let data;
let fairPrice;
let isLoaded = false;
let lastPrice:Number;
let optionsBarChart;
let change:Number;
@ -186,23 +185,61 @@
}
$: {
if ($stockTicker && typeof window !== 'undefined')
{
lastPrice = Number($currentPortfolioPrice);
change = ( (1 - fairPrice/lastPrice ) * 100)?.toFixed(2);
optionsBarChart = plotBarChart()
}
async function getFairPrice(ticker) {
const cachedData = getCache(ticker, 'getFairPrice');
if (cachedData) {
fairPrice = cachedData;
} else {
try {
const response = await fetch(data?.apiURL+'/fair-price', {
method: 'POST',
headers: {
"Content-Type": "application/json",
"X-API-KEY": data?.apiKey
},
body: JSON.stringify({ ticker })
});
fairPrice = await response.json();
setCache(ticker, fairPrice, 'getFairPrice');
} catch (error) {
console.error('Failed to fetch swap data:', error);
fairPrice = null;
}
}
if (fairPrice !== null) {
$dcfComponent = true;
}
else {
$dcfComponent = false;
}
}
$: {
if ($stockTicker && typeof window !== 'undefined')
{
isLoaded = false;
getFairPrice($stockTicker).then(() => {
if (fairPrice !== null) {
lastPrice = data?.getStockQuote?.price?.toFixed(2);
change = ( (1 - fairPrice/lastPrice ) * 100)?.toFixed(2);
optionsBarChart = plotBarChart()
}
isLoaded = true;
});
}
}
</script>
@ -221,8 +258,9 @@
</div>
{#if data?.user?.tier === 'Pro'}
{#if isLoaded}
{#if fairPrice !== null}
<div class="p-3 sm:p-0 mt-2 pb-8 sm:pb-2 rounded-lg bg-[#09090B] sm:bg-[#09090B]">
<div class="mt-4 text-white text-[1rem] sm:text-xl pb-4 sm:pb-0 m-auto text-start">
@ -287,16 +325,21 @@
</div>
{:else}
<h2 class=" mt-10 justify-center items-center text-3xl font-bold text-slate-700 mb-5 m-auto">
No data available
<svg class="w-10 sm:w-12 inline-block" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="#334155" d="M18.68 12.32a4.49 4.49 0 0 0-6.36.01a4.49 4.49 0 0 0 0 6.36a4.508 4.508 0 0 0 5.57.63L21 22.39L22.39 21l-3.09-3.11c1.13-1.77.87-4.09-.62-5.57m-1.41 4.95c-.98.98-2.56.97-3.54 0c-.97-.98-.97-2.56.01-3.54c.97-.97 2.55-.97 3.53 0c.97.98.97 2.56 0 3.54M10.9 20.1a6.527 6.527 0 0 1-1.48-2.32C6.27 17.25 4 15.76 4 14v3c0 2.21 3.58 4 8 4c-.4-.26-.77-.56-1.1-.9M4 9v3c0 1.68 2.07 3.12 5 3.7v-.2c0-.93.2-1.85.58-2.69C6.34 12.3 4 10.79 4 9m8-6C7.58 3 4 4.79 4 7c0 2 3 3.68 6.85 4h.05c1.2-1.26 2.86-2 4.6-2c.91 0 1.81.19 2.64.56A3.215 3.215 0 0 0 20 7c0-2.21-3.58-4-8-4Z"/></svg>
</h2>
{/if}
{/if}
{:else}
<div class="flex justify-center items-center h-80">
<div class="relative">
<label class="bg-[#09090B] rounded-xl h-14 w-14 flex justify-center items-center absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2">
<span class="loading loading-spinner loading-md"></span>
</label>
</div>
</div>
{/if}
{:else}
<div class="shadow-lg shadow-bg-[#000] bg-[#111112] sm:bg-opacity-[0.5] text-sm sm:text-[1rem] rounded-md w-full p-4 min-h-24 mt-4 text-white m-auto flex justify-center items-center text-center font-semibold">
<svg class="mr-1.5 w-5 h-5 inline-block"xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="#A3A3A3" d="M17 9V7c0-2.8-2.2-5-5-5S7 4.2 7 7v2c-1.7 0-3 1.3-3 3v7c0 1.7 1.3 3 3 3h10c1.7 0 3-1.3 3-3v-7c0-1.7-1.3-3-3-3M9 7c0-1.7 1.3-3 3-3s3 1.3 3 3v2H9z"/></svg>

View File

@ -78,7 +78,6 @@ async function getTaRating(ticker) {
else {
$taRatingComponent = false;
}
console.log($taRatingComponent)
}

View File

@ -80,6 +80,7 @@ export const governmentContractComponent= writable(<boolean>(false));
export const analystInsightComponent= writable(<boolean>(false));
export const swapComponent= writable(<boolean>(false));
export const taRatingComponent= writable(<boolean>(false));
export const dcfComponent= writable(<boolean>(false));

View File

@ -134,7 +134,6 @@ export const load = async ({ params, locals, cookies, setHeaders}) => {
const promises = [
fetchData(apiURL,apiKey, '/fair-price',params.tickerID),
fetchData(apiURL,apiKey, '/similar-stocks',params.tickerID),
fetchData(apiURL,apiKey, '/stockdeck',params.tickerID),
fetchData(apiURL,apiKey, '/stock-correlation',params.tickerID),
@ -150,7 +149,6 @@ export const load = async ({ params, locals, cookies, setHeaders}) => {
];
const [
getFairPrice,
getSimilarStock,
getStockDeck,
getCorrelation,
@ -172,7 +170,6 @@ export const load = async ({ params, locals, cookies, setHeaders}) => {
return {
getFairPrice,
getSimilarStock,
getStockDeck,
getCorrelation,

View File

@ -3,7 +3,7 @@
import {AreaSeries, Chart, PriceLine, CandlestickSeries} from 'svelte-lightweight-charts';
import { TrackingModeExitMode } from 'lightweight-charts';
import {getCache, setCache, taRatingComponent, swapComponent, analystInsightComponent, governmentContractComponent, optionsNetFlowComponent, impliedVolatilityComponent, borrowedShareComponent, clinicalTrialComponent, optionComponent, failToDeliverComponent, marketMakerComponent, analystEstimateComponent, sentimentComponent, screenWidth, displayCompanyName, numberOfUnreadNotification, globalForm, varComponent, shareStatisticsComponent, enterpriseComponent, darkPoolComponent, retailVolumeComponent, shareholderComponent, trendAnalysisComponent, revenueSegmentationComponent, priceAnalysisComponent, fundamentalAnalysisComponent, isCrosshairMoveActive, realtimePrice, priceIncrease, currentPortfolioPrice, currentPrice, stockTicker, isOpen, isBeforeMarketOpen, isWeekend} from '$lib/store';
import {getCache, setCache, dcfComponent, taRatingComponent, swapComponent, analystInsightComponent, governmentContractComponent, optionsNetFlowComponent, impliedVolatilityComponent, borrowedShareComponent, clinicalTrialComponent, optionComponent, failToDeliverComponent, marketMakerComponent, analystEstimateComponent, sentimentComponent, screenWidth, displayCompanyName, numberOfUnreadNotification, globalForm, varComponent, shareStatisticsComponent, enterpriseComponent, darkPoolComponent, retailVolumeComponent, shareholderComponent, trendAnalysisComponent, revenueSegmentationComponent, priceAnalysisComponent, fundamentalAnalysisComponent, isCrosshairMoveActive, realtimePrice, priceIncrease, currentPortfolioPrice, currentPrice, stockTicker, isOpen, isBeforeMarketOpen, isWeekend} from '$lib/store';
import { onDestroy, onMount } from 'svelte';
import BullBearSay from '$lib/components/BullBearSay.svelte';
import CommunitySentiment from '$lib/components/CommunitySentiment.svelte';
@ -16,7 +16,6 @@
let displayChartType = 'line';
let stockDeck = data?.getStockDeck ?? [];
let fairPrice = data?.getFairPrice ?? [];
let correlationList = data?.getCorrelation?.correlation ?? [];
let prePostData = {};
let similarstock = [];
@ -664,7 +663,6 @@ function changeChartType() {
output = null;
fairPrice = data?.getFairPrice;
stockDeck = data?.getStockDeck;
correlationList = data?.getCorrelation?.correlation;
marketMoods = data?.getBullBearSay;
@ -1434,14 +1432,11 @@ function changeChartType() {
<!--Start DCF -->
<div class="w-full pt-10 sm:pl-6 sm:pb-6 sm:pt-6 m-auto {fairPrice !== null && fairPrice > 0 ? '' : 'hidden'}">
<Lazy>
<div class="w-full m-auto rounded-2xl mb-5">
{#await import('$lib/components/DCF.svelte') then {default: Comp}}
<svelte:component this={Comp} data = {data} fairPrice = {fairPrice} currentPrice = {currentPrice}/>
{/await}
</div>
</Lazy>
<div class="w-full pt-10 sm:pl-6 sm:pb-6 sm:pt-6 m-auto {!$dcfComponent ? 'hidden' : ''}">
{#await import('$lib/components/DCF.svelte') then {default: Comp}}
<svelte:component this={Comp} data={data}/>
{/await}
</div>
<!--End DCF-->