Skip to content

Commit 126fc62

Browse files
committed
chore: add debounce
1 parent 4e99413 commit 126fc62

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/pages/NotificationUserConfigPage/components/InAppNotificationUserConfigTab.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React, { useCallback, useEffect, useRef } from 'react'
12
import { FormProvider, useForm } from 'react-hook-form'
23
import { NotificationConfigSection } from './NotificationConfigSection'
34
import { SectionContainer } from '@/components/layout/containers'
@@ -7,7 +8,7 @@ import { useTranslation } from 'react-i18next'
78
import MenuBookIcon from '@mui/icons-material/MenuBook'
89
import { useNotificationInAppConfigForm } from '../hooks/useNotificationInAppConfigForm'
910
import { type NotificationConfig } from '@/api/api.generatedTypes'
10-
import React, { useEffect } from 'react'
11+
import { debounce } from 'lodash'
1112

1213
type InAppNotificationUserConfigTabProps = {
1314
inAppConfig: NotificationConfig
@@ -29,26 +30,32 @@ export type NotificationConfigSchema = {
2930
[key: string]: NotificationSectionSchema
3031
}
3132

32-
// TODO: Definire get
33-
// TODO: Definire i campi che sono visibili in base al role
34-
// TODO: Definire POST
35-
3633
export const InAppNotificationUserConfigTab: React.FC<InAppNotificationUserConfigTabProps> = ({
3734
inAppConfig,
3835
}) => {
3936
const { t } = useTranslation('notification', { keyPrefix: 'configurationPage.inAppTab' })
4037

4138
const { notificationSchema } = useNotificationInAppConfigForm()
4239

43-
const formMethods = useForm<NotificationConfig>({
44-
defaultValues: inAppConfig,
40+
const formMethods = useForm<NotificationConfig & { enableAllNotification: boolean }>({
41+
defaultValues: { ...inAppConfig, enableAllNotification: false },
4542
})
4643

4744
const valueChanged = formMethods.watch()
45+
const valuesRef = useRef(valueChanged)
46+
valuesRef.current = valueChanged
47+
48+
const debounceFn = useCallback(
49+
debounce(() => {
50+
console.log('value has been changed: call API', valuesRef.current)
51+
//TODO: Dedcide timing in ms
52+
}, 1000),
53+
[]
54+
)
4855

4956
useEffect(() => {
50-
console.log('valueChanged', valueChanged)
51-
}, [valueChanged])
57+
if (valueChanged) debounceFn()
58+
}, [debounceFn, valueChanged])
5259

5360
return (
5461
<FormProvider {...formMethods}>
@@ -58,8 +65,7 @@ export const InAppNotificationUserConfigTab: React.FC<InAppNotificationUserConfi
5865
</Link>
5966
<Box sx={{ px: 3, mt: 2 }}>
6067
<RHFSwitch
61-
name="todo"
62-
defaultChecked={true}
68+
name="enableAllNotification"
6369
label={
6470
<SwitchLabelDescription
6571
label={t('enableAllNotifications.label')}
@@ -68,7 +74,7 @@ export const InAppNotificationUserConfigTab: React.FC<InAppNotificationUserConfi
6874
}
6975
/>
7076

71-
{true &&
77+
{valueChanged.enableAllNotification &&
7278
Object.keys(notificationSchema).map((sectionName) => {
7379
return (
7480
<Box key={sectionName}>

0 commit comments

Comments
 (0)