2021-05-07 09:50:20 +02:00
|
|
|
import { UserCurrencyComposer } from 'nitro-renderer';
|
|
|
|
import { FC, useEffect, useMemo, useReducer } from 'react';
|
|
|
|
import { SendMessageHook } from '../../hooks/messages/message-event';
|
2021-05-05 00:47:36 +02:00
|
|
|
import { GetConfiguration } from '../../utils/GetConfiguration';
|
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-05-10 19:11:16 +02:00
|
|
|
import { SetLastCurrencies } from './utils/CurrencyHelper';
|
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-04-15 19:29:48 +02:00
|
|
|
return (
|
2021-05-07 09:50:20 +02:00
|
|
|
<PurseContextProvider value={ { purseState, dispatchPurseState }}>
|
|
|
|
<PurseMessageHandler />
|
2021-05-12 04:37:47 +02:00
|
|
|
<div className="nitro-purse row row-cols-2 g-0">
|
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-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
|
|
|
);
|
|
|
|
}
|