update swap component

This commit is contained in:
MuslemRahimi 2024-07-22 11:06:25 +02:00
parent 0af0e293b2
commit 86f407fc8a

View File

@ -25,25 +25,12 @@
function changeTab(index) { function changeTab(index) {
activeIdx = index; activeIdx = index;
switch (activeIdx) { optionsData = getPlotOptions(activeIdx === 0 ? 'effectiveDate' : 'expirationDate');
case 0:
optionsData = getPlotOptions('effectiveDate');
break;
case 1:
optionsData = getPlotOptions('expirationDate');
break;
// Default case in case changeType doesn't match any of the specified cases
default:
// Handle the default case or leave it empty if not needed
break;
} }
}
const usRegion = ['cle1','iad1','pdx1','sfo1']; const usRegion = ['cle1','iad1','pdx1','sfo1'];
let apiURL; let apiURL;
let apiKey = import.meta.env.VITE_STOCKNEAR_API_KEY;
userRegion.subscribe(value => { userRegion.subscribe(value => {
@ -197,73 +184,47 @@ function changeTab(index) {
return option; return option;
} }
const getSwapData = async (ticker) => { async function getSwapData(ticker) {
// Get cached data for the specific tickerID
const cachedData = getCache(ticker, 'getSwapData'); const cachedData = getCache(ticker, 'getSwapData');
if (cachedData) { if (cachedData) {
rawData = cachedData; rawData = cachedData;
} else { } else {
try {
const postData = {'ticker': ticker}; const response = await fetch(`${apiURL}/swap-ticker`, {
// make the POST request to the endpoint
const response = await fetch(apiURL + '/swap-ticker', {
method: 'POST', method: 'POST',
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": apiKey "Content-Type": "application/json",
"X-API-KEY": import.meta.env.VITE_STOCKNEAR_API_KEY
}, },
body: JSON.stringify(postData) body: JSON.stringify({ ticker })
}); });
if (!response.ok) throw new Error('API request failed');
rawData = (await response.json()); rawData = await response.json();
// Cache the data for this specific tickerID with a specific name 'getSwapData'
setCache(ticker, rawData, 'getSwapData'); setCache(ticker, rawData, 'getSwapData');
} catch (error) {
console.error('Failed to fetch swap data:', error);
rawData = [];
}
}
$swapComponent = rawData?.length !== 0;
} }
if(rawData?.length !== 0) {
$swapComponent = true;
} else {
$swapComponent = false;
}
};
$: if ($stockTicker && typeof window !== 'undefined') {
$: {
if($stockTicker && typeof window !== 'undefined') {
isLoaded = false; isLoaded = false;
activeIdx = 0; activeIdx = 0;
const asyncFunctions = [ getSwapData($stockTicker).then(() => {
getSwapData($stockTicker)
];
Promise.all(asyncFunctions)
.then((results) => {
optionsData = getPlotOptions('effectiveDate'); optionsData = getPlotOptions('effectiveDate');
})
.catch((error) => {
console.error('An error occurred:', error);
});
isLoaded = true; isLoaded = true;
});
}
}
let charNumber = 20;
$: {
if($screenWidth < 640)
{
charNumber = 20;
}
else {
charNumber =40;
}
} }
$: charNumber = $screenWidth < 640 ? 20 : 40;
</script> </script>
<svelte:options immutable />
<section class="overflow-hidden text-white h-full pb-8"> <section class="overflow-hidden text-white h-full pb-8">
<main class="overflow-hidden "> <main class="overflow-hidden ">