Skip to content

Commit 8e871df

Browse files
committed
feat: resolve props drilling
1 parent 5c5ff95 commit 8e871df

File tree

7 files changed

+71
-91
lines changed

7 files changed

+71
-91
lines changed

src/components/layout/containers/AttributeContainer.tsx

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -92,40 +92,42 @@ export const AttributeContainer = <
9292
>
9393
<Stack>
9494
<Typography fontWeight={600}>{attribute.name}</Typography>
95-
<Stack direction={'row'} spacing={2} alignItems={'center'}>
96-
{attribute.dailyCallsPerConsumer && (
97-
<Stack direction={'row'} spacing={1}>
98-
<Typography
99-
sx={{
100-
fontSize: 16,
101-
}}
102-
>
103-
{t('thresholdLabel')}
104-
</Typography>
105-
<Typography
106-
sx={{
107-
fontSize: 16,
108-
fontWeight: 700,
109-
}}
110-
>
111-
{attribute.dailyCallsPerConsumer}
112-
</Typography>
113-
</Stack>
114-
)}
115-
<Button
116-
sx={{
117-
justifyContent: 'start',
118-
px: 1,
119-
width: 'fit-content',
120-
}}
121-
onClick={(e) => {
122-
e.stopPropagation()
123-
onCustomizeThreshold?.()
124-
}}
125-
>
126-
{attribute.dailyCallsPerConsumer ? t('changeBtn') : t('customizeBtn')}
127-
</Button>
128-
</Stack>
95+
{onCustomizeThreshold && (
96+
<Stack direction={'row'} spacing={2} alignItems={'center'}>
97+
{attribute.dailyCallsPerConsumer && (
98+
<Stack direction={'row'} spacing={1}>
99+
<Typography
100+
sx={{
101+
fontSize: 16,
102+
}}
103+
>
104+
{t('thresholdLabel')}
105+
</Typography>
106+
<Typography
107+
sx={{
108+
fontSize: 16,
109+
fontWeight: 700,
110+
}}
111+
>
112+
{attribute.dailyCallsPerConsumer}
113+
</Typography>
114+
</Stack>
115+
)}
116+
<Button
117+
sx={{
118+
justifyContent: 'start',
119+
px: 1,
120+
width: 'fit-content',
121+
}}
122+
onClick={(e) => {
123+
e.stopPropagation()
124+
onCustomizeThreshold()
125+
}}
126+
>
127+
{attribute.dailyCallsPerConsumer ? t('changeBtn') : t('customizeBtn')}
128+
</Button>
129+
</Stack>
130+
)}
129131
</Stack>
130132
</AccordionSummary>
131133
<AccordionDetails>

src/components/shared/AddAttributesToForm/AddAttributesToForm.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,18 @@ import { SectionContainer } from '@/components/layout/containers'
66
import { Box, Button, Link, Stack } from '@mui/material'
77
import { attributesHelpLink } from '@/config/constants'
88
import { AttributeGroup } from './AttributeGroup'
9-
import { type DescriptorAttribute } from '@/api/api.generatedTypes'
109
import { type CreateStepThresholdsFormValues } from '@/pages/ProviderEServiceCreatePage/components/EServiceCreateStepThresholds'
1110

1211
export type AddAttributesToFormProps = {
1312
attributeKey: AttributeKey
1413
readOnly: boolean
1514
openCreateAttributeDrawer?: VoidFunction
16-
openCustomizeThresholdDrawer?: (
17-
attribute: DescriptorAttribute,
18-
attributeKey: AttributeKey,
19-
attributeGroupIndex: number
20-
) => void
2115
}
2216

2317
export const AddAttributesToForm: React.FC<AddAttributesToFormProps> = ({
2418
attributeKey,
2519
readOnly,
2620
openCreateAttributeDrawer,
27-
openCustomizeThresholdDrawer,
2821
}) => {
2922
const { t } = useTranslation('eservice', { keyPrefix: `create.step3` })
3023
const { t: tAttribute } = useTranslation('attribute')
@@ -77,7 +70,6 @@ export const AddAttributesToForm: React.FC<AddAttributesToFormProps> = ({
7770
readOnly={readOnly}
7871
onRemoveAttributesGroup={handleRemoveAttributesGroup}
7972
onRemoveAttributeFromGroup={handleRemoveAttributeFromGroup}
80-
onOpenCustomizeThresholdDrawer={openCustomizeThresholdDrawer}
8173
/>
8274
))}
8375
</Stack>

src/components/shared/AddAttributesToForm/AttributeGroup.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { AttributeAutocomplete } from '../AttributeAutocomplete'
99
import type { DescriptorAttribute } from '@/api/api.generatedTypes'
1010
import { useFormContext } from 'react-hook-form'
1111
import type { CreateStepAttributesFormValues } from '@/pages/ProviderEServiceCreatePage/components/EServiceCreateStepAttributes'
12+
import { useCustomizeThresholdDrawer } from '../CustomizeThresholdDrawer'
1213

