-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseFiatStore.tsx
61 lines (56 loc) · 1.79 KB
/
useFiatStore.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import produce from 'immer';
import { Reducer } from 'react';
import { CurrencyTicker } from '../../../config/config/currencies';
import { ID } from '../../chain/blocks/lib/blocks';
import { WithStatus } from '../../chain/blocks/store/useBlocksStore';
import { persistedSettings } from '../../widgets/containers/WidgetsLayout/store/useWidgetsLayoutStore';
import { getSpotPriceId, SpotPrice } from '../lib/fiat';
import { FiatAction } from './useFiatActions';
export interface FiatState {
currency: CurrencyTicker;
spotPrices: Record<ID, WithStatus<SpotPrice>>;
}
export const initialFiatState: FiatState = {
currency: persistedSettings.currency ?? 'USD',
spotPrices: {},
};
export const fiatReducer: Reducer<FiatState, FiatAction> = (state, action) => {
switch (action.type) {
case 'SET_CURRENCY': {
return produce(state, (draft) => {
draft.currency = action.payload.currency;
});
}
case 'LOAD_SPOT_PRICE': {
return produce(state, (draft) => {
const { level, currency, token } = action.payload;
const id = getSpotPriceId(token, currency, level);
draft.spotPrices[id] = {
...draft.spotPrices[id],
status: 'LOADING',
};
});
}
case 'LOAD_SPOT_PRICE_SUCCESS': {
return produce(state, (draft) => {
const { spotPrice } = action.payload;
const id = spotPrice.id;
draft.spotPrices[id] = {
status: 'SUCCESS',
data: spotPrice,
};
});
}
case 'LOAD_SPOT_PRICE_ERROR': {
return produce(state, (draft) => {
const { level, currency, token } = action.payload;
const id = getSpotPriceId(token, currency, level);
draft.spotPrices[id] = {
status: 'ERROR',
};
});
}
default:
return state;
}
};