Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(condo): DOMA-10006 add throttling #5126

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export const GraphQlSearchInputWithCheckAll: React.FC<InputWithCheckAllProps> =
onChange(data)
}

if (selectedDataLength !== allDataLength && isFunction(onCheckBoxChange)) {
onCheckBoxChange(false)
}

if (selectedDataLength === allDataLength && isFunction(onCheckBoxChange)) {
onCheckBoxChange(true)
}
Expand Down
19 changes: 10 additions & 9 deletions apps/condo/domains/news/components/NewsForm/BaseNewsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ import { NewsItemSharingForm, SharingAppValues } from '@condo/domains/news/compo
import SelectSharingAppControl from '@condo/domains/news/components/NewsForm/SelectSharingAppControl'
import { NewsItemCard } from '@condo/domains/news/components/NewsItemCard'
import { MemoizedCondoNewsPreview } from '@condo/domains/news/components/NewsPreview'
import { detectTargetedSections, RecipientCounter } from '@condo/domains/news/components/RecipientCounter'
import {
detectTargetedSections,
MemoizedRecipientCounter,
} from '@condo/domains/news/components/RecipientCounter'
import { TemplatesSelect } from '@condo/domains/news/components/TemplatesSelect'
import { NewsItemScopeNoInstanceType } from '@condo/domains/news/components/types'
import { PROFANITY_TITLE_DETECTED_MOT_ERF_KER, PROFANITY_BODY_DETECTED_MOT_ERF_KER } from '@condo/domains/news/constants/errors'
Expand Down Expand Up @@ -519,7 +522,7 @@ export const BaseNewsForm: React.FC<BaseNewsFormProps> = ({

const { loading: selectedPropertiesLoading, objs: selectedProperties } = Property.useAllObjects({
where: { id_in: selectedPropertiesId },
})
}, { fetchPolicy: 'cache-first' })

const isOnlyOnePropertySelected: boolean = useMemo(() => (selectedPropertiesId.length === 1), [selectedPropertiesId.length])

Expand Down Expand Up @@ -1432,13 +1435,11 @@ export const BaseNewsForm: React.FC<BaseNewsFormProps> = ({
)}
</Row>
</Col>
{
!!formInfoColSpan && newsItemScopesNoInstance.length > 0 && (
<Col span={formInfoColSpan}>
<RecipientCounter newsItemScopes={newsItemScopesNoInstance} />
</Col>
)
}
<Col span={formInfoColSpan}>
<HiddenBlock hide={newsItemScopesNoInstance.length <= 0} >
<MemoizedRecipientCounter newsItemScopes={newsItemScopesNoInstance}/>
</HiddenBlock>
</Col>
</Row>
</Col>
</>
Expand Down
35 changes: 22 additions & 13 deletions apps/condo/domains/news/components/NewsForm/OldBaseNewsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ import { useInputWithCounter } from '@condo/domains/common/hooks/useInputWithCou
import { useValidations } from '@condo/domains/common/hooks/useValidations'
import { BaseNewsFormProps } from '@condo/domains/news/components/NewsForm/BaseNewsForm'
import { MemoizedCondoNewsPreview } from '@condo/domains/news/components/NewsPreview'
import { detectTargetedSections, RecipientCounter } from '@condo/domains/news/components/RecipientCounter'
import {
detectTargetedSections,
MemoizedRecipientCounter,
} from '@condo/domains/news/components/RecipientCounter'
import { TemplatesTabs } from '@condo/domains/news/components/TemplatesTabs'
import { NewsItemScopeNoInstanceType } from '@condo/domains/news/components/types'
import { PROFANITY_TITLE_DETECTED_MOT_ERF_KER, PROFANITY_BODY_DETECTED_MOT_ERF_KER } from '@condo/domains/news/constants/errors'
Expand Down Expand Up @@ -419,7 +422,7 @@ export const OldBaseNewsForm: React.FC<BaseNewsFormProps> = ({

const { loading: selectedPropertiesLoading, objs: selectedProperties } = Property.useAllObjects({
where: { id_in: selectedPropertiesId },
})
}, { fetchPolicy: 'cache-first' })

const isOnlyOnePropertySelected: boolean = useMemo(() => (selectedPropertiesId.length === 1), [selectedPropertiesId.length])

