11import { Col , Row , RowProps } from 'antd'
2- import React , { useCallback , useEffect , useState } from 'react'
2+ import React , { useCallback , useEffect , useMemo , useState } from 'react'
33import { useIntl } from 'react-intl'
44
5- import { split , createRecipientKey } from '@open-condo/billing/utils/paymentSplitter'
6- import type { Split , DistributionItem , SplitOptions } from '@open-condo/billing/utils/paymentSplitter'
5+ import type { DistributionItem , Split , SplitOptions } from '@open-condo/billing/utils/paymentSplitter'
6+ import { createRecipientKey , split } from '@open-condo/billing/utils/paymentSplitter'
77import { Plus , Trash } from '@open-condo/icons'
88import { Alert , Button , Card , Input , MarkdownCodeWrapper , Tabs , Typography } from '@open-condo/ui'
9+ import { useContainerSize } from '@open-condo/ui/hooks'
910
1011import { DistributionItemCard } from './DistributionItemCard'
12+ import styles from './index.module.css'
1113import { SplitResultCard } from './SplitResultCard'
1214
1315const CARD_PADDING = 8
1416const GUTTER_ROW : RowProps [ 'gutter' ] = [ 16 , 16 ]
17+ const MIN_RECIPIENT_CARD_WIDTH = 240
18+ const RECIPIENTS_CARDS_GAP = 16
1519
1620function generateDecimalString ( length : number ) : string {
1721 return Array . from ( { length } , ( ) => Math . floor ( Math . random ( ) * 10 ) ) . join ( '' ) // NOSONAR
@@ -38,6 +42,7 @@ export const AmountDistributionCalculator: React.FC = () => {
3842 const [ splitResult , setSplitResult ] = useState < Split [ ] > ( [ ] )
3943 const [ splitError , setSplitError ] = useState < string > ( )
4044 const [ currentTab , setCurrentTab ] = useState < string > ( )
45+ const [ { width } , setRef ] = useContainerSize ( )
4146
4247 const PaymentTitle = intl . formatMessage ( { id : 'docs.widgets.amountDistributionCalculator.paymentSettings.title' } )
4348 const PaymentAmountTitle = intl . formatMessage ( { id : 'docs.widgets.amountDistributionCalculator.paymentSettings.amount.title' } )
@@ -77,6 +82,12 @@ export const AmountDistributionCalculator: React.FC = () => {
7782 updateDistributionItem ( index , item )
7883 } , [ updateDistributionItem ] )
7984
85+ const recipientsCardsSpan = useMemo < number > ( ( ) => {
86+ // Number of cards is between 1 and 4
87+ const maxCards = Math . min ( Math . max ( 1 , Math . floor ( ( width + RECIPIENTS_CARDS_GAP ) / ( MIN_RECIPIENT_CARD_WIDTH + RECIPIENTS_CARDS_GAP ) ) ) , 4 )
88+ return Math . round ( 24 / maxCards )
89+ } , [ width ] )
90+
8091 return (
8192 < Row gutter = { GUTTER_ROW } align = 'stretch' justify = 'start' >
8293 < Col >
@@ -107,22 +118,22 @@ export const AmountDistributionCalculator: React.FC = () => {
107118 ) ,
108119 } , {
109120 label : RecipientsTitle , key : 'recipients' , children : (
110- < Row gutter = { GUTTER_ROW } align = 'stretch' justify = 'start' >
121+ < Row gutter = { GUTTER_ROW } ref = { setRef } >
111122 { distributions . map ( ( distribution , i ) => (
112- < Col key = { createRecipientKey ( distribution . recipient ) } span = { 8 } >
123+ < Col key = { createRecipientKey ( distribution . recipient ) } span = { recipientsCardsSpan } >
113124 < Card
114125 title = {
115126 < Row align = 'middle' justify = 'end' gutter = { 0 } >
116127 < Col >
117128 < Button . Icon
118- size = 'small '
129+ size = 'medium '
119130 onClick = { ( ) => {
120131 setDistributions ( ( prev ) => {
121132 return prev . filter ( ( _ , index ) => index !== i )
122133 } )
123134 } }
124135 >
125- < Trash />
136+ < Trash size = 'medium' />
126137 </ Button . Icon >
127138 </ Col >
128139 </ Row >
@@ -138,17 +149,14 @@ export const AmountDistributionCalculator: React.FC = () => {
138149 </ Card >
139150 </ Col >
140151 ) ) }
141- < Col span = { 8 } >
142- < Card width = '100%' >
143- < Row align = 'bottom' justify = 'center' gutter = { 0 } >
144- < Col >
145- < Button . Icon
146- onClick = { ( ) => setDistributions ( [ ...distributions , generateRandomDistributionItem ( ) ] ) }
147- >
148- < Plus />
149- </ Button . Icon >
150- </ Col >
151- </ Row >
152+ < Col span = { recipientsCardsSpan } >
153+ < Card
154+ hoverable
155+ onClick = { ( ) => setDistributions ( [ ...distributions , generateRandomDistributionItem ( ) ] ) }
156+ >
157+ < div className = { styles . newRecipientCardBody } >
158+ < Plus size = 'large' />
159+ </ div >
152160 </ Card >
153161 </ Col >
154162 </ Row >
0 commit comments