import { GetSessionDataManager, GiftReceiverNotFoundEvent, PurchaseFromCatalogAsGiftComposer } from '@nitrots/nitro-renderer'; import { ChangeEvent, FC, useCallback, useEffect, useMemo, useState } from 'react'; import { FaChevronLeft, FaChevronRight } from 'react-icons/fa'; import { ColorUtils, 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 { useCatalog, useFriends, useMessageEvent, 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 [ colors, setColors ] = useState<{ id: number, color: string }[]>([]); const [ selectedBoxIndex, setSelectedBoxIndex ] = useState(0); const [ selectedRibbonIndex, setSelectedRibbonIndex ] = useState(0); const [ selectedColorId, setSelectedColorId ] = useState(0); const [ maxBoxIndex, setMaxBoxIndex ] = useState(0); const [ maxRibbonIndex, setMaxRibbonIndex ] = useState(0); const [ receiverNotFound, setReceiverNotFound ] = useState(false); const { catalogOptions = null } = useCatalog(); const { friends } = useFriends(); const { giftConfiguration = null } = catalogOptions; const [ boxTypes, setBoxTypes ] = useState([]); const [ suggestions, setSuggestions ] = useState([]); const [ isAutocompleteVisible, setIsAutocompleteVisible ] = useState(true); 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) return ''; return ((boxTypes[selectedBoxIndex] * 1000) + giftConfiguration.ribbonTypes[selectedRibbonIndex]).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); setIsVisible(true); return; } }); useEffect(() => { setReceiverNotFound(false); }, [ receiverName ]); const createBoxTypes = useCallback(() => { if(!giftConfiguration) return; setBoxTypes(prev => { let newPrev = [ ...giftConfiguration.boxTypes ]; newPrev.push(giftConfiguration.defaultStuffTypes[Math.floor((Math.random() * (giftConfiguration.defaultStuffTypes.length - 1)))]); setMaxBoxIndex(newPrev.length - 1); setMaxRibbonIndex(newPrev.length - 1); return newPrev; }); }, [ giftConfiguration ]); useEffect(() => { if(!giftConfiguration) return; const newColors: { 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) newColors.push({ id: colorId, color: ColorUtils.makeColorNumberHex(giftData.colors[0]) }); } createBoxTypes(); if(newColors.length) { setSelectedColorId(newColors[0].id); setColors(newColors); } }, [ giftConfiguration, createBoxTypes ]); useEffect(() => { if(!isVisible) return; createBoxTypes(); }, [ createBoxTypes, isVisible ]); if(!giftConfiguration || !giftConfiguration.isEnabled || !isVisible) 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) } />
{ selectedColorId &&
}
{ LocalizeText(boxName) }
{ LocalizeText(priceText, [ 'price' ], [ giftConfiguration.price.toString() ]) }
{ LocalizeText(ribbonName) }
{ LocalizeText('catalog.gift_wrapping.pick_color') }
{ colors.map(color =>
); };