@redneckz/wildless-cms-uni-blocks 0.14.1064 → 0.14.1065
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle/blocks.schema.json +1 -1
- package/bundle/bundle.umd.js +49 -27
- package/bundle/bundle.umd.min.js +1 -1
- package/bundle/components/RatesTable/RatesTable.d.ts +3 -0
- package/bundle/components/RatesTable/RatesTableContent.d.ts +13 -3
- package/bundle/components/RatesTable/RatesTableItem.d.ts +2 -2
- package/bundle/hooks/useRates.d.ts +11 -2
- package/dist/components/RatesTable/RatesTable.d.ts +3 -0
- package/dist/components/RatesTable/RatesTable.js +23 -5
- package/dist/components/RatesTable/RatesTable.js.map +1 -1
- package/dist/components/RatesTable/RatesTableContent.d.ts +13 -3
- package/dist/components/RatesTable/RatesTableItem.d.ts +2 -2
- package/dist/components/RatesTable/RatesTableItem.js.map +1 -1
- package/dist/hooks/useRates.d.ts +11 -2
- package/dist/hooks/useRates.js +28 -20
- package/dist/hooks/useRates.js.map +1 -1
- package/lib/components/RatesTable/RatesTable.d.ts +3 -0
- package/lib/components/RatesTable/RatesTable.fixture.d.ts +1 -1
- package/lib/components/RatesTable/RatesTable.fixture.mobile.d.ts +1 -1
- package/lib/components/RatesTable/RatesTable.js +23 -5
- package/lib/components/RatesTable/RatesTable.js.map +1 -1
- package/lib/components/RatesTable/RatesTableContent.d.ts +13 -3
- package/lib/components/RatesTable/RatesTableItem.d.ts +2 -2
- package/lib/components/RatesTable/RatesTableItem.js.map +1 -1
- package/lib/hooks/useRates.d.ts +11 -2
- package/lib/hooks/useRates.js +28 -20
- package/lib/hooks/useRates.js.map +1 -1
- package/mobile/bundle/bundle.umd.js +49 -27
- package/mobile/bundle/bundle.umd.min.js +1 -1
- package/mobile/bundle/components/RatesTable/RatesTable.d.ts +3 -0
- package/mobile/bundle/components/RatesTable/RatesTableContent.d.ts +13 -3
- package/mobile/bundle/components/RatesTable/RatesTableItem.d.ts +2 -2
- package/mobile/bundle/hooks/useRates.d.ts +11 -2
- package/mobile/dist/components/RatesTable/RatesTable.d.ts +3 -0
- package/mobile/dist/components/RatesTable/RatesTable.js +23 -5
- package/mobile/dist/components/RatesTable/RatesTable.js.map +1 -1
- package/mobile/dist/components/RatesTable/RatesTableContent.d.ts +13 -3
- package/mobile/dist/components/RatesTable/RatesTableItem.d.ts +2 -2
- package/mobile/dist/components/RatesTable/RatesTableItem.js.map +1 -1
- package/mobile/dist/hooks/useRates.d.ts +11 -2
- package/mobile/dist/hooks/useRates.js +28 -20
- package/mobile/dist/hooks/useRates.js.map +1 -1
- package/mobile/lib/components/RatesTable/RatesTable.d.ts +3 -0
- package/mobile/lib/components/RatesTable/RatesTable.js +23 -5
- package/mobile/lib/components/RatesTable/RatesTable.js.map +1 -1
- package/mobile/lib/components/RatesTable/RatesTableContent.d.ts +13 -3
- package/mobile/lib/components/RatesTable/RatesTableItem.d.ts +2 -2
- package/mobile/lib/components/RatesTable/RatesTableItem.js.map +1 -1
- package/mobile/lib/hooks/useRates.d.ts +11 -2
- package/mobile/lib/hooks/useRates.js +28 -20
- package/mobile/lib/hooks/useRates.js.map +1 -1
- package/mobile/src/components/RatesTable/RatesTable.example.json +5 -3
- package/mobile/src/components/RatesTable/RatesTable.tsx +71 -16
- package/mobile/src/components/RatesTable/RatesTableContent.ts +14 -3
- package/mobile/src/components/RatesTable/RatesTableItem.tsx +2 -1
- package/mobile/src/hooks/useRates.ts +46 -23
- package/package.json +1 -1
- package/src/components/RatesTable/RatesTable.example.json +5 -3
- package/src/components/RatesTable/RatesTable.fixture.mobile.tsx +10 -5
- package/src/components/RatesTable/RatesTable.fixture.tsx +10 -5
- package/src/components/RatesTable/RatesTable.tsx +71 -16
- package/src/components/RatesTable/RatesTableContent.ts +14 -3
- package/src/components/RatesTable/RatesTableItem.tsx +2 -1
- package/src/hooks/useRates.ts +46 -23
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
import { type HrefProps } from '../../model/LinkProps';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @title
|
|
4
|
+
* @title Тип курсов
|
|
5
|
+
* @default "Курсы для физ.лиц"
|
|
6
|
+
* @enumNames [
|
|
7
|
+
* "Курсы для физ.лиц",
|
|
8
|
+
* "Курсы для держателей карт"
|
|
9
|
+
* ]
|
|
10
|
+
*/
|
|
11
|
+
export type RatesType = 'natural' | 'cards';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @title Кросс-курсы
|
|
5
15
|
*/
|
|
6
16
|
export interface RatesTableContent extends HrefProps {
|
|
7
17
|
/** @title Информационное сообщение */
|
|
8
18
|
infoMessage?: string;
|
|
9
19
|
/** @title Заголовок */
|
|
10
20
|
title?: string;
|
|
11
|
-
|
|
12
|
-
|
|
21
|
+
ratesType?: RatesType;
|
|
22
|
+
/** @title Сообщение при отсутствии ставок */
|
|
23
|
+
notFoundMessage?: string;
|
|
13
24
|
}
|
|
@@ -3,10 +3,11 @@ import { Paragraph } from '../../ui-kit/Paragraph/Paragraph';
|
|
|
3
3
|
import { Rate, RATES_COLUMN_STYLE, type RateItem } from '../../ui-kit/Rate/Rate';
|
|
4
4
|
import { Text } from '../../ui-kit/Text/Text';
|
|
5
5
|
import { style } from '../../utils/style';
|
|
6
|
+
import { type ArchiveRates } from './RatesTable';
|
|
6
7
|
import { type RatesTableContent } from './RatesTableContent';
|
|
7
8
|
import { renderDate } from './renderDate';
|
|
8
9
|
|
|
9
|
-
interface RatesTableItemProps extends RatesTableContent {
|
|
10
|
+
export interface RatesTableItemProps extends RatesTableContent, ArchiveRates {
|
|
10
11
|
maxDate?: string;
|
|
11
12
|
isRatesValid?: boolean;
|
|
12
13
|
list: RateItem[];
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useAsyncData } from '@redneckz/uni-jsx/lib/hooks/useAsyncData';
|
|
2
2
|
import { useLocalStore } from '@redneckz/uni-jsx/lib/Store/useLocalStore';
|
|
3
3
|
import { API_BASE_URI } from '../api/apiBaseUrl';
|
|
4
|
+
import { type RatesType } from '../components/RatesTable/RatesTableContent';
|
|
4
5
|
import { type RateItem } from '../ui-kit/Rate/Rate';
|
|
5
6
|
import { fetchJSONUnsafe, type FetchJSONUnsafeType } from '../utils/fetchJSON';
|
|
6
7
|
|
|
@@ -9,33 +10,43 @@ type RatesStoreItem = {
|
|
|
9
10
|
nextUpdate: number;
|
|
10
11
|
};
|
|
11
12
|
|
|
12
|
-
type RateItemList = RateItem[];
|
|
13
|
+
export type RateItemList = RateItem[];
|
|
13
14
|
|
|
14
15
|
type RatesStoreSlice = {
|
|
15
16
|
rates: RatesStoreItem;
|
|
16
17
|
archiveRates: RatesStoreItem;
|
|
17
18
|
};
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
20
|
+
type FetchRatesProps = {
|
|
21
|
+
ratesType?: RatesType;
|
|
22
|
+
date?: Date;
|
|
23
|
+
fetchArchive?: boolean;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const EMPTY_RATES: RateItemList[] = [];
|
|
27
|
+
|
|
28
|
+
const RATES_URL: Record<RatesType, string> = {
|
|
29
|
+
natural: `${API_BASE_URI}/rates`,
|
|
30
|
+
cards: `${API_BASE_URI}/ratescards`,
|
|
31
|
+
//archive: `${API_BASE_URI}/historyrates`,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const RATES_KEY: Record<RatesType, string> = {
|
|
35
|
+
natural: 'rates',
|
|
36
|
+
cards: 'ratescards',
|
|
37
|
+
//archive: 'historyrates',
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// TODO: для архива прокинуть date
|
|
41
|
+
export function useRates({ ratesType = 'natural', fetchArchive = true }: FetchRatesProps) {
|
|
42
|
+
//const url = ratesType === 'archive' ? getArchiveRatesURL(date) : RATES_URL[ratesType];
|
|
43
|
+
const url = RATES_URL[ratesType];
|
|
44
|
+
const rateKey = RATES_KEY[ratesType];
|
|
34
45
|
|
|
35
46
|
const ratesStore = useLocalStore<RatesStoreSlice>();
|
|
36
47
|
|
|
37
48
|
// проверяем, надо ли обновлять значения ставок
|
|
38
|
-
const shouldUpdate = getShouldUpdate(
|
|
49
|
+
const shouldUpdate = getShouldUpdate(rateKey, ratesStore as RatesStoreSlice, fetchArchive);
|
|
39
50
|
|
|
40
51
|
// костыль, не отправлять запрос на бэк, если уже был запрос менее 15 мин назад
|
|
41
52
|
const { data } = useAsyncData(
|
|
@@ -46,20 +57,32 @@ export function useRates(isArchive: boolean): RateItemList[] {
|
|
|
46
57
|
// если есть data и значение надо обновить, то обновляем локалсторадж
|
|
47
58
|
if (shouldUpdate && data) {
|
|
48
59
|
const nextUpdate = getTimeAfter15Minutes();
|
|
49
|
-
ratesStore[rateKey] = { rate: data, nextUpdate };
|
|
60
|
+
ratesStore[rateKey] = { rate: data ?? [], nextUpdate };
|
|
50
61
|
}
|
|
51
62
|
|
|
52
63
|
// берем данные из локалсторадж
|
|
53
64
|
const rates = ratesStore[rateKey]?.rate;
|
|
54
65
|
|
|
66
|
+
const isLoad = Array.isArray(rates);
|
|
67
|
+
|
|
55
68
|
// возвращаем данные из локалсторадж или заглушку
|
|
56
|
-
return
|
|
69
|
+
return { ratesList: rates ?? EMPTY_RATES, isLoad };
|
|
57
70
|
}
|
|
58
71
|
|
|
59
72
|
// Отсчитываем 15 минут с текущего момента
|
|
60
73
|
const getTimeAfter15Minutes = () => Date.now() + 15 * 60 * 1000;
|
|
61
74
|
|
|
62
|
-
const getShouldUpdate = (
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
75
|
+
const getShouldUpdate = (
|
|
76
|
+
rateKey: string,
|
|
77
|
+
ratesStore: RatesStoreSlice | null,
|
|
78
|
+
fetchArchive: boolean,
|
|
79
|
+
) =>
|
|
80
|
+
rateKey === 'archive'
|
|
81
|
+
? (!ratesStore?.[rateKey] || ratesStore?.[rateKey].nextUpdate <= Date.now()) && fetchArchive
|
|
82
|
+
: !ratesStore?.[rateKey] || ratesStore?.[rateKey].nextUpdate <= Date.now();
|
|
83
|
+
|
|
84
|
+
/*const getArchiveRatesURL = (date?: Date) => {
|
|
85
|
+
const formattedDate = date && formatDate(date, true);
|
|
86
|
+
|
|
87
|
+
return formattedDate ? `${RATES_URL['archive']}?date=${formattedDate}` : RATES_URL['archive'];
|
|
88
|
+
};*/
|
package/package.json
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"content": {
|
|
3
|
-
"infoMessage": "",
|
|
4
|
-
"title": "Курс покупки/продажи
|
|
5
|
-
"href": ""
|
|
3
|
+
"infoMessage": "Информация может содержать неточности, актуальный курс уточняйте в отделении Банка",
|
|
4
|
+
"title": "Курс покупки/продажи",
|
|
5
|
+
"href": "https://www.rshb.ru",
|
|
6
|
+
"ratesType": "natural",
|
|
7
|
+
"notFoundMessage": "Подходящие ставки не найдены"
|
|
6
8
|
}
|
|
7
9
|
}
|
|
@@ -7,20 +7,25 @@ const ratesTableContent = {
|
|
|
7
7
|
href: 'rshb.ru',
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
-
const archiveRatesTableContent = {
|
|
10
|
+
/*const archiveRatesTableContent = {
|
|
11
11
|
title: 'Архив курсов покупки/продажи для операций с использованием платежных карт в сети Банка',
|
|
12
|
-
|
|
12
|
+
notFoundMessage: 'За этот день ставки не приходили',
|
|
13
|
+
};*/
|
|
14
|
+
|
|
15
|
+
const cardsRatesTableContent = {
|
|
16
|
+
title: 'Курс покупки/продажи для операций с использованием платежных карт в сети Банка',
|
|
17
|
+
notFoundMessage: 'Подходящих ставок нет',
|
|
13
18
|
};
|
|
14
19
|
|
|
15
20
|
export default {
|
|
16
21
|
default: (
|
|
17
22
|
<div className="container grid grid-cols-12">
|
|
18
|
-
<RatesTable className="col-span-12" {...ratesTableContent} />
|
|
23
|
+
<RatesTable className="col-span-12" {...ratesTableContent} ratesType="natural" />
|
|
19
24
|
</div>
|
|
20
25
|
),
|
|
21
|
-
|
|
26
|
+
cards: (
|
|
22
27
|
<div className="container grid grid-cols-12">
|
|
23
|
-
<RatesTable className="col-span-12" {...
|
|
28
|
+
<RatesTable className="col-span-12" {...cardsRatesTableContent} ratesType="cards" />
|
|
24
29
|
</div>
|
|
25
30
|
),
|
|
26
31
|
};
|
|
@@ -7,20 +7,25 @@ const ratesTableContent = {
|
|
|
7
7
|
href: 'rshb.ru',
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
-
const archiveRatesTableContent = {
|
|
10
|
+
/*const archiveRatesTableContent = {
|
|
11
11
|
title: 'Архив курсов покупки/продажи для операций с использованием платежных карт в сети Банка',
|
|
12
|
-
|
|
12
|
+
notFoundMessage: 'За этот день ставки не приходили',
|
|
13
|
+
};*/
|
|
14
|
+
|
|
15
|
+
const cardsRatesTableContent = {
|
|
16
|
+
title: 'Курс покупки/продажи для операций с использованием платежных карт в сети Банка',
|
|
17
|
+
notFoundMessage: 'Подходящих ставок нет',
|
|
13
18
|
};
|
|
14
19
|
|
|
15
20
|
export default {
|
|
16
21
|
default: (
|
|
17
22
|
<div className="container grid grid-cols-12">
|
|
18
|
-
<RatesTable className="col-span-12" {...ratesTableContent} />
|
|
23
|
+
<RatesTable className="col-span-12" {...ratesTableContent} ratesType="natural" />
|
|
19
24
|
</div>
|
|
20
25
|
),
|
|
21
|
-
|
|
26
|
+
cards: (
|
|
22
27
|
<div className="container grid grid-cols-12">
|
|
23
|
-
<RatesTable className="col-span-12" {...
|
|
28
|
+
<RatesTable className="col-span-12" {...cardsRatesTableContent} ratesType="cards" />
|
|
24
29
|
</div>
|
|
25
30
|
),
|
|
26
31
|
};
|
|
@@ -1,38 +1,93 @@
|
|
|
1
1
|
import { JSX } from '@redneckz/uni-jsx';
|
|
2
|
-
import {
|
|
2
|
+
import { useEffect, useState } from '@redneckz/uni-jsx/lib/hooks';
|
|
3
|
+
import { useRates, type RateItemList } from '../../hooks/useRates';
|
|
3
4
|
import { BlockWrapper } from '../../ui-kit/BlockWrapper';
|
|
4
5
|
import { Heading } from '../../ui-kit/Heading/Heading';
|
|
6
|
+
import { Loader } from '../../ui-kit/Loader/Loader';
|
|
7
|
+
import { NotFound } from '../../ui-kit/NotFound/NotFound';
|
|
5
8
|
import { type UniBlockProps } from '../../UniBlock/UniBlockProps';
|
|
6
9
|
import { style } from '../../utils/style';
|
|
7
10
|
import { type RatesTableContent } from './RatesTableContent';
|
|
8
11
|
import { RatesTableItem } from './RatesTableItem';
|
|
9
|
-
import { renderDataPickForm } from './renderDataPickForm';
|
|
10
12
|
import { renderLink } from './renderLink';
|
|
11
13
|
|
|
12
14
|
interface RatesTableProps extends RatesTableContent, UniBlockProps {}
|
|
13
15
|
|
|
16
|
+
interface RenderRatesContent extends RatesTableContent, ArchiveRates {
|
|
17
|
+
isRatesValid?: boolean;
|
|
18
|
+
ratesList?: RateItemList[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type ArchiveRates = {
|
|
22
|
+
isArchive?: boolean;
|
|
23
|
+
};
|
|
24
|
+
|
|
14
25
|
export const RatesTable = JSX<RatesTableProps>(
|
|
15
|
-
({
|
|
16
|
-
|
|
26
|
+
({
|
|
27
|
+
href,
|
|
28
|
+
ratesType = 'natural',
|
|
29
|
+
className = '',
|
|
30
|
+
infoMessage,
|
|
31
|
+
notFoundMessage,
|
|
32
|
+
title,
|
|
33
|
+
...rest
|
|
34
|
+
}) => {
|
|
35
|
+
// const [date, setDate] = useState<Date>();
|
|
36
|
+
const [fetchArchive, setFetchArchive] = useState<boolean>(true);
|
|
37
|
+
|
|
38
|
+
//const isArchive = ratesType === 'archive';
|
|
39
|
+
|
|
40
|
+
const { ratesList, isLoad } = useRates({
|
|
41
|
+
ratesType,
|
|
42
|
+
fetchArchive,
|
|
43
|
+
});
|
|
17
44
|
|
|
18
|
-
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
setFetchArchive(false);
|
|
47
|
+
}, []);
|
|
48
|
+
|
|
49
|
+
const isRatesValid = ratesList?.length > 0;
|
|
19
50
|
|
|
20
51
|
return (
|
|
21
52
|
<BlockWrapper className={style('p-6xl space-y-xl', className)} defaultPadding="p-0" {...rest}>
|
|
22
53
|
<Heading className="mb-xl">{title}</Heading>
|
|
23
|
-
{isArchive
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
infoMessage
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
54
|
+
{/*{isArchive
|
|
55
|
+
? renderDataPickForm({ value: date, onChange: setDate, handleClick: setFetchArchive })
|
|
56
|
+
: null}*/}
|
|
57
|
+
{isLoad ? (
|
|
58
|
+
renderRatesContent({
|
|
59
|
+
ratesList,
|
|
60
|
+
isArchive: false,
|
|
61
|
+
infoMessage,
|
|
62
|
+
isRatesValid,
|
|
63
|
+
notFoundMessage,
|
|
64
|
+
})
|
|
65
|
+
) : (
|
|
66
|
+
<Loader position="static" />
|
|
67
|
+
)}
|
|
34
68
|
{href ? renderLink(href) : null}
|
|
35
69
|
</BlockWrapper>
|
|
36
70
|
);
|
|
37
71
|
},
|
|
38
72
|
);
|
|
73
|
+
|
|
74
|
+
const renderRatesContent = ({
|
|
75
|
+
ratesList = [],
|
|
76
|
+
isArchive = false,
|
|
77
|
+
infoMessage,
|
|
78
|
+
isRatesValid,
|
|
79
|
+
notFoundMessage,
|
|
80
|
+
}: RenderRatesContent) =>
|
|
81
|
+
ratesList.length > 0 ? (
|
|
82
|
+
ratesList.map((list, i) => (
|
|
83
|
+
<RatesTableItem
|
|
84
|
+
list={list}
|
|
85
|
+
isRatesValid={isRatesValid}
|
|
86
|
+
isArchive={isArchive}
|
|
87
|
+
key={String(i)}
|
|
88
|
+
infoMessage={infoMessage}
|
|
89
|
+
/>
|
|
90
|
+
))
|
|
91
|
+
) : (
|
|
92
|
+
<NotFound description={notFoundMessage} />
|
|
93
|
+
);
|
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
import { type HrefProps } from '../../model/LinkProps';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @title
|
|
4
|
+
* @title Тип курсов
|
|
5
|
+
* @default "Курсы для физ.лиц"
|
|
6
|
+
* @enumNames [
|
|
7
|
+
* "Курсы для физ.лиц",
|
|
8
|
+
* "Курсы для держателей карт"
|
|
9
|
+
* ]
|
|
10
|
+
*/
|
|
11
|
+
export type RatesType = 'natural' | 'cards';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @title Кросс-курсы
|
|
5
15
|
*/
|
|
6
16
|
export interface RatesTableContent extends HrefProps {
|
|
7
17
|
/** @title Информационное сообщение */
|
|
8
18
|
infoMessage?: string;
|
|
9
19
|
/** @title Заголовок */
|
|
10
20
|
title?: string;
|
|
11
|
-
|
|
12
|
-
|
|
21
|
+
ratesType?: RatesType;
|
|
22
|
+
/** @title Сообщение при отсутствии ставок */
|
|
23
|
+
notFoundMessage?: string;
|
|
13
24
|
}
|
|
@@ -3,10 +3,11 @@ import { Paragraph } from '../../ui-kit/Paragraph/Paragraph';
|
|
|
3
3
|
import { Rate, RATES_COLUMN_STYLE, type RateItem } from '../../ui-kit/Rate/Rate';
|
|
4
4
|
import { Text } from '../../ui-kit/Text/Text';
|
|
5
5
|
import { style } from '../../utils/style';
|
|
6
|
+
import { type ArchiveRates } from './RatesTable';
|
|
6
7
|
import { type RatesTableContent } from './RatesTableContent';
|
|
7
8
|
import { renderDate } from './renderDate';
|
|
8
9
|
|
|
9
|
-
interface RatesTableItemProps extends RatesTableContent {
|
|
10
|
+
export interface RatesTableItemProps extends RatesTableContent, ArchiveRates {
|
|
10
11
|
maxDate?: string;
|
|
11
12
|
isRatesValid?: boolean;
|
|
12
13
|
list: RateItem[];
|
package/src/hooks/useRates.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useAsyncData } from '@redneckz/uni-jsx/lib/hooks/useAsyncData';
|
|
2
2
|
import { useLocalStore } from '@redneckz/uni-jsx/lib/Store/useLocalStore';
|
|
3
3
|
import { API_BASE_URI } from '../api/apiBaseUrl';
|
|
4
|
+
import { type RatesType } from '../components/RatesTable/RatesTableContent';
|
|
4
5
|
import { type RateItem } from '../ui-kit/Rate/Rate';
|
|
5
6
|
import { fetchJSONUnsafe, type FetchJSONUnsafeType } from '../utils/fetchJSON';
|
|
6
7
|
|
|
@@ -9,33 +10,43 @@ type RatesStoreItem = {
|
|
|
9
10
|
nextUpdate: number;
|
|
10
11
|
};
|
|
11
12
|
|
|
12
|
-
type RateItemList = RateItem[];
|
|
13
|
+
export type RateItemList = RateItem[];
|
|
13
14
|
|
|
14
15
|
type RatesStoreSlice = {
|
|
15
16
|
rates: RatesStoreItem;
|
|
16
17
|
archiveRates: RatesStoreItem;
|
|
17
18
|
};
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
20
|
+
type FetchRatesProps = {
|
|
21
|
+
ratesType?: RatesType;
|
|
22
|
+
date?: Date;
|
|
23
|
+
fetchArchive?: boolean;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const EMPTY_RATES: RateItemList[] = [];
|
|
27
|
+
|
|
28
|
+
const RATES_URL: Record<RatesType, string> = {
|
|
29
|
+
natural: `${API_BASE_URI}/rates`,
|
|
30
|
+
cards: `${API_BASE_URI}/ratescards`,
|
|
31
|
+
//archive: `${API_BASE_URI}/historyrates`,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const RATES_KEY: Record<RatesType, string> = {
|
|
35
|
+
natural: 'rates',
|
|
36
|
+
cards: 'ratescards',
|
|
37
|
+
//archive: 'historyrates',
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// TODO: для архива прокинуть date
|
|
41
|
+
export function useRates({ ratesType = 'natural', fetchArchive = true }: FetchRatesProps) {
|
|
42
|
+
//const url = ratesType === 'archive' ? getArchiveRatesURL(date) : RATES_URL[ratesType];
|
|
43
|
+
const url = RATES_URL[ratesType];
|
|
44
|
+
const rateKey = RATES_KEY[ratesType];
|
|
34
45
|
|
|
35
46
|
const ratesStore = useLocalStore<RatesStoreSlice>();
|
|
36
47
|
|
|
37
48
|
// проверяем, надо ли обновлять значения ставок
|
|
38
|
-
const shouldUpdate = getShouldUpdate(
|
|
49
|
+
const shouldUpdate = getShouldUpdate(rateKey, ratesStore as RatesStoreSlice, fetchArchive);
|
|
39
50
|
|
|
40
51
|
// костыль, не отправлять запрос на бэк, если уже был запрос менее 15 мин назад
|
|
41
52
|
const { data } = useAsyncData(
|
|
@@ -46,20 +57,32 @@ export function useRates(isArchive: boolean): RateItemList[] {
|
|
|
46
57
|
// если есть data и значение надо обновить, то обновляем локалсторадж
|
|
47
58
|
if (shouldUpdate && data) {
|
|
48
59
|
const nextUpdate = getTimeAfter15Minutes();
|
|
49
|
-
ratesStore[rateKey] = { rate: data, nextUpdate };
|
|
60
|
+
ratesStore[rateKey] = { rate: data ?? [], nextUpdate };
|
|
50
61
|
}
|
|
51
62
|
|
|
52
63
|
// берем данные из локалсторадж
|
|
53
64
|
const rates = ratesStore[rateKey]?.rate;
|
|
54
65
|
|
|
66
|
+
const isLoad = Array.isArray(rates);
|
|
67
|
+
|
|
55
68
|
// возвращаем данные из локалсторадж или заглушку
|
|
56
|
-
return
|
|
69
|
+
return { ratesList: rates ?? EMPTY_RATES, isLoad };
|
|
57
70
|
}
|
|
58
71
|
|
|
59
72
|
// Отсчитываем 15 минут с текущего момента
|
|
60
73
|
const getTimeAfter15Minutes = () => Date.now() + 15 * 60 * 1000;
|
|
61
74
|
|
|
62
|
-
const getShouldUpdate = (
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
75
|
+
const getShouldUpdate = (
|
|
76
|
+
rateKey: string,
|
|
77
|
+
ratesStore: RatesStoreSlice | null,
|
|
78
|
+
fetchArchive: boolean,
|
|
79
|
+
) =>
|
|
80
|
+
rateKey === 'archive'
|
|
81
|
+
? (!ratesStore?.[rateKey] || ratesStore?.[rateKey].nextUpdate <= Date.now()) && fetchArchive
|
|
82
|
+
: !ratesStore?.[rateKey] || ratesStore?.[rateKey].nextUpdate <= Date.now();
|
|
83
|
+
|
|
84
|
+
/*const getArchiveRatesURL = (date?: Date) => {
|
|
85
|
+
const formattedDate = date && formatDate(date, true);
|
|
86
|
+
|
|
87
|
+
return formattedDate ? `${RATES_URL['archive']}?date=${formattedDate}` : RATES_URL['archive'];
|
|
88
|
+
};*/
|