import { OrganizationStatus , UserStatus , } from '@energyweb/origin-backend-react-query-client' ; import { usePaginateData } from '@energyweb/origin-ui-core' ; import { BundleSplit , useBuyBundleHandler , useCachedAllDevices , useCachedAllFuelTypes , useCachedUser , } from '@energyweb/origin-ui-exchange-data' ; import { BigNumber } from '@ethersproject/bignumber' ; import { useState } from 'react' ; import { ExchangeModalsActionsEnum , useExchangeModalsDispatch , } from '../../../context' ; const PAGE_SIZE = 5 ; export const useSplitContentsEffects = ( splits ) => { const { paginatedData , setActivePage , activePage } = usePaginateData ( splits , PAGE_SIZE ) ; const isFirstPage = activePage === 0 ; const totalPages = Math . ceil ( splits . length / PAGE_SIZE ) ; const isLastPage = activePage === totalPages - 1 ; const [ selectedVolume , setSelectedVolume ] = useState < BigNumber > ( BigNumber . from ( 0 ) ) ; const handleSelect = ( volume ) => { if ( volume . toNumber ( ) === selectedVolume . toNumber ( ) ) { return setSelectedVolume ( BigNumber . from ( 0 ) ) ; } setSelectedVolume ( volume ) ; } ; const handleNextPage = ( ) => { if ( isLastPage ) { return ; } setActivePage ( activePage + 1 ) ; } ; const handlePreviousPage = ( ) => { if ( isFirstPage ) { return ; } setActivePage ( activePage - 1 ) ; } ; const dispatchModals = useExchangeModalsDispatch ( ) ; const closeModal = ( ) => { dispatchModals ( { type : ExchangeModalsActionsEnum . SHOW_BUNDLE_DETAILS , payload : { open : false , bundle : null , isOwner : false , } , } ) ; } ; const buyHandler = useBuyBundleHandler ( selectedVolume , closeModal ) ; const allDevices = useCachedAllDevices ( ) ; const allFuelTypes = useCachedAllFuelTypes ( ) ; const user = useCachedUser ( ) ; const disableBuyButton = user ?. status !== UserStatus . Active || user ?. organization ?. status !== OrganizationStatus . Active ; return { paginatedData , allDevices , allFuelTypes , isFirstPage , isLastPage , selectedVolume , handleNextPage , handlePreviousPage , handleSelect , buyHandler , disableBuyButton , } ; } ;