Skip to content

Commit 07a6b9a

Browse files
committed
refactor: added some logic within usenNotificationConfigHook
1 parent c93e8d7 commit 07a6b9a

File tree

4 files changed

+665
-591
lines changed

4 files changed

+665
-591
lines changed

src/pages/NotificationUserConfigPage/components/NotificationUserConfigTab.tsx

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@ import React, { useCallback, useEffect, useRef } from 'react'
22
import { FormProvider, useForm } from 'react-hook-form'
33
import { NotificationConfigSection } from './NotificationConfigSection'
44
import { SectionContainer } from '@/components/layout/containers'
5-
import { Box, Card, Link, Stack, Typography, Button } from '@mui/material'
5+
import {
6+
Box,
7+
Card,
8+
Link,
9+
Stack,
10+
Typography,
11+
Button,
12+
FormControl,
13+
InputLabel,
14+
MenuItem,
15+
Select,
16+
} from '@mui/material'
617
import { RHFSwitch, SwitchLabelDescription } from '@/components/shared/react-hook-form-inputs'
718
import { useTranslation } from 'react-i18next'
819
import MenuBookIcon from '@mui/icons-material/MenuBook'
9-
import { useNotificationInAppConfigForm } from '../hooks/useNotificationInAppConfigForm'
20+
import { useNotificationInAppConfigForm as useNotificationInAppConfigHook } from '../hooks/useNotificationInAppConfigForm'
1021
import { type NotificationConfig } from '@/api/api.generatedTypes'
1122
import { debounce, isEqual } from 'lodash'
1223
import type { NotificationSubSectionSchema, NotificationConfigType } from '../types'
24+
import { match } from 'ts-pattern'
1325

1426
type NotificationConfigUserTabProps = {
1527
notificationConfig: NotificationConfig
@@ -27,9 +39,12 @@ export const NotificationConfigUserTab: React.FC<NotificationConfigUserTabProps>
2739
}) => {
2840
const { t } = useTranslation('notification', { keyPrefix: `configurationPage.${type}` })
2941

30-
const { notificationSchema } = useNotificationInAppConfigForm('inApp')
42+
const { notificationSchema, sectionComponentKeysMap } = useNotificationInAppConfigHook(type)
43+
const [emailPreferencesChoice, setPreferencesChoices] = React.useState<
44+
'notSend' | 'digest' | 'customize'
45+
>('customize')
3146

32-
const userEmail = '[email protected]'
47+
const userEmail = '[email protected]' // TODO: Should be available with api
3348

3449
const formMethods = useForm<NotificationConfig & { enableAllNotification: boolean }>({
3550
defaultValues: { ...notificationConfig, enableAllNotification: true },
@@ -39,8 +54,6 @@ export const NotificationConfigUserTab: React.FC<NotificationConfigUserTabProps>
3954
const valuesRef = useRef<NotificationConfig>(valueChanged)
4055
const previousValuesRef = useRef<NotificationConfig | null>(null)
4156

42-
console.log('type', type)
43-
4457
valuesRef.current = valueChanged
4558

4659
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -62,14 +75,27 @@ export const NotificationConfigUserTab: React.FC<NotificationConfigUserTabProps>
6275
}
6376
}, [debounceFn, valueChanged, formMethods.formState.isDirty])
6477

