nitro-react/src/views/catalog/CatalogView.tsx

78 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-05-05 09:14:54 +02:00
import { CatalogModeComposer, ICatalogPageData } from 'nitro-renderer';
import { FC, useCallback, useEffect, useReducer, useState } from 'react';
2021-04-22 05:26:30 +02:00
import { CatalogEvent } from '../../events';
import { useUiEvent } from '../../hooks/events/ui/ui-event';
2021-05-05 09:14:54 +02:00
import { SendMessageHook } from '../../hooks/messages/message-event';
import { NitroCardContentView, NitroCardHeaderView, NitroCardTabsItemView, NitroCardTabsView, NitroCardView } from '../../layout';
2021-04-22 05:26:30 +02:00
import { LocalizeText } from '../../utils/LocalizeText';
import { CatalogMessageHandler } from './CatalogMessageHandler';
2021-05-05 09:14:54 +02:00
import { CatalogMode, CatalogViewProps } from './CatalogView.types';
import { CatalogContextProvider } from './context/CatalogContext';
import { CatalogReducer, initialCatalog } from './reducers/CatalogReducer';
import { CatalogNavigationView } from './views/navigation/CatalogNavigationView';
import { CatalogPageView } from './views/page/CatalogPageView';
2021-04-22 05:26:30 +02:00
export const CatalogView: FC<CatalogViewProps> = props =>
{
const [ isVisible, setIsVisible ] = useState(false);
2021-05-05 09:14:54 +02:00
const [ currentTab, setCurrentTab ] = useState<ICatalogPageData>(null);
const [ catalogState, dispatchCatalogState ] = useReducer(CatalogReducer, initialCatalog);
const { root = null } = catalogState;
2021-04-22 05:26:30 +02:00
const onCatalogEvent = useCallback((event: CatalogEvent) =>
{
switch(event.type)
{
case CatalogEvent.SHOW_CATALOG:
setIsVisible(true);
return;
case CatalogEvent.TOGGLE_CATALOG:
setIsVisible(value => !value);
return;
}
}, []);
useUiEvent(CatalogEvent.SHOW_CATALOG, onCatalogEvent);
useUiEvent(CatalogEvent.TOGGLE_CATALOG, onCatalogEvent);
2021-05-05 09:14:54 +02:00
useEffect(() =>
2021-04-22 05:26:30 +02:00
{
2021-05-05 09:14:54 +02:00
if(!isVisible) return;
if(!catalogState.root)
{
SendMessageHook(new CatalogModeComposer(CatalogMode.MODE_NORMAL));
}
else
{
setCurrentTab(catalogState.root.children[0]);
}
}, [ isVisible, catalogState.root ]);
2021-04-22 05:26:30 +02:00
return (
2021-05-05 09:14:54 +02:00
<CatalogContextProvider value={ { catalogState, dispatchCatalogState } }>
2021-04-22 05:26:30 +02:00
<CatalogMessageHandler />
2021-05-05 09:14:54 +02:00
{ isVisible &&
<NitroCardView className="nitro-catalog">
<NitroCardHeaderView headerText={ LocalizeText('catalog.title') } onCloseClick={ event => setIsVisible(false) } />
<NitroCardTabsView>
{ root && root.children.length && root.children.map((page, index) =>
{
return <NitroCardTabsItemView key={ index } tabText={ page.localization } isActive={ (currentTab === page) } onClick={ event => setCurrentTab(page) } />
}) }
</NitroCardTabsView>
<NitroCardContentView>
<div className="row">
<div className="col-3">
<CatalogNavigationView page={ currentTab } />
</div>
<div className="col">
<CatalogPageView />
</div>
</div>
</NitroCardContentView>
</NitroCardView> }
</CatalogContextProvider>
2021-04-22 05:26:30 +02:00
);
}