nitro-react/src/views/purse/PurseView.tsx

53 lines
2.0 KiB
TypeScript
Raw Normal View History

2021-05-07 09:50:20 +02:00
import { UserCurrencyComposer } from 'nitro-renderer';
2021-06-22 11:52:47 +02:00
import { FC, useCallback, useEffect, useMemo, useReducer } from 'react';
2021-06-23 10:05:23 +02:00
import { GetConfiguration } from '../../api';
2021-06-22 11:52:47 +02:00
import { NotificationCenterEvent } from '../../events';
import { dispatchUiEvent } from '../../hooks/events';
2021-05-07 09:50:20 +02:00
import { SendMessageHook } from '../../hooks/messages/message-event';
2021-06-23 06:03:56 +02:00
import { SetLastCurrencies } from './common/CurrencyHelper';
2021-05-07 09:50:20 +02:00
import { PurseContextProvider } from './context/PurseContext';
2021-04-15 19:29:48 +02:00
import { CurrencyView } from './currency/CurrencyView';
2021-05-07 09:50:20 +02:00
import { PurseMessageHandler } from './PurseMessageHandler';
2021-04-15 19:29:48 +02:00
import { PurseViewProps } from './PurseView.types';
2021-05-07 09:50:20 +02:00
import { initialPurse, PurseReducer } from './reducers/PurseReducer';
2021-04-15 19:29:48 +02:00
2021-05-07 09:50:20 +02:00
export const PurseView: FC<PurseViewProps> = props =>
2021-04-15 19:29:48 +02:00
{
2021-05-07 09:50:20 +02:00
const [ purseState, dispatchPurseState ] = useReducer(PurseReducer, initialPurse);
const { currencies = [] } = purseState;
const displayedCurrencies = useMemo(() =>
2021-04-15 19:29:48 +02:00
{
2021-05-07 09:50:20 +02:00
return GetConfiguration<number[]>('system.currency.types', []);
2021-04-21 05:36:39 +02:00
}, []);
2021-04-15 19:29:48 +02:00
useEffect(() =>
{
SendMessageHook(new UserCurrencyComposer());
}, []);
2021-05-10 19:11:16 +02:00
SetLastCurrencies(currencies);
2021-06-22 11:52:47 +02:00
const toggleNotificationCenter = useCallback(() =>
{
dispatchUiEvent(new NotificationCenterEvent(NotificationCenterEvent.TOGGLE_NOTIFICATION_CENTER));
}, []);
2021-04-15 19:29:48 +02:00
return (
2021-05-07 09:50:20 +02:00
<PurseContextProvider value={ { purseState, dispatchPurseState }}>
<PurseMessageHandler />
2021-06-22 09:08:16 +02:00
<div className="nitro-purse rounded d-flex flex-row py-1 justify-content-between">
2021-05-07 09:50:20 +02:00
{ currencies && currencies.map((currency, index) =>
2021-04-21 05:36:39 +02:00
{
2021-05-07 09:50:20 +02:00
if(displayedCurrencies.indexOf(currency.type) === -1) return null;
2021-04-21 05:36:39 +02:00
2021-05-12 10:34:40 +02:00
return <CurrencyView key={ index } currency={ currency } />;
2021-04-21 05:36:39 +02:00
}) }
2021-06-22 11:52:47 +02:00
<div className="notification-button px-2" onClick={ toggleNotificationCenter }>
<i className="fas fa-bars" />
</div>
2021-04-15 19:29:48 +02:00
</div>
2021-05-07 09:50:20 +02:00
</PurseContextProvider>
2021-04-15 19:29:48 +02:00
);
}