Update inventory context

This commit is contained in:
Bill 2021-04-23 21:03:34 -04:00
parent 1378417904
commit 34d0250f17
11 changed files with 83 additions and 50 deletions

View File

@ -1,28 +1,22 @@
import { RoomSessionEvent } from 'nitro-renderer'; import { RoomSessionEvent } from 'nitro-renderer';
import { createContext, FC, useCallback, useEffect, useState } from 'react'; import { FC, useCallback, useState } from 'react';
import { InventoryEvent } from '../../events'; import { InventoryEvent } from '../../events';
import { DraggableWindow } from '../../hooks/draggable-window/DraggableWindow'; import { DraggableWindow } from '../../hooks/draggable-window/DraggableWindow';
import { useRoomSessionManagerEvent } from '../../hooks/events/nitro/session/room-session-manager-event'; import { useRoomSessionManagerEvent } from '../../hooks/events/nitro/session/room-session-manager-event';
import { useUiEvent } from '../../hooks/events/ui/ui-event'; import { useUiEvent } from '../../hooks/events/ui/ui-event';
import { LocalizeText } from '../../utils/LocalizeText'; import { LocalizeText } from '../../utils/LocalizeText';
import { InventoryContextProvider } from './context/InventoryContext';
import { InventoryMessageHandler } from './InventoryMessageHandler'; import { InventoryMessageHandler } from './InventoryMessageHandler';
import { IInventoryContext, InventoryTabs, InventoryViewProps } from './InventoryView.types'; import { InventoryTabs, InventoryViewProps } from './InventoryView.types';
import { InventoryTabsContentView } from './tabs-content/InventoryTabsContentView'; import { InventoryTabsContentView } from './tabs-content/InventoryTabsContentView';
import { InventoryTabsSelectorView } from './tabs-selector/InventoryTabsSelectorView'; import { InventoryTabsSelectorView } from './tabs-selector/InventoryTabsSelectorView';
export const InventoryContext = createContext<IInventoryContext>(null);
export const InventoryView: FC<InventoryViewProps> = props => export const InventoryView: FC<InventoryViewProps> = props =>
{ {
const [ isVisible, setIsVisible ] = useState(false); const tabs = [ InventoryTabs.FURNITURE, InventoryTabs.BOTS, InventoryTabs.PETS, InventoryTabs.BADGES ];
const [ currentTab, setCurrentTab ] = useState<string>(null);
const [ tabs, setTabs ] = useState<string[]>([
InventoryTabs.FURNITURE, InventoryTabs.BOTS, InventoryTabs.PETS, InventoryTabs.BADGES
]);
useEffect(() => { const [ isVisible, setIsVisible ] = useState(false);
setCurrentTab(tabs[0]); const [ currentTab, setCurrentTab ] = useState<string>(tabs[0]);
}, [ tabs ]);
const onInventoryEvent = useCallback((event: InventoryEvent) => const onInventoryEvent = useCallback((event: InventoryEvent) =>
{ {
@ -62,7 +56,7 @@ export const InventoryView: FC<InventoryViewProps> = props =>
} }
return ( return (
<InventoryContext.Provider value={{ currentTab: currentTab, onSetCurrentTab: setCurrentTab }}> <InventoryContextProvider value={{ currentTab, setCurrentTab }}>
<InventoryMessageHandler /> <InventoryMessageHandler />
{ isVisible && <DraggableWindow handle=".drag-handler"> { isVisible && <DraggableWindow handle=".drag-handler">
<div className="nitro-inventory d-flex flex-column bg-primary border border-black shadow rounded"> <div className="nitro-inventory d-flex flex-column bg-primary border border-black shadow rounded">
@ -76,6 +70,6 @@ export const InventoryView: FC<InventoryViewProps> = props =>
<InventoryTabsContentView /> <InventoryTabsContentView />
</div> </div>
</DraggableWindow> } </DraggableWindow> }
</InventoryContext.Provider> </InventoryContextProvider>
); );
} }

View File

@ -1,12 +1,6 @@
export interface InventoryViewProps export interface InventoryViewProps
{} {}
export interface IInventoryContext
{
currentTab: string;
onSetCurrentTab: (tab: string) => void;
}
export class InventoryTabs export class InventoryTabs
{ {
public static readonly FURNITURE: string = 'inventory.furni'; public static readonly FURNITURE: string = 'inventory.furni';

View File

@ -0,0 +1,14 @@
import { createContext, FC, useContext } from 'react';
import { IInventoryContext, InventoryContextProps } from './InventoryContext.types';
const InventoryContext = createContext<IInventoryContext>({
currentTab: null,
setCurrentTab: null
});
export const InventoryContextProvider: FC<InventoryContextProps> = props =>
{
return <InventoryContext.Provider value={ props.value }>{ props.children }</InventoryContext.Provider>
}
export const useInventoryContext = () => useContext(InventoryContext);

View File

@ -0,0 +1,12 @@
import { ProviderProps } from 'react';
export interface IInventoryContext
{
currentTab: string;
setCurrentTab: (tab: string) => void;
}
export interface InventoryContextProps extends ProviderProps<IInventoryContext>
{
}

View File

@ -1,5 +1,5 @@
import { FC, useContext } from 'react'; import { FC } from 'react';
import { InventoryContext } from '../InventoryView'; import { useInventoryContext } from '../context/InventoryContext';
import { InventoryTabs } from '../InventoryView.types'; import { InventoryTabs } from '../InventoryView.types';
import { InventoryTabBadgesView } from './badges/InventoryTabBadgesView'; import { InventoryTabBadgesView } from './badges/InventoryTabBadgesView';
import { InventoryTabBotsView } from './bots/InventoryTabBotsView'; import { InventoryTabBotsView } from './bots/InventoryTabBotsView';
@ -9,14 +9,28 @@ import { InventoryTabPetsView } from './pets/InventoryTabPetsView';
export const InventoryTabsContentView: FC<InventoryTabsContentViewProps> = props => export const InventoryTabsContentView: FC<InventoryTabsContentViewProps> = props =>
{ {
const inventoryContext = useContext(InventoryContext); const inventoryContext = useInventoryContext();
function renderCurrentTab(): JSX.Element
{
switch(inventoryContext.currentTab)
{
case InventoryTabs.FURNITURE:
return <InventoryTabFurnitureView />
case InventoryTabs.BOTS:
return <InventoryTabBotsView />
case InventoryTabs.PETS:
return <InventoryTabPetsView />
case InventoryTabs.BADGES:
return <InventoryTabBadgesView />
}
return null;
}
return ( return (
<div className="px-3 pb-3"> <div className="px-3 pb-3">
{ inventoryContext && inventoryContext.currentTab && inventoryContext.currentTab === InventoryTabs.FURNITURE && <InventoryTabFurnitureView /> } { inventoryContext && inventoryContext.currentTab && renderCurrentTab() }
{ inventoryContext && inventoryContext.currentTab && inventoryContext.currentTab === InventoryTabs.BOTS && <InventoryTabBotsView /> }
{ inventoryContext && inventoryContext.currentTab && inventoryContext.currentTab === InventoryTabs.PETS && <InventoryTabPetsView /> }
{ inventoryContext && inventoryContext.currentTab && inventoryContext.currentTab === InventoryTabs.BADGES && <InventoryTabBadgesView /> }
</div> </div>
); );
} }

View File

@ -1,10 +1,10 @@
import { FC, useContext } from 'react'; import { FC } from 'react';
import { InventoryContext } from '../../InventoryView'; import { useInventoryContext } from '../../context/InventoryContext';
import { InventoryTabBadgesViewProps } from './InventoryTabBadgesView.types'; import { InventoryTabBadgesViewProps } from './InventoryTabBadgesView.types';
export const InventoryTabBadgesView: FC<InventoryTabBadgesViewProps> = props => export const InventoryTabBadgesView: FC<InventoryTabBadgesViewProps> = props =>
{ {
const inventoryContext = useContext(InventoryContext); const inventoryContext = useInventoryContext();
return ( return (
<>Badges content</> <>Badges content</>

View File

@ -1,10 +1,10 @@
import { FC, useContext } from 'react'; import { FC } from 'react';
import { InventoryContext } from '../../InventoryView'; import { useInventoryContext } from '../../context/InventoryContext';
import { InventoryTabBotsViewProps } from './InventoryTabBotsView.types'; import { InventoryTabBotsViewProps } from './InventoryTabBotsView.types';
export const InventoryTabBotsView: FC<InventoryTabBotsViewProps> = props => export const InventoryTabBotsView: FC<InventoryTabBotsViewProps> = props =>
{ {
const inventoryContext = useContext(InventoryContext); const inventoryContext = useInventoryContext();
return ( return (
<>Bots content</> <>Bots content</>

View File

@ -1,10 +1,10 @@
import { FC, useContext } from 'react'; import { FC } from 'react';
import { InventoryContext } from '../../InventoryView'; import { useInventoryContext } from '../../context/InventoryContext';
import { InventoryTabFurnitureViewProps } from './InventoryTabFurnitureView.types'; import { InventoryTabFurnitureViewProps } from './InventoryTabFurnitureView.types';
export const InventoryTabFurnitureView: FC<InventoryTabFurnitureViewProps> = props => export const InventoryTabFurnitureView: FC<InventoryTabFurnitureViewProps> = props =>
{ {
const inventoryContext = useContext(InventoryContext); const inventoryContext = useInventoryContext();
return ( return (
<>Furniture content</> <>Furniture content</>

View File

@ -1,10 +1,10 @@
import { FC, useContext } from 'react'; import { FC } from 'react';
import { InventoryContext } from '../../InventoryView'; import { useInventoryContext } from '../../context/InventoryContext';
import { InventoryTabPetsViewProps } from './InventoryTabPetsView.types'; import { InventoryTabPetsViewProps } from './InventoryTabPetsView.types';
export const InventoryTabPetsView: FC<InventoryTabPetsViewProps> = props => export const InventoryTabPetsView: FC<InventoryTabPetsViewProps> = props =>
{ {
const inventoryContext = useContext(InventoryContext); const inventoryContext = useInventoryContext();
return ( return (
<>Pets content</> <>Pets content</>

View File

@ -4,9 +4,11 @@ import { InventoryTabView } from './tab/InventoryTabView';
export const InventoryTabsSelectorView: FC<InventoryTabsSelectorViewProps> = props => export const InventoryTabsSelectorView: FC<InventoryTabsSelectorViewProps> = props =>
{ {
const { tabs = null } = props;
return ( return (
<div className="p-3"> <div className="p-3">
{ props.tabs && { tabs && tabs.length &&
<div className="btn-group w-100"> <div className="btn-group w-100">
{ props.tabs.map((tab, index) => { props.tabs.map((tab, index) =>
{ {

View File

@ -1,18 +1,21 @@
import classNames from 'classnames'; import classNames from 'classnames';
import { FC, useContext } from 'react'; import { FC, MouseEvent } from 'react';
import { LocalizeText } from '../../../../utils/LocalizeText'; import { LocalizeText } from '../../../../utils/LocalizeText';
import { InventoryContext } from '../../InventoryView'; import { useInventoryContext } from '../../context/InventoryContext';
import { InventoryTabViewProps } from './InventoryTabView.types'; import { InventoryTabViewProps } from './InventoryTabView.types';
export const InventoryTabView: FC<InventoryTabViewProps> = props => export const InventoryTabView: FC<InventoryTabViewProps> = props =>
{ {
const inventoryContext = useContext(InventoryContext); const { tab = null } = props;
return ( const inventoryContext = useInventoryContext();
<button type="button"
className={ 'btn btn-secondary btn-sm ' + classNames({ 'active': inventoryContext.currentTab === props.tab })} function selectTab(event: MouseEvent = null): void
onClick={ () => inventoryContext.onSetCurrentTab(props.tab) }> {
{ LocalizeText(props.tab) } inventoryContext.setCurrentTab(tab);
</button> }
);
if(!tab) return null;
return <button type="button" className={ 'btn btn-secondary btn-sm ' + classNames({ 'active': (inventoryContext.currentTab === tab) })} onClick={ selectTab }>{ LocalizeText(tab) }</button>;
} }