diff --git a/actions/shares/shares.ts b/actions/shares/shares.ts index b3c9611..29496b9 100644 --- a/actions/shares/shares.ts +++ b/actions/shares/shares.ts @@ -8,17 +8,16 @@ import { IInstrument, } from '@/typing'; -// todo need fix type and docs - /** * Fetches the list of shares from the API. * - * This function makes a GET request to the `/shares` endpoint of the API - * defined by the `API_BASE_URL` and returns the response data. + * This function makes a GET request to the /shares endpoint of the API + * defined by the API_BASE_URL and returns the response data. * * @async * @function getStocks - * @returns {Promise} A promise that resolves to an object containing the response data. + * @param {IPagination} pagination - The pagination parameters for the request. + * @returns {Promise} A promise that resolves to an object containing the list of shares and pagination information. * @throws {Error} Throws an error if the request fails. */ export const getStocks = async ( @@ -40,35 +39,71 @@ export const getStocks = async ( }; }; -export const getStockByTicker = async (ticker: string): Promise => { +/** + * Fetches details of a specific stock by its ticker. + * + * This function makes a GET request to the /sharesByTicker endpoint of the API + * and returns the detailed information about the stock. + * + * @async + * @function getStockByTicker + * @param {string} ticker - The ticker symbol of the stock. + * @returns {Promise} A promise that resolves to an object containing the stock details. + * @throws {Error} Throws an error if the request fails. + */ +export const getStockByTicker = async ( + ticker: string, +): Promise => { const { data } = await axios.get<{ data: IInstrument; }>(`${API_BASE_URL}/sharesByTicker`, { params: { ticker }, }); - return { - data, - }; + return data.data; }; -export const getLastPriceByTicker = async (ticker: string): Promise => { +/** + * Fetches the last price of a stock by its ticker. + * + * This function makes a GET request to the /lastPriceByTicker endpoint of the API + * and returns the last known price of the stock. + * + * @async + * @function getLastPriceByTicker + * @param {string} ticker - The ticker symbol of the stock. + * @returns {Promise} A promise that resolves to an object containing the last price of the stock. + * @throws {Error} Throws an error if the request fails. + */ +export const getLastPriceByTicker = async ( + ticker: string, +): Promise => { const { data } = await axios.get<{ data: IInstrument; }>(`${API_BASE_URL}/lastPriceByTicker`, { params: { ticker }, }); - return { - data, - }; + return data.data; }; -export const getCandlesByTicker = async (ticker: string): Promise => { +/** + * Fetches the candle data for a stock by its ticker. + * + * This function makes a GET request to the /candlesByTicker endpoint of the API + * and returns the candle data for the stock. + * + * @async + * @function getCandlesByTicker + * @param {string} ticker - The ticker symbol of the stock. + * @returns {Promise} A promise that resolves to an object containing the candle data. + * @throws {Error} Throws an error if the request fails. + */ +export const getCandlesByTicker = async ( + ticker: string, +): Promise => { const { data } = await axios.get<{ data: IInstrument; }>(`${API_BASE_URL}/candlesByTicker`, { params: { ticker }, }); - return { - data, - }; + return data.data; }; diff --git a/app/[slug]/page.tsx b/app/[slug]/page.tsx index 4c1c235..cad4887 100644 --- a/app/[slug]/page.tsx +++ b/app/[slug]/page.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { Metadata, ResolvingMetadata } from 'next'; +import { Metadata } from 'next'; import { BASE_URL } from '@/config'; import { serviceStocks } from '@/services'; import { StockIntro } from '@/components/StockIntro'; @@ -11,12 +11,8 @@ type Props = { params: { slug: string }; }; -export async function generateMetadata( - { params }: Props, - parent: ResolvingMetadata, -): Promise { - const stockData = await serviceStocks.getByTicker(params.slug); - const dataIntro = stockData?.data; +export async function generateMetadata({ params }: Props): Promise { + const dataIntro = await serviceStocks.getByTicker(params.slug); if (!dataIntro) { return { @@ -51,35 +47,28 @@ const PageStock = async ({ params }: Props) => { serviceStocks.getCandlesByTicker(params.slug), ]); - const dataIntro = stockData?.data; - const dataLastPrice = lastPriceData?.data; - const dataCandlesByTicker = candlesData?.data; - return (
- {stockData?.data ? ( - + {stockData ? ( + ) : ( Not data )}
- {candlesData?.data ? ( - + {candlesData ? ( + ) : ( Not data )}
- {lastPriceData?.data && candlesData?.data ? ( + {lastPriceData && candlesData ? ( ) : ( Not data diff --git a/components/BuyStock/BuyStock.tsx b/components/BuyStock/BuyStock.tsx index 04589df..c1538fd 100644 --- a/components/BuyStock/BuyStock.tsx +++ b/components/BuyStock/BuyStock.tsx @@ -3,25 +3,13 @@ import styles from './styles.module.scss'; import { Button } from '@/components/Button'; import { Profit } from '@/components/Profit'; -interface Price { - units: string; - nano: number; -} - -interface PriceInfo { - figi: string; - price: Price; - time: string; - instrumentUid: string; - lastPriceType: string; -} - export const BuyStock: React.FC<{ - data: PriceInfo; - currency: string; + data?: any; + currency?: string; candlesData: any; }> = ({ data, currency, candlesData }) => { const formattedPrice = parseFloat( + // @ts-ignore `${data?.price?.units}.${data?.price?.nano / 1e9}`, ).toFixed(2); diff --git a/components/Candles/Candles.tsx b/components/Candles/Candles.tsx index 44915ba..cb59c68 100644 --- a/components/Candles/Candles.tsx +++ b/components/Candles/Candles.tsx @@ -5,7 +5,7 @@ import dynamic from 'next/dynamic'; import styles from './styles.module.scss'; const Chart = dynamic(() => import('./Chart/Chart'), { ssr: false }); -const Candles = ({ data, currency }: { data: any, currency: string }) => { +const Candles = ({ data, currency }: { data: any, currency: any }) => { return (
diff --git a/components/StocksList/Pagination/Pagination.tsx b/components/StocksList/Pagination/Pagination.tsx index 6d096f1..5435b3d 100644 --- a/components/StocksList/Pagination/Pagination.tsx +++ b/components/StocksList/Pagination/Pagination.tsx @@ -5,8 +5,6 @@ import dynamic from 'next/dynamic'; const LoadMore = dynamic(() => import('./LoadMore'), { ssr: false }); import styles from './pagination.module.scss'; -// todo need fix load more - export const Pagination = ({ currentPage, totalPages, diff --git a/package.json b/package.json index 7123202..b471d56 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "@reduxjs/toolkit": "^2.2.6", "axios": "^1.7.2", "classnames": "^2.5.1", - "next": "14.2.4", + "next": "^14.2.7", "next-redux-wrapper": "^8.1.0", "prettier": "^3.3.2", "react": "^18", diff --git a/services/stocks/index.ts b/services/stocks/index.ts index 57ed540..c3b2eb9 100644 --- a/services/stocks/index.ts +++ b/services/stocks/index.ts @@ -4,29 +4,54 @@ import { getStockByTicker, getStocks, } from '@/actions/shares'; -import { IPagination } from '@/typing'; +import { IPagination, IApiResponseShares, IInstrument } from '@/typing'; +/** + * Service class for interacting with stock data. + * + * This class provides methods to fetch stock lists, details, prices, and candle data. + */ class ServiceStocks { constructor() {} - async getList({ start = 1, end = 24 }: IPagination) { + /** + * Fetches a list of stocks with pagination. + * + * @param {IPagination} pagination - The pagination parameters. + * @returns {Promise} A promise that resolves to the list of stocks or undefined if an error occurs. + */ + async getList({ + start = 1, + end = 24, + }: IPagination): Promise { try { - const res = await getStocks({ start, end }); - return res; + return await getStocks({ start, end }); } catch (e) { this.handleError(e); } } - async getByTicker(ticker: string) { + /** + * Fetches details of a stock by its ticker. + * + * @param {string} ticker - The ticker symbol of the stock. + * @returns {Promise} A promise that resolves to the stock details or undefined if an error occurs. + */ + async getByTicker(ticker: string): Promise { try { return await getStockByTicker(ticker); } catch (e) { - console.error(e); + this.handleError(e); } } - async getLastPriceByTicker(ticker: string) { + /** + * Fetches the last price of a stock by its ticker. + * + * @param {string} ticker - The ticker symbol of the stock. + * @returns {Promise} A promise that resolves to the last price or undefined if an error occurs. + */ + async getLastPriceByTicker(ticker: string): Promise { try { return await getLastPriceByTicker(ticker); } catch (e) { @@ -34,7 +59,13 @@ class ServiceStocks { } } - async getCandlesByTicker(ticker: string) { + /** + * Fetches the candle data for a stock by its ticker. + * + * @param {string} ticker - The ticker symbol of the stock. + * @returns {Promise} A promise that resolves to the candle data or undefined if an error occurs. + */ + async getCandlesByTicker(ticker: string): Promise { try { return await getCandlesByTicker(ticker); } catch (e) { @@ -42,8 +73,13 @@ class ServiceStocks { } } - handleError(err: any) { - console.error(err); + /** + * Handles errors by logging them. + * + * @param {any} err - The error to handle. + */ + private handleError(err: any) { + console.error('An error occurred:', err); } } diff --git a/typing/index.ts b/typing/index.ts index 6770797..dcb10e0 100644 --- a/typing/index.ts +++ b/typing/index.ts @@ -1,5 +1,3 @@ -// todo need fix type remove double - export interface IBrand { bg: string; color: string; @@ -59,12 +57,6 @@ export interface IMinPriceIncrement { nano: number; } -export interface IInstBrand { - logoName: string; - logoBaseColor: string; - textColor: string; -} - export interface IInstrument { figi: string; ticker: string; diff --git a/yarn.lock b/yarn.lock index 6c334db..5181dcf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -85,10 +85,10 @@ wrap-ansi "^8.1.0" wrap-ansi-cjs "npm:wrap-ansi@^7.0.0" -"@next/env@14.2.4": - version "14.2.4" - resolved "https://registry.npmjs.org/@next/env/-/env-14.2.4.tgz" - integrity sha512-3EtkY5VDkuV2+lNmKlbkibIJxcO4oIHEhBWne6PaAp+76J9KoSsGvNikp6ivzAT8dhhBMYrm6op2pS1ApG0Hzg== +"@next/env@14.2.7": + version "14.2.7" + resolved "https://registry.npmjs.org/@next/env/-/env-14.2.7.tgz" + integrity sha512-OTx9y6I3xE/eih+qtthppwLytmpJVPM5PPoJxChFsbjIEFXIayG0h/xLzefHGJviAa3Q5+Fd+9uYojKkHDKxoQ== "@next/eslint-plugin-next@14.2.4": version "14.2.4" @@ -97,10 +97,10 @@ dependencies: glob "10.3.10" -"@next/swc-darwin-arm64@14.2.4": - version "14.2.4" - resolved "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.2.4.tgz" - integrity sha512-AH3mO4JlFUqsYcwFUHb1wAKlebHU/Hv2u2kb1pAuRanDZ7pD/A/KPD98RHZmwsJpdHQwfEc/06mgpSzwrJYnNg== +"@next/swc-darwin-arm64@14.2.7": + version "14.2.7" + resolved "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.2.7.tgz" + integrity sha512-UhZGcOyI9LE/tZL3h9rs/2wMZaaJKwnpAyegUVDGZqwsla6hMfeSj9ssBWQS9yA4UXun3pPhrFLVnw5KXZs3vw== "@nodelib/fs.scandir@2.1.5": version "2.1.5" @@ -1788,12 +1788,12 @@ next-redux-wrapper@^8.1.0: resolved "https://registry.npmjs.org/next-redux-wrapper/-/next-redux-wrapper-8.1.0.tgz" integrity sha512-2hIau0hcI6uQszOtrvAFqgc0NkZegKYhBB7ZAKiG3jk7zfuQb4E7OV9jfxViqqojh3SEHdnFfPkN9KErttUKuw== -next@>=9, next@14.2.4: - version "14.2.4" - resolved "https://registry.npmjs.org/next/-/next-14.2.4.tgz" - integrity sha512-R8/V7vugY+822rsQGQCjoLhMuC9oFj9SOi4Cl4b2wjDrseD0LRZ10W7R6Czo4w9ZznVSshKjuIomsRjvm9EKJQ== +next@^14.2.7, next@>=9: + version "14.2.7" + resolved "https://registry.npmjs.org/next/-/next-14.2.7.tgz" + integrity sha512-4Qy2aK0LwH4eQiSvQWyKuC7JXE13bIopEQesWE0c/P3uuNRnZCQanI0vsrMLmUQJLAto+A+/8+sve2hd+BQuOQ== dependencies: - "@next/env" "14.2.4" + "@next/env" "14.2.7" "@swc/helpers" "0.5.5" busboy "1.6.0" caniuse-lite "^1.0.30001579" @@ -1801,15 +1801,15 @@ next@>=9, next@14.2.4: postcss "8.4.31" styled-jsx "5.1.1" optionalDependencies: - "@next/swc-darwin-arm64" "14.2.4" - "@next/swc-darwin-x64" "14.2.4" - "@next/swc-linux-arm64-gnu" "14.2.4" - "@next/swc-linux-arm64-musl" "14.2.4" - "@next/swc-linux-x64-gnu" "14.2.4" - "@next/swc-linux-x64-musl" "14.2.4" - "@next/swc-win32-arm64-msvc" "14.2.4" - "@next/swc-win32-ia32-msvc" "14.2.4" - "@next/swc-win32-x64-msvc" "14.2.4" + "@next/swc-darwin-arm64" "14.2.7" + "@next/swc-darwin-x64" "14.2.7" + "@next/swc-linux-arm64-gnu" "14.2.7" + "@next/swc-linux-arm64-musl" "14.2.7" + "@next/swc-linux-x64-gnu" "14.2.7" + "@next/swc-linux-x64-musl" "14.2.7" + "@next/swc-win32-arm64-msvc" "14.2.7" + "@next/swc-win32-ia32-msvc" "14.2.7" + "@next/swc-win32-x64-msvc" "14.2.7" normalize-path@^3.0.0, normalize-path@~3.0.0: version "3.0.0"