Skip to content

Commit 415a72c

Browse files
committed
fix preselectedConsumer and agreements option passed to autocomplete
1 parent 1a6f37b commit 415a72c

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/components/dialogs/DialogSelectAgreementConsumer/DialogSelectAgreementConsumer.tsx

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import React from 'react'
1717
import { FormProvider, useForm } from 'react-hook-form'
1818
import { useTranslation } from 'react-i18next'
1919
import { DialogSelectAgreementConsumerAutocomplete } from './DialogSelectAgreementConsumerAutocomplete'
20+
import { match } from 'ts-pattern'
2021

2122
type SelectAgreementConsumerFormValues = {
2223
consumerId: string
@@ -37,9 +38,33 @@ export const DialogSelectAgreementConsumer: React.FC<DialogSelectAgreementConsum
3738
const { closeDialog } = useDialog()
3839
const { jwt } = AuthHooks.useJwt()
3940

41+
const preselectedConsumer = jwt
42+
? ({
43+
id: jwt?.organizationId,
44+
name: jwt?.organization.name,
45+
} as DelegationTenant)
46+
: undefined
47+
48+
const agreementsOptions = match(action)
49+
.with('inspect', () =>
50+
agreements.filter(
51+
(agreement) =>
52+
agreement.state === 'ACTIVE' ||
53+
agreement.state === 'SUSPENDED' ||
54+
agreement.state === 'PENDING'
55+
)
56+
)
57+
.with('edit', () => agreements.filter((agreement) => agreement.state === 'DRAFT'))
58+
.exhaustive()
59+
60+
const hasPreselectedConsumer = Boolean(
61+
agreementsOptions.find((agreement) => agreement.consumerId === preselectedConsumer?.id)
62+
)
63+
4064
const formMethods = useForm<SelectAgreementConsumerFormValues>({
4165
defaultValues: {
42-
consumerId: jwt?.organizationId ?? undefined,
66+
consumerId:
67+
preselectedConsumer && hasPreselectedConsumer ? preselectedConsumer.id : undefined,
4368
},
4469
})
4570

@@ -70,15 +95,8 @@ export const DialogSelectAgreementConsumer: React.FC<DialogSelectAgreementConsum
7095
<Typography>{t(`description.${action}`)}</Typography>
7196
<DialogSelectAgreementConsumerAutocomplete
7297
eserviceId={eserviceId}
73-
preselectedConsumer={
74-
jwt
75-
? ({
76-
id: jwt?.organizationId,
77-
name: jwt?.organization.name,
78-
} as DelegationTenant)
79-
: undefined
80-
}
81-
agreements={agreements}
98+
preselectedConsumer={hasPreselectedConsumer ? preselectedConsumer : undefined}
99+
agreements={agreementsOptions}
82100
action={action}
83101
/>
84102
</Stack>

src/components/dialogs/DialogSelectAgreementConsumer/DialogSelectAgreementConsumerAutocomplete.tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@ export const DialogSelectAgreementConsumerAutocomplete: React.FC<
2424
})
2525
const { jwt } = AuthHooks.useJwt()
2626

27-
const agreementOptions = match(action)
28-
.with('inspect', () =>
29-
agreements.filter(
30-
(agreement) =>
31-
agreement.state === 'ACTIVE' ||
32-
agreement.state === 'SUSPENDED' ||
33-
agreement.state === 'PENDING'
34-
)
35-
)
36-
.with('edit', () => agreements.filter((agreement) => agreement.state === 'DRAFT'))
37-
.exhaustive()
38-
3927
const { data: delegations = [] } = useQuery({
4028
...DelegationQueries.getList({
4129
limit: 50,
@@ -100,19 +88,19 @@ export const DialogSelectAgreementConsumerAutocomplete: React.FC<
10088
}, [setValue, selectedConsumerId, delegators, setConsumerAutocompleteTextInput])
10189

10290
const delegatorOptions = delegators.filter((delegator) =>
103-
agreementOptions.some((agreement) => agreement.consumerId === delegator.id)
91+
agreements.some((agreement) => agreement.consumerId === delegator.id)
10492
)
10593

10694
const tenantOptions = match(action)
10795
.with('inspect', () =>
108-
jwt && agreementOptions.some((agreement) => agreement.consumerId === jwt.organizationId)
96+
jwt && agreements.some((agreement) => agreement.consumerId === jwt.organizationId)
10997
? [{ id: jwt.organizationId, name: jwt.organization.name }, ...delegatorOptions]
11098
: delegatorOptions
11199
)
112100
.with('edit', () =>
113101
jwt &&
114102
!isDelegator &&
115-
agreementOptions.some((agreement) => agreement.consumerId === jwt.organizationId)
103+
agreements.some((agreement) => agreement.consumerId === jwt.organizationId)
116104
? [{ id: jwt.organizationId, name: jwt.organization.name }, ...delegatorOptions]
117105
: delegatorOptions
118106
)

0 commit comments

Comments
 (0)