import { OrderDTO } from '@energyweb/exchange-irec-react-query-client' ; import { FormSelectOption , TableActionData } from '@energyweb/origin-ui-core' ; import { useAllDeviceFuelTypes , useApiCancelOrderHandler , } from '@energyweb/origin-ui-exchange-data' ; import { useMyOrdersBidsTableLogic } from '@energyweb/origin-ui-exchange-logic' ; import { Remove , Search , Visibility } from '@mui/icons-material' ; import dayjs , { Dayjs } from 'dayjs' ; import React from 'react' ; import { useTranslation } from 'react-i18next' ; import { useNavigate } from 'react-router-dom' ; import { ExchangeModalsActionsEnum , useExchangeModalsDispatch , } from '../../../context' ; import { EndDateFilter , FuelTypeFilter , StartDateFilter , } from '../../table-filters' ; import { BidsTableProps } from './BidsTable' ; export const useBidsTableEffects = ( { bids , isLoading } ) => { const { allTypes : allFuelTypes , isLoading : areFuelTypesLoading } = useAllDeviceFuelTypes ( ) ; const { t } = useTranslation ( ) ; const navigate = useNavigate ( ) ; const dispatchModals = useExchangeModalsDispatch ( ) ; const bidText = t ( 'exchange.myOrders.bid' ) ; const { removeHandler , isMutating } = useApiCancelOrderHandler ( bidText ) ; const openRemoveModal = ( id ) => { dispatchModals ( { type : ExchangeModalsActionsEnum . SHOW_REMOVE_ORDER_CONFIRM , payload : { open : true , id , removeHandler , } , } ) ; } ; const openDetailsModal = ( id ) => { const orderToShow = bids ?. find ( ( bid ) => bid . id === id ) ; dispatchModals ( { type : ExchangeModalsActionsEnum . SHOW_ORDER_DETAILS , payload : { open : true , order : orderToShow , } , } ) ; } ; const viewMarket = ( id ) => { const bid = bids . find ( ( bid ) => bid . id === id ) ; navigate ( '/exchange/view-market' , { state : { deviceType : bid . product . deviceType , location : bid . product . location , gridOperator : bid . product . gridOperator , generationFrom : bid . product . generationFrom , generationTo : bid . product . generationTo , } , } ) ; } ; const actions = [ { name : t ( 'exchange.myOrders.remove' ) , icon : < Remove />, onClick : ( id ) => openRemoveModal ( id ) , loading : isMutating , } , { name : t ( 'exchange.myOrders.view' ) , icon : < Visibility />, onClick : ( id ) => openDetailsModal ( id ) , } , { icon : < Search />, name : t ( 'exchange.myOrders.viewMarket' ) , onClick : ( id ) => viewMarket ( id ) , } , ] ; const tableFilters = [ { name : 'generationStart' , filterFunc : ( cellValue , filterValue ) => { return ( dayjs ( cellValue ) . isSame ( filterValue , 'day' ) || dayjs ( cellValue ) . isAfter ( filterValue ) ) ; } , component : StartDateFilter , } , { name : 'fuelType' , filterFunc : ( cellValue , filterValue ) => { if ( ! filterValue || filterValue . length < 1 ) return true ; const cellTypesArr = cellValue . split ( ', ' ) ; const filterTypesArr = filterValue . map ( ( filter ) => filter . label ) ; return cellTypesArr . some ( ( cellType ) => filterTypesArr . includes ( cellType ) ) ; } , component : FuelTypeFilter , } , { name : 'generationEnd' , filterFunc : ( cellValue , filterValue ) => { return ( dayjs ( cellValue ) . isSame ( filterValue , 'day' ) || dayjs ( cellValue ) . isBefore ( filterValue ) ) ; } , component : EndDateFilter , } , ] ; const tableData = useMyOrdersBidsTableLogic ( { bids , isLoading : isLoading || areFuelTypesLoading , allFuelTypes , actions , tableFilters , openDetailsModal , } ) ; return tableData ; } ;