ui fixes for dividend page

This commit is contained in:
MuslemRahimi 2025-02-22 16:47:18 +01:00
parent 92fa85c144
commit deb3edad99
4 changed files with 172 additions and 490 deletions

View File

@ -1,26 +1,11 @@
<script lang="ts">
import {
numberOfUnreadNotification,
displayCompanyName,
etfTicker,
} from "$lib/store";
import { onMount } from "svelte";
import { monthNames } from "$lib/utils";
import { Chart } from "svelte-echarts";
import { init, use } from "echarts/core";
import { LineChart, BarChart } from "echarts/charts";
import { GridComponent, TooltipComponent } from "echarts/components";
import { CanvasRenderer } from "echarts/renderers";
import { displayCompanyName, etfTicker } from "$lib/store";
import Infobox from "$lib/components/Infobox.svelte";
import SEO from "$lib/components/SEO.svelte";
use([LineChart, BarChart, TooltipComponent, GridComponent, CanvasRenderer]);
export let data;
let isLoaded = false;
let dateDistance;
let rawData = data?.getStockDividend;
let optionsDividend;
let exDividendDate = rawData?.history?.at(0)?.date;
let dividendYield = rawData?.dividendYield;
@ -29,93 +14,6 @@
let payoutRatio = rawData?.payoutRatio;
let dividendGrowth = rawData?.dividendGrowth;
async function plotDividend() {
// Combine the data into an array of objects to keep them linked
const combinedData = rawData?.history?.map((item) => ({
date: item?.paymentDate,
dividend: item?.adjDividend?.toFixed(3),
}));
// Sort the combined data array based on the date
combinedData.sort((a, b) => new Date(a?.date) - new Date(b?.date));
// Separate the sorted data back into individual arrays
const dates = combinedData.map((item) => item.date);
const dividendList = combinedData.map((item) => item.dividend);
const options = {
animation: false,
grid: {
left: "3%",
right: "3%",
bottom: "10%",
top: "10%",
containLabel: true,
},
xAxis: {
data: dates,
type: "category",
axisLabel: {
color: "#fff",
},
splitLine: {
show: false, // Disable x-axis grid lines
},
},
yAxis: [
{
type: "value",
splitLine: {
show: false, // Disable x-axis grid lines
},
axisLabel: {
show: false, // Hide y-axis labels
},
},
],
series: [
{
name: "Dividend per Share",
data: dividendList,
type: "bar",
smooth: true,
itemStyle: {
color: "#fff",
},
},
],
tooltip: {
trigger: "axis",
hideDelay: 100,
borderColor: "#969696", // Black border color
borderWidth: 1, // Border width of 1px
backgroundColor: "#313131", // Optional: Set background color for contrast
textStyle: {
color: "#fff", // Optional: Text color for better visibility
},
formatter: function (params) {
const date = params[0].name; // Get the date from the x-axis value
const dateParts = date.split("-");
const year = dateParts[0];
const monthIndex = parseInt(dateParts[1]) - 1;
const day = dateParts[2];
const formattedDate = `${monthNames[monthIndex]} ${day}, ${year}`;
// Return the tooltip content
return `${formattedDate}<br/> Dividend Per Share: ${params[0].value}`;
},
},
};
return options;
}
onMount(async () => {
optionsDividend = await plotDividend();
isLoaded = true;
});
function generateDividendInfoHTML() {
const history = rawData?.history || [];
@ -130,20 +28,11 @@
},
);
const payoutFrequencyText =
payoutFrequency === 4
? "3 months"
: payoutFrequency === 2
? "6 months"
: payoutFrequency === 1
? "12 months"
: "n/a";
return `
<span>
${$displayCompanyName} has an annual dividend of $${annualDividend} per share, with a forward yield of ${dividendYield}%.
The dividend is paid every ${payoutFrequencyText} and the last ex-dividend date was ${formattedExDividendDate}.
</span>
<span>
${$etfTicker} has a dividend yield of ${dividendYield}% and paid $${annualDividend} per share in the past year. The dividend is paid once per ${payoutFrequency === "Annually" ? "year" : payoutFrequency === "Quarterly" ? "quarter" : payoutFrequency === "Weekly" ? "week" : ""} and the last ex-dividend date was ${formattedExDividendDate}.
</span>
`;
} else {
const latestDividendDate = new Date(history.at(0)?.date).toLocaleString(
@ -253,15 +142,7 @@
<div
class="mt-1 break-words font-semibold leading-8 text-light text-xl"
>
{payoutFrequency === 4
? "Quartely"
: payoutFrequency === 2
? "Half-Yearly"
: payoutFrequency === 1
? "Annually"
: payoutFrequency > 4
? "Weekly"
: "n/a"}
{payoutFrequency ? payoutFrequency : "n/a"}
</div>
</div>
<div class="p-4 bp:p-5 sm:p-6 border-t border-r border-contrast">
@ -289,122 +170,122 @@
<div
class="mt-1 break-words font-semibold leading-8 text-light text-xl"
>
{dividendGrowth !== "NaN" ? dividendGrowth + "%" : "-"}
{dividendGrowth ? dividendGrowth + "%" : "n/a"}
</div>
</div>
</div>
<div
class="flex flex-col sm:flex-row items-start sm:items-center w-full mt-14 mb-8"
class="flex flex-col sm:flex-row items-start sm:items-center w-full mt-5 mb-8"
>
<h3 class="text-xl text-white font-semibold">Dividends History</h3>
<h2 class="text-xl sm:text-2xl text-white font-bold">
Dividends History
</h2>
</div>
{#if isLoaded}
{#if rawData?.history?.length !== 0 && optionsDividend}
<div class="app w-full">
<Chart {init} options={optionsDividend} class="chart" />
</div>
<div
class="overflow-x-scroll no-scrollbar flex justify-start items-center w-full m-auto rounded-none sm:rounded-md mb-4"
{#if rawData?.history?.length !== 0}
<div
class="overflow-x-scroll no-scrollbar flex justify-start items-center w-full m-auto rounded-none sm:rounded-md mb-4"
>
<table
class="table table-sm table-compact bg-table border border-gray-800 flex justify-start items-center w-full m-auto"
>
<table
class="table table-sm table-compact bg-table border border-gray-800 flex justify-start items-center w-full m-auto"
>
<thead class="bg-default">
<tr class="">
<th class="text-start text-white text-sm font-semibold">
Ex-Divid. Date
</th>
<th class="text-end text-white text-sm font-semibold">
Cash Amount
</th>
<th class="text-end text-white text-sm font-semibold">
Record Date
</th>
<th class="text-end text-white text-sm font-semibold">
Pay Date
</th>
</tr>
</thead>
<tbody class="">
{#each rawData?.history as item}
<tr
class="text-white odd:bg-odd border-b border-gray-800"
<thead class="bg-default">
<tr class="">
<th class="text-start text-white text-sm font-semibold">
Ex-Dividend Date
</th>
<th class="text-end text-white text-sm font-semibold">
Cash Amount
</th>
<th class="text-end text-white text-sm font-semibold">
Declaration Date
</th>
<th class="text-end text-white text-sm font-semibold">
Record Date
</th>
<th class="text-end text-white text-sm font-semibold">
Pay Date
</th>
</tr>
</thead>
<tbody class="">
{#each rawData?.history as item}
<tr class="text-white odd:bg-odd border-b border-gray-800">
<td
class="text-start text-sm sm:text-[1rem] whitespace-nowrap text-white font-medium"
>
<td
class="text-start text-sm sm:text-[1rem] whitespace-nowrap text-white font-medium"
>
{new Date(item?.date)?.toLocaleString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
daySuffix: "2-digit",
})}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap text-white"
>
{item?.adjDividend?.toFixed(3)}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap text-white"
>
{item?.recordDate?.length !== 0
? new Date(item?.recordDate)?.toLocaleString(
"en-US",
{
month: "short",
day: "numeric",
year: "numeric",
daySuffix: "2-digit",
},
)
: "n/a"}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap text-white"
>
{item?.paymentDate?.length !== 0
? new Date(item?.paymentDate)?.toLocaleString(
"en-US",
{
month: "short",
day: "numeric",
year: "numeric",
daySuffix: "2-digit",
},
)
: "n/a"}
</td>
</tr>
{/each}
</tbody>
</table>
</div>
<span class="text-gray-200 text-sm italic">
* Dividend amounts are adjusted for stock splits when
applicable.
</span>
{:else}
<h1
class="text-xl m-auto flex justify-center text-gray-200 mb-4 mt-10"
>
No history found
</h1>
{/if}
{:else}
<div class="flex justify-center items-center h-80">
<div class="relative">
<label
class="bg-secondary rounded-md 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 text-gray-400"
></span>
</label>
</div>
{new Date(item?.date)?.toLocaleString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
daySuffix: "2-digit",
})}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap text-white"
>
${item?.adjDividend?.toFixed(3)}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap text-white"
>
{item?.declarationDate?.length !== 0
? new Date(item?.declarationDate)?.toLocaleString(
"en-US",
{
month: "short",
day: "numeric",
year: "numeric",
daySuffix: "2-digit",
},
)
: "n/a"}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap text-white"
>
{item?.recordDate?.length !== 0
? new Date(item?.recordDate)?.toLocaleString(
"en-US",
{
month: "short",
day: "numeric",
year: "numeric",
daySuffix: "2-digit",
},
)
: "n/a"}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap text-white"
>
{item?.paymentDate?.length !== 0
? new Date(item?.paymentDate)?.toLocaleString(
"en-US",
{
month: "short",
day: "numeric",
year: "numeric",
daySuffix: "2-digit",
},
)
: "n/a"}
</td>
</tr>
{/each}
</tbody>
</table>
</div>
<span class="text-gray-200 text-sm italic">
* Dividend amounts are adjusted for stock splits when applicable.
</span>
{:else}
<h1
class="text-xl m-auto flex justify-center text-gray-200 mb-4 mt-10"
>
No history found
</h1>
{/if}
{/if}
</div>

View File

@ -1,16 +1,10 @@
<script lang="ts">
import { stockTicker } from "$lib/store";
import { formatDate } from "$lib/utils";
export let data;
let similarStocks;
$: {
if ($stockTicker) {
similarStocks = data?.getSimilarStocks?.sort(
(a, b) => b?.dividendYield - a?.dividendYield,
);
}
}
let newsList = data?.getNews ?? [];
</script>
<section class="w-auto overflow-hidden min-h-screen">
@ -24,46 +18,29 @@
</main>
<aside class="hidden lg:block relative fixed w-1/4 mt-3">
{#if similarStocks?.length > 0}
{#if newsList?.length !== 0}
<div
class="w-full p-2 text-white border border-gray-600 bg-inherit rounded-md h-fit pb-4 mt-4 cursor-pointer"
class="w-full sm:hover:text-white text-white border border-gray-600 rounded-md h-fit pb-4 mt-4 cursor-pointer bg-inherit"
>
<h3 class="p-2 pt-4 text-xl font-semibold">Related Stocks</h3>
<table class="table table-sm table-compact w-full text-white">
<thead class="text-white"
><tr
><th
class="whitespace-nowrap border-b border-gray-600 font-semibold text-[1rem] text-left"
>Company</th
>
<th
class="whitespace-nowrap border-b border-gray-600 font-semibold text-[1rem] text-right"
>Dividend Yield</th
></tr
></thead
>
<tbody>
{#each similarStocks?.slice(0, 8) as item}
<tr class="border-gray-800 border-b text-[1rem]"
><td class="text-left text-[1rem]"
><a
href={`/stocks/${item?.symbol}/dividends`}
class="sm:hover:text-white text-blue-400"
>{item?.name?.length > 30
? item?.name?.slice(0, 30) + "..."
: item?.name}</a
></td
<div class="p-4 text-sm">
<h3 class="text-lg text-white font-semibold mb-3">
{$stockTicker} News
</h3>
<ul class="text-white">
{#each newsList?.slice(0, 10) as item}
<li class="mb-3 last:mb-1">
{formatDate(item?.publishedDate)} &#183;
<a
class="sm:hover:text-white text-blue-400"
href={item?.url}
target="_blank"
rel="noopener noreferrer nofollow">{item?.title}</a
>
<td class="text-right cursor-normal text-[1rem]"
>{item?.dividendYield !== null &&
item?.dividendYield !== undefined
? item?.dividendYield + "%"
: "n/a"}</td
>
</tr>
- {item?.site}
</li>
{/each}
</tbody>
</table>
</ul>
</div>
</div>
{/if}
</aside>

View File

@ -1,8 +1,3 @@
import { error, fail, redirect } from "@sveltejs/kit";
import { validateData } from "$lib/utils";
import { loginUserSchema, registerUserSchema } from "$lib/schemas";
export const load = async ({ params, locals }) => {
const getStockDividend = async () => {
let newsList;
@ -33,155 +28,3 @@ export const load = async ({ params, locals }) => {
getStockDividend: await getStockDividend(),
};
};
export const actions = {
login: async ({ url, request, locals }) => {
const path = url?.href?.replace("/oauth2","")
const { formData, errors } = await validateData(
await request.formData(),
loginUserSchema,
);
if (errors) {
return fail(400, {
data: formData,
errors: errors.fieldErrors,
});
}
try {
await locals.pb
.collection("users")
.authWithPassword(formData.email, formData.password);
/*
if (!locals.pb?.authStore?.model?.verified) {
locals.pb.authStore.clear();
return {
notVerified: true,
};
}
*/
} catch (err) {
console.log("Error: ", err);
error(err.status, err.message);
}
redirect(302, path);
},
register: async ({ url, locals, request }) => {
const path = url?.href?.replace("/oauth2","")
const { formData, errors } = await validateData(
await request.formData(),
registerUserSchema,
);
if (errors) {
return fail(400, {
data: formData,
errors: errors.fieldErrors,
});
}
try {
let newUser = await locals.pb.collection("users").create(formData);
/*
await locals.pb?.collection('users').update(
newUser?.id, {
'freeTrial' : true,
'tier': 'Pro', //Give new users a free trial for the Pro Subscription
});
*/
await locals.pb.collection("users")?.requestVerification(formData.email);
} catch (err) {
console.log("Error: ", err);
error(err.status, err.message);
}
try {
await locals.pb
.collection("users")
.authWithPassword(formData.email, formData.password);
} catch (err) {
console.log("Error: ", err);
error(err.status, err.message);
}
redirect(303, path);
},
oauth2: async ({ url, locals, request, cookies }) => {
const path = url?.href?.replace("/oauth2","")
const authMethods = (await locals?.pb
?.collection("users")
?.listAuthMethods())?.oauth2;
const data = await request?.formData();
const providerSelected = data?.get("provider");
if (!authMethods) {
return {
authProviderRedirect: "",
authProviderState: "",
};
}
const redirectURL = `${url.origin}/oauth`;
const targetItem = authMethods?.providers?.findIndex(
(item) => item?.name === providerSelected,
);
//console.log("==================")
//console.log(authMethods.authProviders)
//console.log('target item is: ', targetItem)
const provider = authMethods.providers[targetItem];
const authProviderRedirect = `${provider.authUrl}${redirectURL}`;
const state = provider.state;
const verifier = provider.codeVerifier;
cookies.set("state", state, {
httpOnly: true,
sameSite: "lax",
secure: true,
path: "/",
maxAge: 60 * 60,
});
cookies.set("verifier", verifier, {
httpOnly: true,
sameSite: "lax",
secure: true,
path: "/",
maxAge: 60 * 60,
});
cookies.set("provider", providerSelected, {
httpOnly: true,
sameSite: "lax",
secure: true,
path: "/",
maxAge: 60 * 60,
});
cookies.set("path", path, {
httpOnly: true,
sameSite: "lax",
secure: true,
path: "/",
maxAge: 60,
});
redirect(302, authProviderRedirect);
},
};

View File

@ -5,7 +5,7 @@
export let data;
let dateDistance;
let rawData = data?.getStockDividend || [];
let rawData = data?.getStockDividend;
let exDividendDate = rawData?.history?.at(0)?.date;
let dividendYield = rawData?.dividendYield;
@ -28,20 +28,11 @@
},
);
const payoutFrequencyText =
payoutFrequency === 4
? "3 months"
: payoutFrequency === 2
? "6 months"
: payoutFrequency === 1
? "12 months"
: "n/a";
return `
<span>
${$displayCompanyName} has an annual dividend of $${annualDividend} per share, with a forward yield of ${dividendYield}%.
The dividend is paid every ${payoutFrequencyText} and the last ex-dividend date was ${formattedExDividendDate}.
</span>
<span>
${$stockTicker} has a dividend yield of ${dividendYield}% and paid $${annualDividend} per share in the past year. The dividend is paid once per ${payoutFrequency === "Annually" ? "year" : payoutFrequency === "Quarterly" ? "quarter" : payoutFrequency === "Weekly" ? "week" : ""} and the last ex-dividend date was ${formattedExDividendDate}.
</span>
`;
} else {
const latestDividendDate = new Date(history.at(0)?.date).toLocaleString(
@ -69,25 +60,11 @@
}
}
let htmlOutput = generateDividendInfoHTML();
$: {
if ($stockTicker) {
rawData = data?.getStockDividend || [];
exDividendDate = rawData?.history?.at(0)?.date;
dividendYield = rawData?.dividendYield;
annualDividend = rawData?.annualDividend;
payoutFrequency = rawData?.payoutFrequency;
payoutRatio = rawData?.payoutRatio;
dividendGrowth = rawData?.dividendGrowth;
htmlOutput = generateDividendInfoHTML();
}
}
const htmlOutput = generateDividendInfoHTML();
</script>
<SEO
title={`${$displayCompanyName} (${$stockTicker}) Dividend History, Dates & Yield · Stocknear`}
title={`${$displayCompanyName} (${$stockTicker}) Dividend History, Dates & Yield`}
description={`Get the latest dividend data for ${$displayCompanyName} (${$stockTicker}) stock price quote with breaking news, financials, statistics, charts and more.`}
/>
@ -165,15 +142,7 @@
<div
class="mt-1 break-words font-semibold leading-8 text-light text-xl"
>
{payoutFrequency === 4
? "Quartely"
: payoutFrequency === 2
? "Half-Yearly"
: payoutFrequency === 1
? "Annually"
: payoutFrequency > 4
? "Weekly"
: "n/a"}
{payoutFrequency ? payoutFrequency : "n/a"}
</div>
</div>
<div class="p-4 bp:p-5 sm:p-6 border-t border-r border-contrast">
@ -201,15 +170,17 @@
<div
class="mt-1 break-words font-semibold leading-8 text-light text-xl"
>
{dividendGrowth !== "NaN" ? dividendGrowth + "%" : "-"}
{dividendGrowth ? dividendGrowth + "%" : "n/a"}
</div>
</div>
</div>
<div
class="flex flex-col sm:flex-row items-start sm:items-center w-full mt-14 mb-8"
class="flex flex-col sm:flex-row items-start sm:items-center w-full mt-5 mb-8"
>
<h3 class="text-xl text-white font-semibold">Dividends History</h3>
<h2 class="text-xl sm:text-2xl text-white font-bold">
Dividends History
</h2>
</div>
{#if rawData?.history?.length !== 0}
@ -217,28 +188,23 @@
class="overflow-x-scroll no-scrollbar flex justify-start items-center w-full m-auto rounded-none sm:rounded-md mb-4"
>
<table
class="table table-sm table-compact flex justify-start items-center w-full m-auto"
class="table table-sm table-compact bg-table border border-gray-800 flex justify-start items-center w-full m-auto"
>
<thead>
<tr class="bg-default border-b border-gray-800">
<th
class="text-start bg-default text-white text-sm font-semibold"
>
Ex-Divid. Date
<thead class="bg-default">
<tr class="">
<th class="text-start text-white text-sm font-semibold">
Ex-Dividend Date
</th>
<th
class="text-end bg-default text-white text-sm font-semibold"
>
<th class="text-end text-white text-sm font-semibold">
Cash Amount
</th>
<th
class="text-end bg-default text-white text-sm font-semibold"
>
<th class="text-end text-white text-sm font-semibold">
Declaration Date
</th>
<th class="text-end text-white text-sm font-semibold">
Record Date
</th>
<th
class="text-end bg-default text-white text-sm font-semibold"
>
<th class="text-end text-white text-sm font-semibold">
Pay Date
</th>
</tr>
@ -259,7 +225,22 @@
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap text-white"
>
{item?.adjDividend?.toFixed(3)}
${item?.adjDividend?.toFixed(3)}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap text-white"
>
{item?.declarationDate?.length !== 0
? new Date(item?.declarationDate)?.toLocaleString(
"en-US",
{
month: "short",
day: "numeric",
year: "numeric",
daySuffix: "2-digit",
},
)
: "n/a"}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap text-white"