Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
u4aew committed Sep 2, 2024
1 parent 4f5f999 commit 8f24426
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 98 deletions.
69 changes: 52 additions & 17 deletions actions/shares/shares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object>} A promise that resolves to an object containing the response data.
* @param {IPagination} pagination - The pagination parameters for the request.
* @returns {Promise<IApiResponseShares>} 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 (
Expand All @@ -40,35 +39,71 @@ export const getStocks = async (
};
};

export const getStockByTicker = async (ticker: string): Promise<any> => {
/**
* 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<IInstrument>} 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<IInstrument> => {
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<any> => {
/**
* 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<IInstrument>} 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<IInstrument> => {
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<any> => {
/**
* 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<IInstrument>} 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<IInstrument> => {
const { data } = await axios.get<{
data: IInstrument;
}>(`${API_BASE_URL}/candlesByTicker`, {
params: { ticker },
});
return {
data,
};
return data.data;
};
33 changes: 11 additions & 22 deletions app/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,12 +11,8 @@ type Props = {
params: { slug: string };
};

export async function generateMetadata(
{ params }: Props,
parent: ResolvingMetadata,
): Promise<Metadata> {
const stockData = await serviceStocks.getByTicker(params.slug);
const dataIntro = stockData?.data;
export async function generateMetadata({ params }: Props): Promise<Metadata> {
const dataIntro = await serviceStocks.getByTicker(params.slug);

if (!dataIntro) {
return {
Expand Down Expand Up @@ -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 (
<div className={styles.page}>
<div className={styles.main}>
<div className={styles.intro}>
{stockData?.data ? (
<StockIntro data={stockData?.data} />
{stockData ? (
<StockIntro data={stockData} />
) : (
<span>Not data</span>
)}
</div>
{candlesData?.data ? (
<Candles
currency={stockData?.data?.currency}
data={candlesData?.data}
/>
{candlesData ? (
<Candles currency={stockData?.currency} data={candlesData} />
) : (
<span>Not data</span>
)}
</div>
<div className={styles.side}>
{lastPriceData?.data && candlesData?.data ? (
{lastPriceData && candlesData ? (
<BuyStock
candlesData={candlesData?.data}
currency={stockData?.data?.currency}
data={lastPriceData?.data}
candlesData={candlesData}
currency={stockData?.currency}
data={lastPriceData}
/>
) : (
<span>Not data</span>
Expand Down
18 changes: 3 additions & 15 deletions components/BuyStock/BuyStock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion components/Candles/Candles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className={styles.chart}>
<Chart data={data} currency={currency} />
Expand Down
2 changes: 0 additions & 2 deletions components/StocksList/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 46 additions & 10 deletions services/stocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,82 @@ 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<IApiResponseShares | undefined>} A promise that resolves to the list of stocks or undefined if an error occurs.
*/
async getList({
start = 1,
end = 24,
}: IPagination): Promise<IApiResponseShares | undefined> {
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<IInstrument | undefined>} A promise that resolves to the stock details or undefined if an error occurs.
*/
async getByTicker(ticker: string): Promise<IInstrument | undefined> {
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<IInstrument | undefined>} A promise that resolves to the last price or undefined if an error occurs.
*/
async getLastPriceByTicker(ticker: string): Promise<IInstrument | undefined> {
try {
return await getLastPriceByTicker(ticker);
} catch (e) {
this.handleError(e);
}
}

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<IInstrument | undefined>} A promise that resolves to the candle data or undefined if an error occurs.
*/
async getCandlesByTicker(ticker: string): Promise<IInstrument | undefined> {
try {
return await getCandlesByTicker(ticker);
} catch (e) {
this.handleError(e);
}
}

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);
}
}

Expand Down
8 changes: 0 additions & 8 deletions typing/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// todo need fix type remove double

export interface IBrand {
bg: string;
color: string;
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 8f24426

Please sign in to comment.