import { GetGiftWrappingConfigurationComposer, GetSessionDataManager, GiftReceiverNotFoundEvent, GiftWrappingConfigurationEvent, PurchaseFromCatalogAsGiftComposer } from '@nitrots/nitro-renderer'; import { ChangeEvent, FC, useCallback, useEffect, useMemo, useState } from 'react'; import { FaChevronLeft, FaChevronRight } from 'react-icons/fa'; import { ColorUtils, GiftWrappingConfiguration, LocalizeText, MessengerFriend, ProductTypeEnum, SendMessageComposer } from '../../../../api'; import { Button, Column, Flex, FormGroup, LayoutCurrencyIcon, LayoutFurniImageView, LayoutGiftTagView, NitroCardContentView, NitroCardHeaderView, NitroCardView, Text } from '../../../../common'; import { CatalogEvent, CatalogInitGiftEvent, CatalogPurchasedEvent } from '../../../../events'; import { useFriends, useMessageEvent, useMessageEventState, useUiEvent } from '../../../../hooks'; import { classNames } from '../../../../layout'; let isBuyingGift = false; export const CatalogGiftView: FC<{}> = props => { const [ isVisible, setIsVisible ] = useState(false); const [ pageId, setPageId ] = useState(0); const [ offerId, setOfferId ] = useState(0); const [ extraData, setExtraData ] = useState(''); const [ receiverName, setReceiverName ] = useState(''); const [ showMyFace, setShowMyFace ] = useState(true); const [ message, setMessage ] = useState(''); const [ selectedBoxIndex, setSelectedBoxIndex ] = useState(0); const [ selectedRibbonIndex, setSelectedRibbonIndex ] = useState(0); const [ selectedColorId, setSelectedColorId ] = useState(0); const [ receiverNotFound, setReceiverNotFound ] = useState(false); const { friends } = useFriends(); const giftConfiguration = useMessageEventState( GiftWrappingConfigurationEvent, event => new GiftWrappingConfiguration(event.getParser()), null ); const [ suggestions, setSuggestions ] = useState([]); const [ isAutocompleteVisible, setIsAutocompleteVisible ] = useState(true); const boxTypes = useMemo(() => { if(!giftConfiguration) return []; const list = [ ...giftConfiguration.boxTypes ]; const defaults = giftConfiguration.defaultStuffTypes; if(defaults && defaults.length) { const pickIndex = Math.floor(Math.random() * Math.max(defaults.length - 1, 1)); list.push(defaults[pickIndex]); } return list; }, [ giftConfiguration ]); const colors = useMemo<{ id: number, color: string }[]>(() => { if(!giftConfiguration) return []; const result: { id: number, color: string }[] = []; for(const colorId of giftConfiguration.stuffTypes) { const giftData = GetSessionDataManager().getFloorItemData(colorId); if(!giftData) continue; if(giftData.colors && giftData.colors.length > 0) result.push({ id: colorId, color: ColorUtils.makeColorNumberHex(giftData.colors[0]) }); } return result; }, [ giftConfiguration ]); const maxBoxIndex = Math.max(boxTypes.length - 1, 0); const maxRibbonIndex = Math.max(boxTypes.length - 1, 0); useEffect(() => { if(!colors.length) return; setSelectedColorId(prev => (prev || colors[0].id)); }, [ colors ]); const onClose = useCallback(() => { isBuyingGift = false; setIsVisible(false); setPageId(0); setOfferId(0); setExtraData(''); setReceiverName(''); setShowMyFace(true); setMessage(''); setSelectedBoxIndex(0); setSelectedRibbonIndex(0); setIsAutocompleteVisible(false); setSuggestions([]); if(colors.length) setSelectedColorId(colors[0].id); }, [ colors ]); const isBoxDefault = useMemo(() => { return giftConfiguration ? (giftConfiguration.defaultStuffTypes.findIndex(s => (s === boxTypes[selectedBoxIndex])) > -1) : false; }, [ boxTypes, giftConfiguration, selectedBoxIndex ]); const boxExtraData = useMemo(() => { if(!giftConfiguration || !boxTypes.length) return ''; const boxType = boxTypes[selectedBoxIndex]; const ribbonType = giftConfiguration.ribbonTypes[selectedRibbonIndex]; if(boxType === undefined || ribbonType === undefined) return ''; return ((boxType * 1000) + ribbonType).toString(); }, [ giftConfiguration, selectedBoxIndex, selectedRibbonIndex, boxTypes ]); const isColorable = useMemo(() => { if(!giftConfiguration) return false; if(isBoxDefault) return false; const boxType = boxTypes[selectedBoxIndex]; return (boxType === 8 || (boxType >= 3 && boxType <= 6)) ? false : true; }, [ giftConfiguration, selectedBoxIndex, isBoxDefault, boxTypes ]); const colourId = useMemo(() => { return isBoxDefault ? boxTypes[selectedBoxIndex] : selectedColorId; }, [ isBoxDefault, boxTypes, selectedBoxIndex, selectedColorId ]); const allFriends = friends.filter((friend: MessengerFriend) => friend.id !== -1); const onTextChanged = (e: ChangeEvent) => { const value = e.target.value; let suggestions = []; if(value.length > 0) { suggestions = allFriends.sort().filter((friend: MessengerFriend) => friend.name.includes(value)); } setReceiverName(value); setIsAutocompleteVisible(true); setSuggestions(suggestions); }; const selectedReceiverName = (friendName: string) => { setReceiverName(friendName); setIsAutocompleteVisible(false); }; const handleAction = useCallback((action: string) => { switch(action) { case 'prev_box': setSelectedBoxIndex(value => (value === 0 ? maxBoxIndex : value - 1)); return; case 'next_box': setSelectedBoxIndex(value => (value === maxBoxIndex ? 0 : value + 1)); return; case 'prev_ribbon': setSelectedRibbonIndex(value => (value === 0 ? maxRibbonIndex : value - 1)); return; case 'next_ribbon': setSelectedRibbonIndex(value => (value === maxRibbonIndex ? 0 : value + 1)); return; case 'buy': if(!receiverName || (receiverName.length === 0)) { setReceiverNotFound(true); return; } if(isBuyingGift) return; isBuyingGift = true; setTimeout(() => { isBuyingGift = false; }, 10000); SendMessageComposer(new PurchaseFromCatalogAsGiftComposer(pageId, offerId, extraData, receiverName, message, colourId, selectedBoxIndex, selectedRibbonIndex, showMyFace)); return; } }, [ colourId, extraData, maxBoxIndex, maxRibbonIndex, message, offerId, pageId, receiverName, selectedBoxIndex, selectedRibbonIndex, showMyFace ]); useMessageEvent(GiftReceiverNotFoundEvent, event => setReceiverNotFound(true)); useUiEvent([ CatalogPurchasedEvent.PURCHASE_SUCCESS, CatalogEvent.INIT_GIFT ], event => { switch(event.type) { case CatalogPurchasedEvent.PURCHASE_SUCCESS: isBuyingGift = false; onClose(); return; case CatalogEvent.INIT_GIFT: const castedEvent = (event as CatalogInitGiftEvent); onClose(); setPageId(castedEvent.pageId); setOfferId(castedEvent.offerId); setExtraData(castedEvent.extraData); setSelectedBoxIndex(0); setSelectedRibbonIndex(0); setIsVisible(true); if(!giftConfiguration) SendMessageComposer(new GetGiftWrappingConfigurationComposer()); return; } }); useEffect(() => { setReceiverNotFound(false); }, [ receiverName ]); if(!isVisible || !giftConfiguration || !giftConfiguration.isEnabled || !boxTypes.length) return null; const boxName = 'catalog.gift_wrapping_new.box.' + (isBoxDefault ? 'default' : boxTypes[selectedBoxIndex]); const ribbonName = `catalog.gift_wrapping_new.ribbon.${ selectedRibbonIndex }`; const priceText = 'catalog.gift_wrapping_new.' + (isBoxDefault ? 'freeprice' : 'price'); return ( { LocalizeText('catalog.gift_wrapping.receiver') } onTextChanged(e) } /> { (suggestions.length > 0 && isAutocompleteVisible) && { suggestions.map((friend: MessengerFriend) => (
selectedReceiverName(friend.name) }>{ friend.name }
)) }
} { receiverNotFound &&
{ LocalizeText('catalog.gift_wrapping.receiver_not_found.title') }
}
setMessage(value) } />
setShowMyFace(value => !value) } />
{ (colourId > 0 && boxExtraData) && }
{ LocalizeText(boxName) }
{ LocalizeText(priceText, [ 'price' ], [ giftConfiguration.price.toString() ]) }
{ LocalizeText(ribbonName) }
{ LocalizeText('catalog.gift_wrapping.pick_color') }
{ colors.map(color =>
); };