diff --git a/src/routes/stocks/[tickerID]/financials/+layout.svelte b/src/routes/stocks/[tickerID]/financials/+layout.svelte index d86aedb4..18f583e7 100644 --- a/src/routes/stocks/[tickerID]/financials/+layout.svelte +++ b/src/routes/stocks/[tickerID]/financials/+layout.svelte @@ -72,6 +72,10 @@ Object.keys(sectionMap).find( (key) => sectionMap[key] === foundSection, ) || "income"; + + if (displaySubSection === "ratios" && $selectedTimePeriod === "ttm") { + $selectedTimePeriod = "annual"; + } } } @@ -156,16 +160,18 @@ > Quarterly - updateQuery("ttm")} - class="hidden sm:block p-2 px-5 cursor-pointer {$selectedTimePeriod === - 'ttm' - ? 'text-muted dark:text-white bg-[#EEEEEE] dark:bg-primary/90 font-semibold' - : 'text-blue-700 dark:text-gray-400 sm:hover:text-muted dark:sm:hover:text-white sm:hover:bg-[#EEEEEE] dark:sm:hover:bg-primary/90'}" - > - TTM - + {#if displaySubSection !== "ratios"} + updateQuery("ttm")} + class="hidden sm:block p-2 px-5 cursor-pointer {$selectedTimePeriod === + 'ttm' + ? 'text-muted dark:text-white bg-[#EEEEEE] dark:bg-primary/90 font-semibold' + : 'text-blue-700 dark:text-gray-400 sm:hover:text-muted dark:sm:hover:text-white sm:hover:bg-[#EEEEEE] dark:sm:hover:bg-primary/90'}" + > + TTM + + {/if} diff --git a/src/routes/stocks/[tickerID]/financials/+page.svelte b/src/routes/stocks/[tickerID]/financials/+page.svelte index 5ec5170d..f4baf749 100644 --- a/src/routes/stocks/[tickerID]/financials/+page.svelte +++ b/src/routes/stocks/[tickerID]/financials/+page.svelte @@ -452,7 +452,9 @@ {"FY" + " " + item?.fiscalYear} {:else} - + {item?.period + " " + item?.fiscalYear} {/if} diff --git a/src/routes/stocks/[tickerID]/financials/balance-sheet/+page.server.ts b/src/routes/stocks/[tickerID]/financials/balance-sheet/+page.server.ts index 759e5848..5aa53817 100644 --- a/src/routes/stocks/[tickerID]/financials/balance-sheet/+page.server.ts +++ b/src/routes/stocks/[tickerID]/financials/balance-sheet/+page.server.ts @@ -1,14 +1,12 @@ -import { getCache, setCache } from "$lib/store"; - export const load = async ({ locals, params }) => { const getData = async () => { const { apiKey, apiURL } = locals; const postData = { ticker: params.tickerID, + statement: 'balance-sheet-statement', }; - // make the POST request to the endpoint - const response = await fetch(apiURL + "/stock-balance-sheet", { + const response = await fetch(apiURL + "/financial-statement", { method: "POST", headers: { "Content-Type": "application/json", @@ -22,6 +20,8 @@ export const load = async ({ locals, params }) => { return output; }; + + // Make sure to return a promise return { getData: await getData(), diff --git a/src/routes/stocks/[tickerID]/financials/balance-sheet/+page.svelte b/src/routes/stocks/[tickerID]/financials/balance-sheet/+page.svelte index 3995c4af..e56b5518 100644 --- a/src/routes/stocks/[tickerID]/financials/balance-sheet/+page.svelte +++ b/src/routes/stocks/[tickerID]/financials/balance-sheet/+page.svelte @@ -4,6 +4,7 @@ stockTicker, coolMode, timeFrame, + selectedTimePeriod, } from "$lib/store"; import { removeCompanyStrings } from "$lib/utils"; import * as DropdownMenu from "$lib/components/shadcn/dropdown-menu/index.js"; @@ -16,25 +17,11 @@ export let data; - let isLoaded = true; let tableList = []; let processedData = {}; let financialData = []; let fullStatement = []; - let filterRule = "annual"; - let displayStatement = "revenue"; - - let activeIdx = 0; - - const tabs = [ - { - title: "Annual", - }, - { - title: "Quarterly", - }, - ]; const statementConfig = [ { @@ -229,7 +216,6 @@ }; fullStatement = data?.getData; - displayStatement = "revenue"; const exportFundamentalData = (format = "csv") => { if (["Pro", "Plus"]?.includes(data?.user?.tier)) { @@ -240,8 +226,8 @@ let properties = [ { - key: filterRule === "annual" ? "fiscalYear" : "date", - label: filterRule === "annual" ? "Year" : "Quarter", + key: $selectedTimePeriod === "annual" ? "fiscalYear" : "date", + label: $selectedTimePeriod === "annual" ? "Year" : "Quarter", }, ]; @@ -274,7 +260,7 @@ a.href = url; a.download = $stockTicker.toLowerCase() + - `${filterRule === "annual" ? "_annual" : "_quarter"}_balance_sheet_statement.csv`; + `${$selectedTimePeriod === "annual" ? "_annual" : $selectedTimePeriod === "quarterly" ? "_quarter" : "_ttm"}_balance_sheet_statement.csv`; document.body.appendChild(a); a.click(); document.body.removeChild(a); @@ -297,13 +283,16 @@ } }); + // Precompute xList from income (reverse order) const xList = []; for (let i = financialData.length - 1; i >= 0; i--) { const statement = financialData[i]; const year = statement.fiscalYear.slice(-2); const quarter = statement.period; xList.push( - filterRule === "annual" ? "FY" + year : "FY" + year + " " + quarter, + $selectedTimePeriod === "annual" + ? "FY" + year + : "FY" + year + " " + quarter, ); } @@ -331,7 +320,7 @@ }); // Build tableList once for all charts and sort by date (newest first) - tableList = financialData.map((statement) => ({ + tableList = financialData?.map((statement) => ({ date: statement.date, // Add more properties if needed })); @@ -340,14 +329,17 @@ } $: { - if ($timeFrame || activeIdx) { - if (activeIdx === 0) { - filterRule = "annual"; + if ($timeFrame || $selectedTimePeriod) { + if ($selectedTimePeriod === "annual") { fullStatement = data?.getData?.annual; - } else { - filterRule = "quarterly"; + } else if ($selectedTimePeriod === "quarterly") { fullStatement = data?.getData?.quarter; + } else if ($selectedTimePeriod === "ttm") { + fullStatement = data?.getData?.ttm; + } else { + fullStatement = data?.getData?.annual; } + financialData = filterStatement(fullStatement, $timeFrame); preprocessFinancialData(); } @@ -355,8 +347,8 @@
@@ -367,113 +359,70 @@

- {removeCompanyStrings($displayCompanyName)} Balance Sheet Statement + {removeCompanyStrings($displayCompanyName)} Income Statement

-
- {#each tabs as item, i} - {#if !["Pro", "Plus"]?.includes(data?.user?.tier) && i > 0} - - {:else} - - {/if} - {/each} -
+
{#if financialData?.length > 0}
- - -
- + +
- {#if $coolMode}
{#each fields as item, i} @@ -526,7 +497,7 @@ data={financialData} {statementConfig} displayStatement={item?.key} - {filterRule} + filterRule={$selectedTimePeriod} {processedData} color={["#ff00cc", "#37ff00", "#0c63e7", "#07c8f9"][ i % 4 @@ -535,91 +506,6 @@ {/each}
{:else} -
- - Financials in {data?.getProfileData?.currency}. Fiscal year - is - {data?.getProfileData?.fiscalYearRange}. - -
-
- - - - - - - Select time frame - - - - ($timeFrame = "5Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 5 years - - ($timeFrame = "10Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 10 years - - ($timeFrame = "MAX")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - Max - - - - -
- -
-
@@ -632,14 +518,16 @@ >Fiscal Year {#each financialData as item} - {#if filterRule === "annual"} + {#if $selectedTimePeriod === "annual"} {"FY" + " " + item?.fiscalYear} {:else} - + {item?.period + " " + item?.fiscalYear} {/if} diff --git a/src/routes/stocks/[tickerID]/financials/cash-flow/+page.server.ts b/src/routes/stocks/[tickerID]/financials/cash-flow/+page.server.ts index c747278f..391b4596 100644 --- a/src/routes/stocks/[tickerID]/financials/cash-flow/+page.server.ts +++ b/src/routes/stocks/[tickerID]/financials/cash-flow/+page.server.ts @@ -1,13 +1,12 @@ export const load = async ({ locals, params }) => { const getData = async () => { const { apiKey, apiURL } = locals; - const postData = { ticker: params.tickerID, + statement: 'cash-flow-statement', }; - // make the POST request to the endpoint - const response = await fetch(apiURL + "/stock-cash-flow", { + const response = await fetch(apiURL + "/financial-statement", { method: "POST", headers: { "Content-Type": "application/json", @@ -21,6 +20,8 @@ export const load = async ({ locals, params }) => { return output; }; + + // Make sure to return a promise return { getData: await getData(), diff --git a/src/routes/stocks/[tickerID]/financials/cash-flow/+page.svelte b/src/routes/stocks/[tickerID]/financials/cash-flow/+page.svelte index 3502cd64..18c7cfd5 100644 --- a/src/routes/stocks/[tickerID]/financials/cash-flow/+page.svelte +++ b/src/routes/stocks/[tickerID]/financials/cash-flow/+page.svelte @@ -4,6 +4,7 @@ stockTicker, coolMode, timeFrame, + selectedTimePeriod, } from "$lib/store"; import { removeCompanyStrings } from "$lib/utils"; import * as DropdownMenu from "$lib/components/shadcn/dropdown-menu/index.js"; @@ -16,24 +17,11 @@ export let data; - let isLoaded = true; let tableList = []; let processedData = {}; let financialData = []; let fullStatement = []; - let filterRule = "annual"; - - let activeIdx = 0; - - const tabs = [ - { - title: "Annual", - }, - { - title: "Quarterly", - }, - ]; const statementConfig = [ { @@ -163,7 +151,6 @@ text: "Free cash flow is the cash remaining after the company spends on everything required to maintain and grow the business. It is calculated by subtracting capital expenditures from operating cash flow.", }, ]; - const fields = statementConfig.map((item) => ({ label: item.label, key: item.propertyName, @@ -202,8 +189,8 @@ let properties = [ { - key: filterRule === "annual" ? "fiscalYear" : "date", - label: filterRule === "annual" ? "Year" : "Quarter", + key: $selectedTimePeriod === "annual" ? "fiscalYear" : "date", + label: $selectedTimePeriod === "annual" ? "Year" : "Quarter", }, ]; @@ -236,7 +223,7 @@ a.href = url; a.download = $stockTicker.toLowerCase() + - `${filterRule === "annual" ? "_annual" : "_quarter"}_cash_flow_statement.csv`; + `${$selectedTimePeriod === "annual" ? "_annual" : $selectedTimePeriod === "quarterly" ? "_quarter" : "_ttm"}_cash_flow_statement.csv`; document.body.appendChild(a); a.click(); document.body.removeChild(a); @@ -259,13 +246,16 @@ } }); + // Precompute xList from income (reverse order) const xList = []; for (let i = financialData.length - 1; i >= 0; i--) { const statement = financialData[i]; const year = statement.fiscalYear.slice(-2); const quarter = statement.period; xList.push( - filterRule === "annual" ? "FY" + year : "FY" + year + " " + quarter, + $selectedTimePeriod === "annual" + ? "FY" + year + : "FY" + year + " " + quarter, ); } @@ -293,7 +283,7 @@ }); // Build tableList once for all charts and sort by date (newest first) - tableList = financialData.map((statement) => ({ + tableList = financialData?.map((statement) => ({ date: statement.date, // Add more properties if needed })); @@ -302,14 +292,17 @@ } $: { - if ($timeFrame || activeIdx) { - if (activeIdx === 0) { - filterRule = "annual"; + if ($timeFrame || $selectedTimePeriod) { + if ($selectedTimePeriod === "annual") { fullStatement = data?.getData?.annual; - } else { - filterRule = "quarterly"; + } else if ($selectedTimePeriod === "quarterly") { fullStatement = data?.getData?.quarter; + } else if ($selectedTimePeriod === "ttm") { + fullStatement = data?.getData?.ttm; + } else { + fullStatement = data?.getData?.annual; } + financialData = filterStatement(fullStatement, $timeFrame); preprocessFinancialData(); } @@ -329,113 +322,70 @@

- {removeCompanyStrings($displayCompanyName)} Cash Flow Statement + {removeCompanyStrings($displayCompanyName)} Income Statement

-
- {#each tabs as item, i} - {#if !["Pro", "Plus"]?.includes(data?.user?.tier) && i > 0} - - {:else} - - {/if} - {/each} -
+
{#if financialData?.length > 0}
- - -
- + +
- {#if $coolMode}
{#each fields as item, i} @@ -488,7 +460,7 @@ data={financialData} {statementConfig} displayStatement={item?.key} - {filterRule} + filterRule={$selectedTimePeriod} {processedData} color={["#ff00cc", "#37ff00", "#0c63e7", "#07c8f9"][ i % 4 @@ -497,91 +469,6 @@ {/each}
{:else} -
- - Financials in {data?.getProfileData?.currency}. Fiscal year - is - {data?.getProfileData?.fiscalYearRange}. - -
-
- - - - - - - Select time frame - - - - ($timeFrame = "5Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 5 years - - ($timeFrame = "10Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 10 years - - ($timeFrame = "MAX")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - Max - - - - -
- -
-
@@ -594,14 +481,16 @@ >Fiscal Year {#each financialData as item} - {#if filterRule === "annual"} + {#if $selectedTimePeriod === "annual"} {"FY" + " " + item?.fiscalYear} {:else} - + {item?.period + " " + item?.fiscalYear} {/if} diff --git a/src/routes/stocks/[tickerID]/financials/ratios/+page.server.ts b/src/routes/stocks/[tickerID]/financials/ratios/+page.server.ts index 3d5d95e8..4be37eb3 100644 --- a/src/routes/stocks/[tickerID]/financials/ratios/+page.server.ts +++ b/src/routes/stocks/[tickerID]/financials/ratios/+page.server.ts @@ -1,13 +1,12 @@ export const load = async ({ locals, params }) => { const getData = async () => { const { apiKey, apiURL } = locals; - const postData = { ticker: params.tickerID, + statement: 'ratios', }; - // make the POST request to the endpoint - const response = await fetch(apiURL + "/stock-ratios", { + const response = await fetch(apiURL + "/financial-statement", { method: "POST", headers: { "Content-Type": "application/json", @@ -17,10 +16,12 @@ export const load = async ({ locals, params }) => { }); const output = await response.json(); - + return output; }; + + // Make sure to return a promise return { getData: await getData(), diff --git a/src/routes/stocks/[tickerID]/financials/ratios/+page.svelte b/src/routes/stocks/[tickerID]/financials/ratios/+page.svelte index 3871d5f6..747ce208 100644 --- a/src/routes/stocks/[tickerID]/financials/ratios/+page.svelte +++ b/src/routes/stocks/[tickerID]/financials/ratios/+page.svelte @@ -4,6 +4,7 @@ stockTicker, coolMode, timeFrame, + selectedTimePeriod, } from "$lib/store"; import { removeCompanyStrings } from "$lib/utils"; import * as DropdownMenu from "$lib/components/shadcn/dropdown-menu/index.js"; @@ -16,24 +17,11 @@ export let data; - let isLoaded = true; let tableList = []; let processedData = {}; let financialData = []; let fullStatement = []; - let filterRule = "annual"; - - let activeIdx = 0; - - const tabs = [ - { - title: "Annual", - }, - { - title: "Quarterly", - }, - ]; const statementConfig = [ { @@ -191,8 +179,8 @@ let properties = [ { - key: filterRule === "annual" ? "fiscalYear" : "date", - label: filterRule === "annual" ? "Year" : "Quarter", + key: $selectedTimePeriod === "annual" ? "fiscalYear" : "date", + label: $selectedTimePeriod === "annual" ? "Year" : "Quarter", }, ]; @@ -225,7 +213,7 @@ a.href = url; a.download = $stockTicker.toLowerCase() + - `${filterRule === "annual" ? "_annual" : "_quarter"}_cash_flow_statement.csv`; + `${$selectedTimePeriod === "annual" ? "_annual" : $selectedTimePeriod === "quarterly" ? "_quarter" : "_ttm"}_ratios.csv`; document.body.appendChild(a); a.click(); document.body.removeChild(a); @@ -248,13 +236,16 @@ } }); + // Precompute xList from income (reverse order) const xList = []; for (let i = financialData.length - 1; i >= 0; i--) { const statement = financialData[i]; const year = statement.fiscalYear.slice(-2); const quarter = statement.period; xList.push( - filterRule === "annual" ? "FY" + year : "FY" + year + " " + quarter, + $selectedTimePeriod === "annual" + ? "FY" + year + : "FY" + year + " " + quarter, ); } @@ -282,7 +273,7 @@ }); // Build tableList once for all charts and sort by date (newest first) - tableList = financialData.map((statement) => ({ + tableList = financialData?.map((statement) => ({ date: statement.date, // Add more properties if needed })); @@ -291,14 +282,17 @@ } $: { - if ($timeFrame || activeIdx) { - if (activeIdx === 0) { - filterRule = "annual"; + if ($timeFrame || $selectedTimePeriod) { + if ($selectedTimePeriod === "annual") { fullStatement = data?.getData?.annual; - } else { - filterRule = "quarterly"; + } else if ($selectedTimePeriod === "quarterly") { fullStatement = data?.getData?.quarter; + } else if ($selectedTimePeriod === "ttm") { + fullStatement = data?.getData?.ttm; + } else { + fullStatement = data?.getData?.annual; } + financialData = filterStatement(fullStatement, $timeFrame); preprocessFinancialData(); } @@ -318,113 +312,70 @@

- {removeCompanyStrings($displayCompanyName)} Ratios Statement + {removeCompanyStrings($displayCompanyName)} Income Statement

-
- {#each tabs as item, i} - {#if !["Pro", "Plus"]?.includes(data?.user?.tier) && i > 0} - - {:else} - - {/if} - {/each} -
+
{#if financialData?.length > 0}
- - -
- + +
- {#if $coolMode}
{#each fields as item, i} @@ -477,7 +450,7 @@ data={financialData} {statementConfig} displayStatement={item?.key} - {filterRule} + filterRule={$selectedTimePeriod} {processedData} color={["#ff00cc", "#37ff00", "#0c63e7", "#07c8f9"][ i % 4 @@ -486,91 +459,6 @@ {/each}
{:else} -
- - Financials in {data?.getProfileData?.currency}. Fiscal year - is - {data?.getProfileData?.fiscalYearRange}. - -
-
- - - - - - - Select time frame - - - - ($timeFrame = "5Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 5 years - - ($timeFrame = "10Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 10 years - - ($timeFrame = "MAX")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - Max - - - - -
- -
-
@@ -583,14 +471,16 @@ >Fiscal Year {#each financialData as item} - {#if filterRule === "annual"} + {#if $selectedTimePeriod === "annual"} {"FY" + " " + item?.fiscalYear} {:else} - + {item?.period + " " + item?.fiscalYear} {/if}