65-
const onClickEnableAllSectionSwitch = (sectionName: string) => {
66-
const sectionComponentsKeys = notificationSchema[sectionName].subsections.flatMap(
67-
(s: NotificationSubSectionSchema) => s.components.map((c) => c.key)
78+
const onClickEnableAllSectionSwitch = (sectionName: string, value: boolean) => {
79+
sectionComponentKeysMap[sectionName].map((inAppConfigKey: string) => {
80+
formMethods.setValue(inAppConfigKey as keyof NotificationConfig, value)
81+
})
82+
}
83+
84+
const getSwitchBySections = (sectionName: string) => {
85+
return sectionComponentKeysMap[sectionName].filter(
86+
(item) => !valuesRef.current[item as keyof NotificationConfig]
6887
)
88+
}
6989

70-
sectionComponentsKeys.map((inAppConfigKey: string) => {
71-
formMethods.setValue(inAppConfigKey as keyof NotificationConfig, true)
72-
})
90+
const isEnabledShowPreferencesSwitch = (): boolean => {
91+
return match(type)
92+
.with('email', () => {
93+
return emailPreferencesChoice === 'customize'
94+
})
95+
.with('inApp', () => {
96+
return valueChanged.enableAllNotification
97+
})
98+
.exhaustive()
7399
}
74100

75101
return (
@@ -82,12 +108,41 @@ export const NotificationConfigUserTab: React.FC<NotificationConfigUserTabProps>
82108
)}
83109

84110
{type === 'email' && (
85-
<Stack direction="row" spacing={8} sx={{ mb: 2 }}>
86-
<Typography data-testid="test-email">Indirizzo email</Typography>
87-
<Typography fontWeight={600}>{userEmail}</Typography>
88-
</Stack>
111+
<>
112+
<Stack direction="row" spacing={8} sx={{ mb: 2 }}>
113+
<Typography data-testid="test-email">Indirizzo email</Typography>
114+
<Typography fontWeight={600}>{userEmail}</Typography>
115+
</Stack>
116+
<Link
117+
href="https://docs.pagopa.it/interoperabilita-1"
118+
underline="none"
119+
variant="button"
120+
>
121+
{t('linkLabel')}
122+
</Link>
123+
</>
89124
)}
90-
<Box sx={{ px: 3, mt: 2 }}>
125+
126+
{type === 'email' && (
127+
<FormControl fullWidth sx={{ mt: 3 }}>
128+
<InputLabel id="demo-simple-select-label">{t('emailPreferencesLabel')}</InputLabel>
129+
<Select
130+
labelId="emailPrefefences"
131+
id="emailPreferences"
132+
value={emailPreferencesChoice}
133+
label={t('emailPreferencesLabel')}
134+
onChange={(event) =>
135+
setPreferencesChoices(event.target.value as 'notSend' | 'digest' | 'customize')
136+
}
137+
>
138+
<MenuItem value="notSend">{t('notSend')}</MenuItem>
139+
<MenuItem value="digest">{t('digest')}</MenuItem>
140+
<MenuItem value="customize">{t('customize')}</MenuItem>
141+
</Select>
142+
</FormControl>
143+
)}
144+
145+
<Box sx={{ ml: 2, mt: 2 }}>
91146
{type === 'inApp' && (
92147
<RHFSwitch
93148
data-testid="enableAllNotification"
@@ -101,10 +156,9 @@ export const NotificationConfigUserTab: React.FC<NotificationConfigUserTabProps>
101156
/>
102157
)}
103158

104-
{type === 'email' && <div>EMAIL DA CONFIGURARE</div>}
105-
106-
{valueChanged.enableAllNotification &&
159+
{isEnabledShowPreferencesSwitch() &&
107160
Object.keys(notificationSchema).map((sectionName) => {
161+
const isAllSwitchWithinSectionDisabled = getSwitchBySections(sectionName).length <= 0
108162
return (
109163
<Box key={sectionName} data-testid={`config-section-${sectionName}`}>
110164
<Card sx={{ ml: -2, px: 3, mb: 2 }} variant="outlined">
@@ -124,9 +178,16 @@ export const NotificationConfigUserTab: React.FC<NotificationConfigUserTabProps>
124178
<Button
125179
variant="naked"
126180
sx={{ mr: 3 }}
127-
onClick={() => onClickEnableAllSectionSwitch(sectionName)}
181+
onClick={() =>
182+
onClickEnableAllSectionSwitch(
183+
sectionName,
184+
!isAllSwitchWithinSectionDisabled
185+
)
186+
}
128187
>
129-
{t('enableSectionAllNotifications')}
188+
{isAllSwitchWithinSectionDisabled
189+
? t('disableSectionAllNotifications')
190+
: t('enableSectionAllNotifications')}
130191
</Button>
131192
</Box>
132193

0 commit comments

Comments
 (0)