From bd121629e10e7436e6b9f8c496c3e06fb050d082 Mon Sep 17 00:00:00 2001 From: Sergey Uchaev Date: Wed, 4 Sep 2024 23:53:40 +0300 Subject: [PATCH] Refactoring --- actions/shares/shares.ts | 68 ++++++++++++++++++++++++++++++---------- app/[slug]/page.tsx | 29 +++++++---------- app/layout.tsx | 10 +++--- 3 files changed, 68 insertions(+), 39 deletions(-) diff --git a/actions/shares/shares.ts b/actions/shares/shares.ts index b3c9611..a799397 100644 --- a/actions/shares/shares.ts +++ b/actions/shares/shares.ts @@ -4,12 +4,9 @@ import { IApiResponseShares, IFinancialInstrument, IPagination, - IApiResponseShareItem, IInstrument, } from '@/typing'; -// todo need fix type and docs - /** * Fetches the list of shares from the API. * @@ -18,7 +15,10 @@ import { * * @async * @function getStocks - * @returns {Promise} A promise that resolves to an object containing the response data. + * @param {IPagination} pagination - An object containing pagination parameters. + * @param {number} pagination.page - The page number to fetch. + * @param {number} pagination.pageSize - The number of items to return per page. + * @returns {Promise} A promise that resolves to an object containing the response data. * @throws {Error} Throws an error if the request fails. */ export const getStocks = async ( @@ -40,35 +40,71 @@ export const getStocks = async ( }; }; -export const getStockByTicker = async (ticker: string): Promise => { +/** + * Fetches a stock by its ticker symbol. + * + * This function makes a GET request to the `/sharesByTicker` endpoint of the API + * defined by the `API_BASE_URL` and returns the response data. + * + * @async + * @function getStockByTicker + * @param {string} ticker - The ticker symbol of the stock to fetch. + * @returns {Promise<{ data: IInstrument }>} A promise that resolves to an object containing the response data. + * @throws {Error} Throws an error if the request fails. + */ +export const getStockByTicker = async ( + ticker: string, +): Promise<{ data: any }> => { const { data } = await axios.get<{ data: IInstrument; }>(`${API_BASE_URL}/sharesByTicker`, { params: { ticker }, }); - return { - data, - }; + return { data }; }; -export const getLastPriceByTicker = async (ticker: string): Promise => { +/** + * Fetches the latest price for a stock by its ticker symbol. + * + * This function makes a GET request to the `/lastPriceByTicker` endpoint of the API + * defined by the `API_BASE_URL` and returns the response data. + * + * @async + * @function getLastPriceByTicker + * @param {string} ticker - The ticker symbol of the stock to fetch the latest price for. + * @returns {Promise<{ data: IInstrument }>} A promise that resolves to an object containing the response data. + * @throws {Error} Throws an error if the request fails. + */ +export const getLastPriceByTicker = async ( + ticker: string, +): Promise<{ data: any }> => { const { data } = await axios.get<{ data: IInstrument; }>(`${API_BASE_URL}/lastPriceByTicker`, { params: { ticker }, }); - return { - data, - }; + return { data }; }; -export const getCandlesByTicker = async (ticker: string): Promise => { +/** + * Fetches the candlestick data for a stock by its ticker symbol. + * + * This function makes a GET request to the `/candlesByTicker` endpoint of the API + * defined by the `API_BASE_URL` and returns the response data. + * + * @async + * @function getCandlesByTicker + * @param {string} ticker - The ticker symbol of the stock to fetch the candlestick data for. + * @returns {Promise<{ data: IInstrument }>} A promise that resolves to an object containing the response data. + * @throws {Error} Throws an error if the request fails. + */ +export const getCandlesByTicker = async ( + ticker: string, +): Promise<{ data: any }> => { const { data } = await axios.get<{ data: IInstrument; }>(`${API_BASE_URL}/candlesByTicker`, { params: { ticker }, }); - return { - data, - }; + return { data }; }; diff --git a/app/[slug]/page.tsx b/app/[slug]/page.tsx index 4c1c235..a6e6a0f 100644 --- a/app/[slug]/page.tsx +++ b/app/[slug]/page.tsx @@ -55,31 +55,26 @@ const PageStock = async ({ params }: Props) => { const dataLastPrice = lastPriceData?.data; const dataCandlesByTicker = candlesData?.data; + const renderContent = (data: any, Component: React.FC, props: any) => + data ? : Not data; + return (
- {stockData?.data ? ( - - ) : ( - Not data - )} + {renderContent(dataIntro, StockIntro, { data: dataIntro })}
- {candlesData?.data ? ( - - ) : ( - Not data - )} + {renderContent(dataCandlesByTicker, Candles, { + currency: dataIntro?.currency, + data: dataCandlesByTicker, + })}
- {lastPriceData?.data && candlesData?.data ? ( + {dataLastPrice && dataCandlesByTicker ? ( ) : ( Not data diff --git a/app/layout.tsx b/app/layout.tsx index b202022..8b83c54 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -12,11 +12,7 @@ const roboto = Open_Sans({ subsets: ['latin'], }); -export default function RootLayout({ - children, -}: Readonly<{ - children: React.ReactNode; -}>) { +const RootLayout: React.FC<{ children: React.ReactNode }> = ({ children }) => { return ( @@ -32,7 +28,9 @@ export default function RootLayout({ ); -} +}; + +export default RootLayout; export const viewport: Viewport = { colorScheme: 'light dark',