From 0bdf5b1becc215d54ff343de112740e46ce3f508 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sat, 8 Jun 2024 21:15:10 +0200 Subject: [PATCH] update --- src/routes/analysts/+page.server.ts | 37 --------------------- src/routes/analysts/+page.ts | 50 +++++++++++++++++++++++++++++ src/routes/stocks/+page.ts | 1 - 3 files changed, 50 insertions(+), 38 deletions(-) delete mode 100644 src/routes/analysts/+page.server.ts create mode 100644 src/routes/analysts/+page.ts diff --git a/src/routes/analysts/+page.server.ts b/src/routes/analysts/+page.server.ts deleted file mode 100644 index c026ff92..00000000 --- a/src/routes/analysts/+page.server.ts +++ /dev/null @@ -1,37 +0,0 @@ -const usRegion = ['cle1','iad1','pdx1','sfo1']; - - -export const load = async ({locals}) => { - - const userRegion = locals.region?.split("::")[0]; - - let apiURL; - - if (usRegion?.includes(userRegion)) { - apiURL = import.meta.env.VITE_USEAST_API_URL; - } else { - apiURL = import.meta.env.VITE_EU_API_URL; - }; - - const getTopAnalyst = async () => { - // make the POST request to the endpoint - const response = await fetch(apiURL + '/top-analysts', { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - }, - }); - - let output = await response.json(); - - output = locals?.user?.tier !== 'Pro' ? output?.reverse()?.slice(0,6) : output; - - - return output; - }; - - // Make sure to return a promise - return { - getTopAnalyst: await getTopAnalyst() - }; -}; \ No newline at end of file diff --git a/src/routes/analysts/+page.ts b/src/routes/analysts/+page.ts new file mode 100644 index 00000000..e7edd20a --- /dev/null +++ b/src/routes/analysts/+page.ts @@ -0,0 +1,50 @@ +import { userRegion, getCache, setCache } from '$lib/store'; + + +const usRegion = ['cle1','iad1','pdx1','sfo1']; + +let apiURL; + +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 const load = async ( { parent } ) => { + + const getTopAnalyst = async () => { + const data = await parent() + let output; + + const cachedData = getCache('', 'getTopAnalyst'); + if (cachedData) { + output = cachedData; + } else { + + const response = await fetch(apiURL + '/top-analysts', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + output = await response?.json(); + setCache('', output, 'getTopAnalyst'); + } + + output = data?.user?.tier !== 'Pro' ? output?.reverse()?.slice(0,6) : output; + + return output; + }; + + // Make sure to return a promise + return { + getTopAnalyst: await getTopAnalyst() + }; +}; \ No newline at end of file diff --git a/src/routes/stocks/+page.ts b/src/routes/stocks/+page.ts index 3e9740f7..0622535b 100644 --- a/src/routes/stocks/+page.ts +++ b/src/routes/stocks/+page.ts @@ -19,7 +19,6 @@ userRegion.subscribe(value => { export const load = async () => { const getStockList = async () => { let output; - // Get cached data for the specific tickerID const cachedData = getCache('', 'getStockList'); if (cachedData) {