Expand Down Expand Up @@ -827,7 +830,15 @@ export const OldBaseNewsForm: React.FC<BaseNewsFormProps> = ({
}

return []
}, [isAllPropertiesChecked, isOnlyOnePropertySelected, selectedProperties, selectedPropertiesId.length, selectedPropertiesLoading, selectedSectionKeys, selectedUnitNameKeys])
}, [
selectedPropertiesLoading,
isAllPropertiesChecked,
isOnlyOnePropertySelected,
selectedProperties,
selectedPropertiesId,
selectedSectionKeys,
selectedUnitNameKeys,
])

const dayjsTz = dayjs().format('Z')
const tzInfo = useMemo<string>(() => {
Expand Down Expand Up @@ -1082,12 +1093,12 @@ export const OldBaseNewsForm: React.FC<BaseNewsFormProps> = ({
}
<HiddenBlock hide={newsItemForOneProperty}>
<GraphQlSearchInputWithCheckAll
checkAllFieldName='hasAllProperties'
checkAllInitialValue={get(initialValues, 'hasAllProperties', false)}
selectFormItemProps={propertySelectFormItemProps}
selectProps={propertySelectProps(form)}
onCheckBoxChange={propertyCheckboxChange(form)}
checkAllFieldName='hasAllProperties'
checkAllInitialValue={get(initialValues, 'hasAllProperties', false)}
CheckAllMessage={CheckAllLabel}
onCheckBoxChange={propertyCheckboxChange(form)}
onDataLoaded={handleAllPropertiesDataLoading}
form={form}
/>
Expand Down Expand Up @@ -1141,13 +1152,11 @@ export const OldBaseNewsForm: React.FC<BaseNewsFormProps> = ({
}
</Row>
</Col>
{
!!formInfoColSpan && newsItemScopesNoInstance.length > 0 && (
<Col span={formInfoColSpan}>
<RecipientCounter newsItemScopes={newsItemScopesNoInstance}/>
</Col>
)
}
<Col span={formInfoColSpan}>
<HiddenBlock hide={newsItemScopesNoInstance.length <= 0} >
<MemoizedRecipientCounter newsItemScopes={newsItemScopesNoInstance}/>
</HiddenBlock>
</Col>
</Row>
</Col>
<Col span={24}>
Expand Down
14 changes: 11 additions & 3 deletions apps/condo/domains/news/components/RecipientCounter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import intersectionWith from 'lodash/intersectionWith'
import isEqual from 'lodash/isEqual'
import map from 'lodash/map'
import slice from 'lodash/slice'
import throttle from 'lodash/throttle'
import uniq from 'lodash/uniq'
import React, { CSSProperties, useEffect, useMemo, useState } from 'react'
import React, { CSSProperties, useCallback, useEffect, useMemo, useState } from 'react'

import { useAuth } from '@open-condo/next/auth'
import { useIntl } from '@open-condo/next/intl'
Expand Down Expand Up @@ -237,11 +238,13 @@ export const RecipientCounter: React.FC<RecipientCounterProps> = ({ newsItemScop
},
)

const throttledGetCounters = useCallback(throttle(getCounters, 1500), [throttle])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why getCounters not in deps array? To avoid callback rerender?

Copy link
Member

@SavelevMatthew SavelevMatthew Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, throttle is imported function, its not necessary to put it in deps

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why 1500? I'm almost sure we can reproduce error consistently with this value


useEffect(() => {
const sender = getClientSideSenderInfo()
const meta = { dv: 1, sender }

getCounters({
throttledGetCounters({
variables: {
data: {
newsItemScopes: processedNewsItemScope,
Expand All @@ -266,6 +269,9 @@ export const RecipientCounter: React.FC<RecipientCounterProps> = ({ newsItemScop

const isLoadingCounters = isCountersLoading || !counters

console.log('Recipient counter', { isLoadingCounters, isCountersLoading, counters })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?



return (
<RecipientCounterContainer
title={<>{MailingMessage}<Typography.Text strong>{message}</Typography.Text></>}
Expand Down Expand Up @@ -341,7 +347,7 @@ const NewsSharingRecipientCounter: React.FC<{ contextId: string, newsItemScopes:
},
},
})
}, [getCounter, contextId, newsItemScopes])
}, [ getCounter, contextId, newsItemScopes ])

// if typeof counter !== number is used here instead of just if !counter because bool(0) => false
const isLoadingCounter = isCounterLoading || typeof counter !== 'number'
Expand All @@ -357,4 +363,6 @@ const NewsSharingRecipientCounter: React.FC<{ contextId: string, newsItemScopes:
)
}

export const MemoizedRecipientCounter = React.memo(RecipientCounter)

export const MemoizedNewsSharingRecipientCounter = React.memo(NewsSharingRecipientCounter)
Loading