1314
export type AttributeGroupProps = {
1415
group: Array<DescriptorAttribute>
@@ -17,11 +18,6 @@ export type AttributeGroupProps = {
1718
readOnly: boolean
1819
onRemoveAttributesGroup: (groupIndex: number) => void
1920
onRemoveAttributeFromGroup: (attributeId: string, groupIndex: number) => void
20-
onOpenCustomizeThresholdDrawer?: (
21-
attribute: DescriptorAttribute,
22-
attributeKey: AttributeKey,
23-
attributeGroupIndex: number
24-
) => void
2521
}
2622

2723
export const AttributeGroup: React.FC<AttributeGroupProps> = ({
@@ -31,10 +27,10 @@ export const AttributeGroup: React.FC<AttributeGroupProps> = ({
3127
readOnly,
3228
onRemoveAttributesGroup,
3329
onRemoveAttributeFromGroup,
34-
onOpenCustomizeThresholdDrawer,
3530
}) => {
3631
const { t } = useTranslation('attribute', { keyPrefix: 'group' })
3732
const [isAttributeAutocompleteShown, setIsAttributeAutocompleteShown] = React.useState(false)
33+
const { open } = useCustomizeThresholdDrawer()
3834

3935
const handleDeleteAttributesGroup = () => {
4036
onRemoveAttributesGroup(groupIndex)
@@ -69,8 +65,8 @@ export const AttributeGroup: React.FC<AttributeGroupProps> = ({
6965
onRemove={
7066
!readOnly ? handleDeleteAttributeFromGroup.bind(null, attribute.id) : undefined
7167
}
72-
onCustomizeThreshold={() =>
73-
onOpenCustomizeThresholdDrawer?.(attribute, attributeKey, groupIndex)
68+
onCustomizeThreshold={
69+
attributeKey === 'certified' ? () => open(attribute, groupIndex) : undefined
7470
}
7571
/>
7672
</Box>

src/components/shared/CustomizeThresholdDrawer.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,39 @@ import { Alert, Stack, Button, Typography } from '@mui/material'
55
import { RHFTextField } from './react-hook-form-inputs'
66
import { useTranslation } from 'react-i18next'
77
import { type DescriptorAttribute } from '@/api/api.generatedTypes'
8-
import { type AttributeKey } from '@/types/attribute.types'
98
import { WarningAmber } from '@mui/icons-material'
9+
import { create } from 'zustand'
1010

1111
export type CustomizeThresholdDrawerProps = {
12-
isOpen: boolean
13-
onClose: VoidFunction
14-
attribute?: DescriptorAttribute
15-
attributeKey?: AttributeKey
16-
attributeGroupIndex?: number
1712
dailyCallsPerConsumer?: number
1813
dailyCallsTotal?: number
1914
onSubmit: (threshold: number) => void
2015
}
2116

17+
type CustomizeThresholdDrawerStore = {
18+
isOpen: boolean
19+
open: (attribute: DescriptorAttribute, attributeGroupIndex: number) => void
20+
close: VoidFunction
21+
attribute?: DescriptorAttribute
22+
attributeGroupIndex?: number
23+
}
24+
25+
export const useCustomizeThresholdDrawer = create<CustomizeThresholdDrawerStore>((set) => ({
26+
isOpen: false,
27+
open: (attribute, attributeGroupIndex) => set({ attribute, attributeGroupIndex, isOpen: true }),
28+
close: () => set({ isOpen: false }),
29+
}))
30+
2231
type CustomizeThresholdFormValues = {
2332
threshold: number
2433
}
2534

2635
export const CustomizeThresholdDrawer: React.FC<CustomizeThresholdDrawerProps> = ({
27-
isOpen,
28-
onClose,
2936
onSubmit,
30-
attribute,
31-
attributeKey,
3237
dailyCallsPerConsumer,
3338
dailyCallsTotal,
3439
}) => {
40+
const { isOpen, close, attribute } = useCustomizeThresholdDrawer()
3541
const { t } = useTranslation('eservice', {
3642
keyPrefix: 'create.step2.attributes.customizeThresholdDrawer',
3743
})
@@ -57,9 +63,9 @@ export const CustomizeThresholdDrawer: React.FC<CustomizeThresholdDrawerProps> =
5763
<Drawer
5864
isOpen={isOpen}
5965
title={t('title')}
60-
subtitle={t('subtitle', { type: attributeKey, name: attribute?.name })}
66+
subtitle={t('subtitle', { name: attribute?.name })}
6167
onTransitionExited={formMethods.reset}
62-
onClose={onClose}
68+
onClose={close}
6369
>
6470
<Stack
6571
justifyContent={'space-between'}

src/pages/ProviderEServiceCreatePage/components/EServiceCreateStepThresholds/EServiceCreateStepThresholds.tsx

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { compareObjects } from '@/utils/common.utils'
2727
import { remapDescriptorAttributesToDescriptorAttributesSeed } from '@/utils/attribute.utils'
2828
import {
2929
CustomizeThresholdDrawer,
30-
type CustomizeThresholdDrawerProps,
30+
useCustomizeThresholdDrawer,
3131
} from '@/components/shared/CustomizeThresholdDrawer'
3232

3333
export type CreateStepThresholdsFormValues = {
@@ -53,6 +53,12 @@ export const EServiceCreateStepThresholds: React.FC<ActiveStepProps> = () => {
5353
},
5454
})
5555

56+
const {
57+
attribute,
58+
attributeGroupIndex,
59+
close: closeCustomizeThresholdDrawer,
60+
} = useCustomizeThresholdDrawer()
61+
5662
const isEServiceCreatedFromTemplate = Boolean(descriptor?.templateRef?.templateVersionId)
5763

5864
const [createAttributeCreateDrawerState, setCreateAttributeCreateDrawerState] = React.useState<{
@@ -63,12 +69,6 @@ export const EServiceCreateStepThresholds: React.FC<ActiveStepProps> = () => {
6369
isOpen: false,
6470
})
6571

66-
const [customizeThresholdDrawerState, setCustomizeThresholdDrawerState] = React.useState<
67-
Omit<CustomizeThresholdDrawerProps, 'onClose' | 'onSubmit'>
68-
>({
69-
isOpen: false,
70-
})
71-
7272
const handleCloseAttributeCreateDrawer = () => {
7373
setCreateAttributeCreateDrawerState((prevState) => ({ ...prevState, isOpen: false }))
7474
}
@@ -78,34 +78,21 @@ export const EServiceCreateStepThresholds: React.FC<ActiveStepProps> = () => {
7878
setCreateAttributeCreateDrawerState({ attributeKey, isOpen: true })
7979
}
8080

81-
const handleCloseCustomizeThresholdDrawer = () => {
82-
setCustomizeThresholdDrawerState({ isOpen: false })
83-
}
84-
8581
const handleSubmitCustomizeThresholdDrawer = (threshold: number) => {
86-
const { attribute, attributeKey, attributeGroupIndex } = customizeThresholdDrawerState
87-
if (!attribute || !attributeKey || attributeGroupIndex === undefined) return
82+
if (!attribute || attributeGroupIndex === undefined) return
8883

8984
const attributes = formMethods.getValues('attributes')
90-
const groups = attributes[attributeKey as keyof typeof attributes]
85+
const groups = attributes['certified']
9186
const group = groups[attributeGroupIndex]
9287

9388
groups[attributeGroupIndex] = group.map((att) =>
9489
att.id === attribute.id ? { ...att, dailyCallsPerConsumer: threshold } : att
9590
)
9691

97-
formMethods.setValue(`attributes.${attributeKey as keyof typeof attributes}`, groups, {
92+
formMethods.setValue(`attributes.certified`, groups, {
9893
shouldValidate: false,
9994
})
100-
setCustomizeThresholdDrawerState({ isOpen: false })
101-
}
102-
103-
const handleOpenCustomizeThresholdDrawer = (
104-
attribute: DescriptorAttribute,
105-
attributeKey: AttributeKey,
106-
attributeGroupIndex: number
107-
) => {
108-
setCustomizeThresholdDrawerState({ isOpen: true, attribute, attributeKey, attributeGroupIndex })
95+
closeCustomizeThresholdDrawer()
10996
}
11097

11198
const dailyCallsPerConsumer = formMethods.watch('dailyCallsPerConsumer')
@@ -205,7 +192,6 @@ export const EServiceCreateStepThresholds: React.FC<ActiveStepProps> = () => {
205192
<AddAttributesToForm
206193
attributeKey="certified"
207194
readOnly={isEServiceCreatedFromTemplate}
208-
openCustomizeThresholdDrawer={handleOpenCustomizeThresholdDrawer}
209195
/>
210196
</TabPanel>
211197
<TabPanel value="verified">
@@ -242,10 +228,8 @@ export const EServiceCreateStepThresholds: React.FC<ActiveStepProps> = () => {
242228
onClose={handleCloseAttributeCreateDrawer}
243229
/>
244230
<CustomizeThresholdDrawer
245-
{...customizeThresholdDrawerState}
246231
dailyCallsTotal={dailyCallsTotal}
247232
dailyCallsPerConsumer={dailyCallsPerConsumer}
248-
onClose={handleCloseCustomizeThresholdDrawer}
249233
onSubmit={handleSubmitCustomizeThresholdDrawer}
250234
/>
251235
</>

src/static/locales/en/eservice.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
},
160160
"customizeThresholdDrawer": {
161161
"title": "Customize threshold",
162-
"subtitle": "Set a custom threshold for the attribute {{type}} {{name}}.",
162+
"subtitle": "Set a custom threshold for the certified attribute {{name}}.",
163163
"submitBtnLabel": "Customize threshold",
164164
"field": {
165165
"placeholder": "API calls/day per consumer",

src/static/locales/it/eservice.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
},
160160
"customizeThresholdDrawer": {
161161
"title": "Personalizza soglia",
162-
"subtitle": "Imposta una soglia personalizzata per l’attributo {{type}} {{name}}.",
162+
"subtitle": "Imposta una soglia personalizzata per l’attributo certificato {{name}}.",
163163
"submitBtnLabel": "Personalizza soglia",
164164
"field": {
165165
"placeholder": "Chiamate API/giorno per fruitore",

0 commit comments

Comments
 (0)