diff --git a/src/api/catalog/ICatalogOptions.ts b/src/api/catalog/ICatalogOptions.ts index e50559c..d95b16a 100644 --- a/src/api/catalog/ICatalogOptions.ts +++ b/src/api/catalog/ICatalogOptions.ts @@ -1,7 +1,6 @@ -import { ClubGiftInfoParser, MarketplaceConfigurationMessageParser } from '@nitrots/nitro-renderer'; +import { ClubGiftInfoParser } from '@nitrots/nitro-renderer'; export interface ICatalogOptions { clubGifts?: ClubGiftInfoParser; - marketplaceConfiguration?: MarketplaceConfigurationMessageParser; } diff --git a/src/components/catalog/views/page/layout/marketplace/MarketplacePostOfferView.tsx b/src/components/catalog/views/page/layout/marketplace/MarketplacePostOfferView.tsx index af1e147..4c147cb 100644 --- a/src/components/catalog/views/page/layout/marketplace/MarketplacePostOfferView.tsx +++ b/src/components/catalog/views/page/layout/marketplace/MarketplacePostOfferView.tsx @@ -1,9 +1,9 @@ -import { GetMarketplaceConfigurationMessageComposer, MakeOfferMessageComposer, MarketplaceConfigurationEvent } from '@nitrots/nitro-renderer'; +import { MakeOfferMessageComposer } from '@nitrots/nitro-renderer'; import { FC, useEffect, useState } from 'react'; import { FurnitureItem, LocalizeText, ProductTypeEnum, SendMessageComposer } from '../../../../../../api'; import { Button, Column, Grid, LayoutFurniImageView, NitroCardContentView, NitroCardHeaderView, NitroCardView, Text } from '../../../../../../common'; import { CatalogPostMarketplaceOfferEvent } from '../../../../../../events'; -import { useCatalog, useMessageEvent, useNotification, useUiEvent } from '../../../../../../hooks'; +import { useMarketplaceConfiguration, useNotification, useUiEvent } from '../../../../../../hooks'; import { NitroInput } from '../../../../../../layout'; let isPostingMarketplaceOffer = false; @@ -13,8 +13,7 @@ export const MarketplacePostOfferView: FC<{}> = props => const [ item, setItem ] = useState(null); const [ askingPrice, setAskingPrice ] = useState(0); const [ tempAskingPrice, setTempAskingPrice ] = useState('0'); - const { catalogOptions = null, setCatalogOptions = null } = useCatalog(); - const { marketplaceConfiguration = null } = catalogOptions; + const { data: marketplaceConfiguration = null } = useMarketplaceConfiguration({ enabled: !!item }); const { showConfirm = null } = useNotification(); const updateAskingPrice = (price: string) => @@ -28,29 +27,8 @@ export const MarketplacePostOfferView: FC<{}> = props => setAskingPrice(parseInt(price)); }; - useMessageEvent(MarketplaceConfigurationEvent, event => - { - const parser = event.getParser(); - - setCatalogOptions(prevValue => - { - const newValue = { ...prevValue }; - - newValue.marketplaceConfiguration = parser; - - return newValue; - }); - }); - useUiEvent(CatalogPostMarketplaceOfferEvent.POST_MARKETPLACE, event => setItem(event.item)); - useEffect(() => - { - if(!item || marketplaceConfiguration) return; - - SendMessageComposer(new GetMarketplaceConfigurationMessageComposer()); - }, [ item, marketplaceConfiguration ]); - useEffect(() => { if(!item) return; diff --git a/src/hooks/catalog/index.ts b/src/hooks/catalog/index.ts index 2995cd8..b706172 100644 --- a/src/hooks/catalog/index.ts +++ b/src/hooks/catalog/index.ts @@ -4,4 +4,5 @@ export * from './useCatalogPlaceMultipleItems'; export * from './useCatalogSkipPurchaseConfirmation'; export * from './useClubOffers'; export * from './useGiftConfiguration'; +export * from './useMarketplaceConfiguration'; export * from './useSellablePetPalette'; diff --git a/src/hooks/catalog/useMarketplaceConfiguration.ts b/src/hooks/catalog/useMarketplaceConfiguration.ts new file mode 100644 index 0000000..ac4d97e --- /dev/null +++ b/src/hooks/catalog/useMarketplaceConfiguration.ts @@ -0,0 +1,28 @@ +import { GetMarketplaceConfigurationMessageComposer, MarketplaceConfigurationEvent, MarketplaceConfigurationMessageParser } from '@nitrots/nitro-renderer'; +import { UseQueryResult } from '@tanstack/react-query'; +import { useNitroQuery } from '../../api/nitro-query'; + +/** + * Marketplace configuration (commission rates, min/max ask, etc.) as + * returned by GetMarketplaceConfigurationMessageComposer → + * MarketplaceConfigurationEvent. Cached at session level — the values + * are server-side constants for the duration of a session. + * + * Replaces the previous pattern where MarketplacePostOfferView + * stuffed the parser into catalogOptions.marketplaceConfiguration + * via setCatalogOptions inside its own listener, and dispatched + * GetMarketplaceConfigurationMessageComposer from an effect that + * checked the same field as the cache. With useNitroQuery, the cache + * is React Query's; the component just reads `data`. + */ +export const useMarketplaceConfiguration = ( + options: { enabled?: boolean } = {} +): UseQueryResult => + useNitroQuery({ + key: [ 'nitro', 'catalog', 'marketplaceConfiguration' ], + request: () => new GetMarketplaceConfigurationMessageComposer(), + parser: MarketplaceConfigurationEvent, + select: event => event.getParser(), + enabled: options.enabled, + staleTime: Infinity + });