From 945894db9d02088de224a0d9c2a589d6466969c2 Mon Sep 17 00:00:00 2001 From: Bill Date: Wed, 19 Jan 2022 02:22:30 -0500 Subject: [PATCH] More changes --- src/components/catalog/CatalogView.tsx | 113 ++++++++---------- .../catalog/common/RequestedPage.ts | 42 +++---- .../catalog/context/CatalogContext.tsx | 6 +- .../navigation/CatalogNavigationItemView.tsx | 4 +- 4 files changed, 76 insertions(+), 89 deletions(-) diff --git a/src/components/catalog/CatalogView.tsx b/src/components/catalog/CatalogView.tsx index 5449b53f..ad0a092f 100644 --- a/src/components/catalog/CatalogView.tsx +++ b/src/components/catalog/CatalogView.tsx @@ -26,6 +26,7 @@ import { MarketplacePostOfferView } from './views/page/layout/marketplace/Market import { CatalogTabsViews } from './views/tabs/CatalogTabsView'; const DUMMY_PAGE_ID_FOR_OFFER_SEARCH: number = -12345678; +const REQUESTED_PAGE = new RequestedPage(); export const CatalogView: FC<{}> = props => { @@ -43,11 +44,7 @@ export const CatalogView: FC<{}> = props => const [ purchasableOffer, setPurchasableOffer ] = useState(null); const [ currentTab, setCurrentTab ] = useState(null); const [ activeNodes, setActiveNodes ] = useState([]); - const [ lastActiveNodes, setLastActiveNodes ] = useState(null); const [ frontPageItems, setFrontPageItems ] = useState([]); - - - const [ requestedPage, setRequestedPage ] = useState(new RequestedPage()); const [ roomPreviewer, setRoomPreviewer ] = useState(null); const [ catalogState, dispatchCatalogState ] = useReducer(CatalogReducer, initialCatalog); @@ -64,18 +61,35 @@ export const CatalogView: FC<{}> = props => setPurchasableOffer(null); setCurrentTab(null); setActiveNodes([]); - setLastActiveNodes(null); setFrontPageItems([]); setIsInitialized(false); setIsVisible(true); }); }, []); - const getNodesByOfferId = useCallback((offerId: number) => + const getNodeById = useCallback((id: number, node: ICatalogNode = null) => + { + if(!node) node = rootNode; + + if(!node) return null; + + if((node.pageId === id) && (node !== rootNode)) return node; + + for(const child of node.children) + { + const n = (getNodeById(id, child) as ICatalogNode); + + if(n) return n; + } + + return null; + }, [ rootNode ]); + + const getNodesByOfferId = useCallback((offerId: number, flag: boolean = false) => { if(!currentOffers || !currentOffers.size) return null; - if(true) + if(flag) { const nodes: ICatalogNode[] = []; const offers = currentOffers.get(offerId); @@ -91,7 +105,7 @@ export const CatalogView: FC<{}> = props => return currentOffers.get(offerId); }, [ currentOffers ]); - const loadCatalogPage = useCallback((pageId: number, offerId: number, forceRefresh: boolean = false) => + const loadCatalogPage = useCallback((pageId: number, offerId: number) => { if(pageId < 0) return; @@ -99,8 +113,6 @@ export const CatalogView: FC<{}> = props => { setIsBusy(true); setPageId(pageId); - - if(forceRefresh) setForceRefresh(true); }); SendMessageHook(new GetCatalogPageComposer(pageId, offerId, currentType)); @@ -135,7 +147,7 @@ export const CatalogView: FC<{}> = props => }); }, [ currentPage, forceRefresh, selectOffer ]); - const selectCatalogNode = useCallback((targetNode: ICatalogNode) => + const activateNode = useCallback((targetNode: ICatalogNode) => { setActiveNodes(prevValue => { @@ -167,7 +179,7 @@ export const CatalogView: FC<{}> = props => return newNodes; }); - if(targetNode.pageId > -1) loadCatalogPage(targetNode.pageId, -1, true); + if(targetNode.pageId > -1) loadCatalogPage(targetNode.pageId, -1); }, [ setActiveNodes, loadCatalogPage ]); const onCatalogEvent = useCallback((event: CatalogEvent) => @@ -215,13 +227,10 @@ export const CatalogView: FC<{}> = props => switch(parts[2]) { case 'offerId': - //setPendingPageLookup({ value: parts[3], isOffer: true }); - + return; } } - - //setPendingPageLookup({ value: parts[2], isOffer: false }); } else { @@ -270,42 +279,37 @@ export const CatalogView: FC<{}> = props => { if(!rootNode) return; - BatchUpdates(() => - { - setIsInitialized(true); + setIsInitialized(true); - if(rootNode.isBranch) - { - for(const child of rootNode.children) + switch(REQUESTED_PAGE.requestType) + { + case RequestedPage.REQUEST_TYPE_NONE: + BatchUpdates(() => { - if(child && child.isVisible) + setIsInitialized(true); + + if(rootNode.isBranch) { - setCurrentTab(child); + for(const child of rootNode.children) + { + if(child && child.isVisible) + { + setCurrentTab(child); - return; + return; + } + } } - } - } - }); - }, [ rootNode ]); - - useEffect(() => - { - if(!rootNode) return; - - switch(requestedPage.requestType) - { - // case RequestedPage.REQUEST_TYPE_NONE: - // loadFrontPage(); - // return; + }); + return; case RequestedPage.REQUEST_TYPE_ID: - requestedPage.resetRequest(); + REQUESTED_PAGE.resetRequest(); return; case RequestedPage.REQUEST_TYPE_NAME: - requestedPage.resetRequest(); + REQUESTED_PAGE.resetRequest(); return; } - }, [ rootNode, requestedPage ]); + }, [ rootNode ]); useEffect(() => { @@ -317,16 +321,16 @@ export const CatalogView: FC<{}> = props => { if(!child.isVisible) continue; - selectCatalogNode(child); + activateNode(child); return; } } else { - loadCatalogPage(currentTab.pageId, -1, true); + loadCatalogPage(currentTab.pageId, -1); } - }, [ currentTab, selectCatalogNode, loadCatalogPage ]); + }, [ currentTab, activateNode, loadCatalogPage ]); useEffect(() => { @@ -335,23 +339,6 @@ export const CatalogView: FC<{}> = props => setCurrentOffer(null); }, [ currentPage ]); - useEffect(() => - { - if(!isVisible && !lastActiveNodes && activeNodes && activeNodes.length) - { - setLastActiveNodes(activeNodes.concat()); - } - - else if(isVisible && lastActiveNodes) - { - BatchUpdates(() => - { - setActiveNodes(lastActiveNodes.concat()); - setLastActiveNodes(null); - }); - } - }, [ isVisible, lastActiveNodes, activeNodes ]); - UseMountEffect(() => { SendMessageHook(new GetMarketplaceConfigurationMessageComposer()); @@ -360,7 +347,7 @@ export const CatalogView: FC<{}> = props => }); return ( - + { isVisible && diff --git a/src/components/catalog/common/RequestedPage.ts b/src/components/catalog/common/RequestedPage.ts index 4925776a..c1458a7c 100644 --- a/src/components/catalog/common/RequestedPage.ts +++ b/src/components/catalog/common/RequestedPage.ts @@ -5,27 +5,15 @@ export class RequestedPage public static REQUEST_TYPE_NAME: number = 2; private _requestType: number; - private _requestId: number; + private _requestById: number; private _requestedOfferId: number; - private _requestName: string; + private _requestByName: string; constructor() { this._requestType = RequestedPage.REQUEST_TYPE_NONE; } - public setRequestById(id: number):void - { - this._requestType = RequestedPage.REQUEST_TYPE_ID; - this._requestId = id; - } - - public setRequestByName(name: string):void - { - this._requestType = RequestedPage.REQUEST_TYPE_NAME; - this._requestName = name; - } - public resetRequest():void { this._requestType = RequestedPage.REQUEST_TYPE_NONE; @@ -37,9 +25,26 @@ export class RequestedPage return this._requestType; } - public get requestId(): number + public get requestById(): number { - return this._requestId; + return this._requestById; + } + + public set requestById(id: number) + { + this._requestType = RequestedPage.REQUEST_TYPE_ID; + this._requestById = id; + } + + public get requestByName(): string + { + return this._requestByName; + } + + public set requestByName(name: string) + { + this._requestType = RequestedPage.REQUEST_TYPE_NAME; + this._requestByName = name; } public get requestedOfferId(): number @@ -51,9 +56,4 @@ export class RequestedPage { this._requestedOfferId = offerId; } - - public get requestName(): string - { - return this._requestName; - } } diff --git a/src/components/catalog/context/CatalogContext.tsx b/src/components/catalog/context/CatalogContext.tsx index bdabcae8..5f1d07fe 100644 --- a/src/components/catalog/context/CatalogContext.tsx +++ b/src/components/catalog/context/CatalogContext.tsx @@ -29,9 +29,9 @@ export interface ICatalogContext frontPageItems: FrontPageItem[]; setFrontPageItems: Dispatch>; resetState: () => void; - loadCatalogPage: (pageId: number, offerId: number, forceRefresh?: boolean) => void; + loadCatalogPage: (pageId: number, offerId: number) => void; showCatalogPage: (pageId: number, layoutCode: string, localization: IPageLocalization, offers: IPurchasableOffer[], offerId: number, acceptSeasonCurrencyAsCredits: boolean) => void; - selectCatalogNode: (targetNode: ICatalogNode) => void; + activateNode: (targetNode: ICatalogNode) => void; catalogState: ICatalogState; dispatchCatalogState: Dispatch; @@ -61,7 +61,7 @@ const CatalogContext = createContext({ resetState: null, loadCatalogPage: null, showCatalogPage: null, - selectCatalogNode: null, + activateNode: null, catalogState: null, dispatchCatalogState: null }); diff --git a/src/components/catalog/views/navigation/CatalogNavigationItemView.tsx b/src/components/catalog/views/navigation/CatalogNavigationItemView.tsx index cb83f720..1d40bc01 100644 --- a/src/components/catalog/views/navigation/CatalogNavigationItemView.tsx +++ b/src/components/catalog/views/navigation/CatalogNavigationItemView.tsx @@ -15,11 +15,11 @@ export interface CatalogNavigationItemViewProps export const CatalogNavigationItemView: FC = props => { const { node = null } = props; - const { selectCatalogNode = null } = useCatalogContext(); + const { activateNode = null } = useCatalogContext(); return ( <> - selectCatalogNode(node) }> + activateNode(node) }> { node.localization } { node.isBranch &&