|
1 | 1 | import axios from "axios";
|
2 | 2 | import useSWR from "swr";
|
3 | 3 |
|
| 4 | +function serializeLocalToGlobal(localStock) { |
| 5 | + const { |
| 6 | + name, |
| 7 | + cusip, |
| 8 | + ticker, |
| 9 | + sector, |
| 10 | + industry, |
| 11 | + class: rights, |
| 12 | + shares_held, |
| 13 | + shares_held_str, |
| 14 | + market_value, |
| 15 | + market_value_str, |
| 16 | + sold, |
| 17 | + update, |
| 18 | + ratios, |
| 19 | + records, |
| 20 | + prices, |
| 21 | + report_time = "N/A", |
| 22 | + recent_price = "N/A", |
| 23 | + } = localStock; |
| 24 | + |
| 25 | + const { |
| 26 | + portfolio_percent: portfolio_percentage, |
| 27 | + portfolio_str: portfolio_percentage_str, |
| 28 | + ownership_percent: ownership_percentage, |
| 29 | + ownership_str: ownership_percentage_str, |
| 30 | + } = ratios; |
| 31 | + |
| 32 | + const { buy: buy_price, sold: sold_price } = prices; |
| 33 | + |
| 34 | + const { |
| 35 | + time: buy_time, |
| 36 | + time_str: buy_time_str, |
| 37 | + series: buy_series, |
| 38 | + } = buy_price; |
| 39 | + |
| 40 | + const sold_time = sold_price ? sold_price.time : "N/A"; |
| 41 | + const sold_time_str = sold_price ? sold_price.time_str : "N/A"; |
| 42 | + const sold_series = sold_price ? sold_price.series : "N/A"; |
| 43 | + |
| 44 | + const report_date = |
| 45 | + report_time !== "N/A" ? new Date(report_time * 1000) : "N/A"; |
| 46 | + const report_date_str = |
| 47 | + report_time !== "N/A" |
| 48 | + ? `Q${ |
| 49 | + Math.floor(report_date.getMonth() / 3) + 1 |
| 50 | + } ${report_date.getFullYear()}` |
| 51 | + : "N/A"; |
| 52 | + |
| 53 | + const price_bought = buy_series !== "N/A" ? buy_series.close : "N/A"; |
| 54 | + const price_bought_str = buy_series !== "N/A" ? `$${price_bought}` : "N/A"; |
| 55 | + |
| 56 | + const price_sold = sold_series !== "N/A" ? sold_series.close : "N/A"; |
| 57 | + const price_sold_str = sold_series !== "N/A" ? `$${price_sold}` : "N/A"; |
| 58 | + |
| 59 | + const recent_price_str = recent_price !== "N/A" ? `$${recent_price}` : "N/A"; |
| 60 | + |
| 61 | + const gain_value = |
| 62 | + recent_price !== "N/A" && price_bought !== "N/A" |
| 63 | + ? parseFloat(recent_price - price_bought) |
| 64 | + : "N/A"; |
| 65 | + const gain_percent = |
| 66 | + gain_value !== "N/A" && price_bought !== "N/A" |
| 67 | + ? parseFloat((gain_value / price_bought) * 100) |
| 68 | + : "N/A"; |
| 69 | + const gain_value_str = gain_value !== "N/A" ? gain_value.toFixed(2) : "N/A"; |
| 70 | + const gain_percent_str = |
| 71 | + gain_percent !== "N/A" ? gain_percent.toFixed(2) : "N/A"; |
| 72 | + |
| 73 | + return { |
| 74 | + name, |
| 75 | + cusip, |
| 76 | + ticker, |
| 77 | + sector, |
| 78 | + industry, |
| 79 | + class: rights, |
| 80 | + update, |
| 81 | + sold, |
| 82 | + recent_price, |
| 83 | + recent_price_str, |
| 84 | + buy_price: price_bought, |
| 85 | + buy_price_str: price_bought_str, |
| 86 | + sold_price: price_sold, |
| 87 | + sold_price_str: price_sold_str, |
| 88 | + shares_held, |
| 89 | + shares_held_str, |
| 90 | + market_value, |
| 91 | + market_value_str, |
| 92 | + portfolio_percent: portfolio_percentage, |
| 93 | + portfolio_str: portfolio_percentage_str, |
| 94 | + ownership_percent: ownership_percentage, |
| 95 | + ownership_str: ownership_percentage_str, |
| 96 | + gain_value, |
| 97 | + gain_value_str, |
| 98 | + gain_percent, |
| 99 | + gain_str: gain_percent_str, |
| 100 | + report_time, |
| 101 | + report_str: report_date_str, |
| 102 | + buy_time, |
| 103 | + buy_str: buy_time_str, |
| 104 | + sold_time, |
| 105 | + sold_str: sold_time_str, |
| 106 | + }; |
| 107 | +} |
| 108 | + |
4 | 109 | const server = process.env.NEXT_PUBLIC_SERVER;
|
5 | 110 | const useChanges = (
|
6 | 111 | cik,
|
@@ -51,7 +156,7 @@ const useChanges = (
|
51 | 156 | })
|
52 | 157 | .catch((e) => console.error(e));
|
53 | 158 | const { isLoading: loading, error } = useSWR(
|
54 |
| - cik && access ? [server + "/stocks/filing", cik, access, sort] : null, |
| 159 | + cik && access ? [server + "/stocks/changes", cik, access, sort] : null, |
55 | 160 | ([url, cik, access, sort]) => stockFetcher(url, cik, access, sort),
|
56 | 161 | {
|
57 | 162 | revalidateOnFocus: false,
|
|
0 commit comments