Skip to content

Commit 66ed564

Browse files
authored
Fix waiting for approval version (#1033)
* update useGetProviderEServiceActions and its tests * fix eserviceTableRow status chip * Update ProviderEServiceSummary.page.tsx * Fix useGetProviderEServiceActions and tests * fix navigation after mutation on accept and reject version draft
1 parent d9e4459 commit 66ed564

File tree

5 files changed

+93
-30
lines changed

5 files changed

+93
-30
lines changed

src/components/dialogs/DialogRejectDelegatedVersionDraft.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import React from 'react'
1414
import { FormProvider, useForm } from 'react-hook-form'
1515
import { useTranslation } from 'react-i18next'
1616
import { RHFTextField } from '../shared/react-hook-form-inputs'
17+
import { useNavigate } from '@/router'
1718

1819
type RejectDelegatedVersionDraftFormValues = {
1920
reason: string
@@ -23,6 +24,7 @@ export const DialogRejectDelegatedVersionDraft: React.FC<
2324
DialogRejectDelegatedVersionDraftProps
2425
> = ({ eserviceId, descriptorId }) => {
2526
const ariaLabelId = React.useId()
27+
const navigate = useNavigate()
2628

2729
const { t } = useTranslation('shared-components', {
2830
keyPrefix: 'dialogRejectDelegatedVersionDraft',
@@ -36,7 +38,12 @@ export const DialogRejectDelegatedVersionDraft: React.FC<
3638
})
3739

3840
const onSubmit = ({ reason }: RejectDelegatedVersionDraftFormValues) => {
39-
rejectDelegatedVersionDraft({ eserviceId, descriptorId, rejectionReason: reason })
41+
rejectDelegatedVersionDraft(
42+
{ eserviceId, descriptorId, rejectionReason: reason },
43+
{
44+
onSuccess: () => navigate('PROVIDE_ESERVICE_LIST'),
45+
}
46+
)
4047
closeDialog()
4148
}
4249

src/hooks/__tests__/useGetProviderEServiceActions.test.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ describe('useGetProviderEServiceTableActions tests', () => {
231231
draftDescriptor: { id: 'test-2', state: 'DRAFT', version: '2' },
232232
})
233233
const { result } = renderUseGetProviderEServiceTableActionsHook(descriptorMock)
234-
expect(result.current.actions).toHaveLength(0)
234+
expect(result.current.actions).toHaveLength(1)
235+
expect(result.current.actions[0].label).toBe('manageDraft')
235236
})
236237

237238
it('should return the correct actions if user is admin and delegator, e-service is PUBLISHED with a draft descriptor in state WAITING_FOR_APPROVAL', () => {
@@ -241,9 +242,8 @@ describe('useGetProviderEServiceTableActions tests', () => {
241242
draftDescriptor: { id: 'test-2', state: 'WAITING_FOR_APPROVAL', version: '2' },
242243
})
243244
const { result } = renderUseGetProviderEServiceTableActionsHook(descriptorMock)
244-
expect(result.current.actions).toHaveLength(2)
245-
expect(result.current.actions[0].label).toBe('approve')
246-
expect(result.current.actions[1].label).toBe('reject')
245+
expect(result.current.actions).toHaveLength(1)
246+
expect(result.current.actions[0].label).toBe('manageDraft')
247247
})
248248

249249
it('should return the correct actions if user is admin and delegate, e-service is PUBLISHED with no draft descriptors', () => {
@@ -333,9 +333,8 @@ describe('useGetProviderEServiceTableActions tests', () => {
333333
draftDescriptor: { id: 'test-2', state: 'WAITING_FOR_APPROVAL', version: '2' },
334334
})
335335
const { result } = renderUseGetProviderEServiceTableActionsHook(descriptorMock)
336-
expect(result.current.actions).toHaveLength(2)
337-
expect(result.current.actions[0].label).toBe('approve')
338-
expect(result.current.actions[1].label).toBe('reject')
336+
expect(result.current.actions).toHaveLength(1)
337+
expect(result.current.actions[0].label).toBe('manageDraft')
339338
})
340339

341340
it('should return the correct actions if user is admin and delegate, e-service is SUSPENDED with no draft descriptors', () => {
@@ -542,7 +541,8 @@ describe('useGetProviderEServiceTableActions tests', () => {
542541
draftDescriptor: { id: 'test-2', state: 'DRAFT', version: '2' },
543542
})
544543
const { result } = renderUseGetProviderEServiceTableActionsHook(descriptorMock)
545-
expect(result.current.actions).toHaveLength(0)
544+
expect(result.current.actions).toHaveLength(1)
545+
expect(result.current.actions[0].label).toBe('manageDraft')
546546
})
547547

548548
it('should return the correct actions if user is an api operator and delegator, e-service is PUBLISHED with a draft descriptor in state WAITING_FOR_APPROVAL', () => {
@@ -553,9 +553,8 @@ describe('useGetProviderEServiceTableActions tests', () => {
553553
draftDescriptor: { id: 'test-2', state: 'WAITING_FOR_APPROVAL', version: '2' },
554554
})
555555
const { result } = renderUseGetProviderEServiceTableActionsHook(descriptorMock)
556-
expect(result.current.actions).toHaveLength(2)
557-
expect(result.current.actions[0].label).toBe('approve')
558-
expect(result.current.actions[1].label).toBe('reject')
556+
expect(result.current.actions).toHaveLength(1)
557+
expect(result.current.actions[0].label).toBe('manageDraft')
559558
})
560559

561560
it('should return the correct actions if user is an api operator and delegate, e-service is PUBLISHED with no draft descriptors', () => {
@@ -648,9 +647,8 @@ describe('useGetProviderEServiceTableActions tests', () => {
648647
draftDescriptor: { id: 'test-2', state: 'WAITING_FOR_APPROVAL', version: '2' },
649648
})
650649
const { result } = renderUseGetProviderEServiceTableActionsHook(descriptorMock)
651-
expect(result.current.actions).toHaveLength(2)
652-
expect(result.current.actions[0].label).toBe('approve')
653-
expect(result.current.actions[1].label).toBe('reject')
650+
expect(result.current.actions).toHaveLength(1)
651+
expect(result.current.actions[0].label).toBe('manageDraft')
654652
})
655653

656654
it('should return the correct actions if user is an api operator and delegate, e-service is SUSPENDED with no draft descriptors', () => {

src/hooks/useGetProviderEServiceActions.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export function useGetProviderEServiceActions(
245245
hasVersionDraft: true,
246246
isDraftWaitingForApproval: false,
247247
},
248-
() => []
248+
() => [editDraftAction]
249249
)
250250
.with(
251251
{
@@ -255,7 +255,7 @@ export function useGetProviderEServiceActions(
255255
hasVersionDraft: true,
256256
isDraftWaitingForApproval: true,
257257
},
258-
() => [approveDelegatedVersionDraftAction, rejectDelegatedVersionDraftAction]
258+
() => [editDraftAction]
259259
)
260260
.with({ isAdmin: true, isDelegator: false, isDelegate: true, hasVersionDraft: false }, () => [
261261
createNewDraftAction,
@@ -302,7 +302,7 @@ export function useGetProviderEServiceActions(
302302
hasVersionDraft: true,
303303
isDraftWaitingForApproval: false,
304304
},
305-
() => []
305+
() => [editDraftAction]
306306
)
307307
.with(
308308
{
@@ -312,7 +312,7 @@ export function useGetProviderEServiceActions(
312312
hasVersionDraft: true,
313313
isDraftWaitingForApproval: true,
314314
},
315-
() => [approveDelegatedVersionDraftAction, rejectDelegatedVersionDraftAction]
315+
() => [editDraftAction]
316316
)
317317
.with({ isAdmin: false, isDelegator: false, isDelegate: true, hasVersionDraft: false }, () => [
318318
createNewDraftAction,
@@ -382,7 +382,7 @@ export function useGetProviderEServiceActions(
382382
hasVersionDraft: true,
383383
isDraftWaitingForApproval: true,
384384
},
385-
() => [approveDelegatedVersionDraftAction, rejectDelegatedVersionDraftAction]
385+
() => [editDraftAction]
386386
)
387387
.with({ isAdmin: true, isDelegator: false, isDelegate: true, hasVersionDraft: false }, () => [
388388
reactivateAction,
@@ -439,7 +439,7 @@ export function useGetProviderEServiceActions(
439439
hasVersionDraft: true,
440440
isDraftWaitingForApproval: true,
441441
},
442-
() => [approveDelegatedVersionDraftAction, rejectDelegatedVersionDraftAction]
442+
() => [editDraftAction]
443443
)
444444
.with({ isAdmin: false, isDelegator: false, isDelegate: true, hasVersionDraft: false }, () => [
445445
createNewDraftAction,

src/pages/ProviderEServiceListPage/components/EServiceTableRow.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { EServiceQueries } from '@/api/eservice'
88
import { ButtonSkeleton } from '@/components/shared/MUI-skeletons'
99
import { useGetProviderEServiceActions } from '@/hooks/useGetProviderEServiceActions'
1010
import { TableRow } from '@pagopa/interop-fe-commons'
11-
import type { ProducerEService } from '@/api/api.generatedTypes'
11+
import type { EServiceDescriptorState, ProducerEService } from '@/api/api.generatedTypes'
1212
import { AuthHooks } from '@/api/auth'
1313
import { useQueryClient } from '@tanstack/react-query'
1414
import { ByDelegationChip } from '@/components/shared/ByDelegationChip'
@@ -38,8 +38,8 @@ export const EServiceTableRow: React.FC<EServiceTableRow> = ({ eservice }) => {
3838
eservice.mode
3939
)
4040

41-
const isEServiceInDraft = !eservice.activeDescriptor
42-
const isEServiceEditable = (isAdmin || isOperatorAPI) && isEServiceInDraft
41+
const hasActiveDescriptor = eservice.activeDescriptor
42+
const isEServiceEditable = (isAdmin || isOperatorAPI) && !hasActiveDescriptor
4343

4444
const isEServiceByDelegation = isDelegate || isDelegator
4545

@@ -70,10 +70,10 @@ export const EServiceTableRow: React.FC<EServiceTableRow> = ({ eservice }) => {
7070
{eservice?.activeDescriptor && (
7171
<StatusChip for="eservice" state={eservice.activeDescriptor.state} />
7272
)}
73-
{(isEServiceInDraft || eservice?.draftDescriptor) && (
73+
{(!hasActiveDescriptor || eservice?.draftDescriptor) && (
7474
<StatusChip
7575
for="eservice"
76-
state={'DRAFT'}
76+
state={eservice.draftDescriptor?.state as EServiceDescriptorState}
7777
isDraftToCorrect={eservice.draftDescriptor?.requireCorrections}
7878
/>
7979
)}

src/pages/ProviderEServiceSummaryPage/ProviderEServiceSummary.page.tsx

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ import { RejectReasonDrawer } from '@/components/shared/RejectReasonDrawer'
2020
import { useDrawerState } from '@/hooks/useDrawerState'
2121
import { AuthHooks } from '@/api/auth'
2222
import { useGetDelegationUserRole } from '@/hooks/useGetDelegationUserRole'
23+
import { useDialog } from '@/stores'
2324

2425
const ProviderEServiceSummaryPage: React.FC = () => {
2526
const { t } = useTranslation('eservice')
2627
const { t: tCommon } = useTranslation('common', { keyPrefix: 'actions' })
28+
const { t: tDialogApproveDelegatedVersionDraft } = useTranslation('shared-components', {
29+
keyPrefix: 'dialogApproveDelegatedVersionDraft',
30+
})
2731
const { jwt } = AuthHooks.useJwt()
2832

2933
const { eserviceId, descriptorId } = useParams<'PROVIDE_ESERVICE_SUMMARY'>()
3034
const navigate = useNavigate()
35+
const { openDialog, closeDialog } = useDialog()
3136

3237
const { isOpen, openDrawer, closeDrawer } = useDrawerState()
3338

@@ -36,11 +41,17 @@ const ProviderEServiceSummaryPage: React.FC = () => {
3641
organizationId: jwt?.organizationId,
3742
})
3843

44+
const delegation = producerDelegations?.find(
45+
(delegation) => delegation.eservice?.id === eserviceId
46+
)
47+
3948
const { mutate: deleteVersion } = EServiceMutations.useDeleteVersionDraft()
4049
const { mutate: deleteDraft } = EServiceMutations.useDeleteDraft()
4150
const { mutate: publishVersion } = EServiceMutations.usePublishVersionDraft({
4251
isByDelegation: isDelegate,
4352
})
53+
const { mutate: approveDelegatedVersionDraft } =
54+
EServiceMutations.useApproveDelegatedVersionDraft()
4455

4556
const { data: descriptor, isLoading } = useQuery(
4657
EServiceQueries.getDescriptorProvider(eserviceId, descriptorId)
@@ -78,10 +89,6 @@ const ProviderEServiceSummaryPage: React.FC = () => {
7889
const handlePublishDraft = () => {
7990
if (!descriptor) return
8091

81-
const delegation = producerDelegations?.find(
82-
(delegation) => delegation.eservice?.id === eserviceId
83-
)
84-
8592
publishVersion(
8693
{
8794
eserviceId: descriptor.eservice.id,
@@ -101,6 +108,38 @@ const ProviderEServiceSummaryPage: React.FC = () => {
101108
)
102109
}
103110

111+
const handleRejectDelegatedVersionDraft = () => {
112+
openDialog({
113+
type: 'rejectDelegatedVersionDraft',
114+
eserviceId,
115+
descriptorId,
116+
})
117+
}
118+
119+
const handleApproveDelegatedVersionDraft = () => {
120+
const handleProceed = () => {
121+
approveDelegatedVersionDraft(
122+
{ eserviceId, descriptorId },
123+
{
124+
onSuccess: () =>
125+
navigate('PROVIDE_ESERVICE_MANAGE', { params: { eserviceId, descriptorId } }),
126+
}
127+
)
128+
closeDialog()
129+
}
130+
131+
openDialog({
132+
type: 'basic',
133+
title: tDialogApproveDelegatedVersionDraft('title'),
134+
description: tDialogApproveDelegatedVersionDraft('description', {
135+
eserviceName: delegation?.eservice?.name,
136+
delegateName: delegation?.delegate.name,
137+
}),
138+
proceedLabel: tDialogApproveDelegatedVersionDraft('actions.approveAndPublish'),
139+
onProceed: handleProceed,
140+
})
141+
}
142+
104143
const canBePublished = () => {
105144
return !!(
106145
descriptor &&
@@ -220,6 +259,25 @@ const ProviderEServiceSummaryPage: React.FC = () => {
220259
<PublishButton onClick={handlePublishDraft} disabled={!canBePublished()} />
221260
</Stack>
222261
)}
262+
{isDelegator && descriptor?.state === 'WAITING_FOR_APPROVAL' && (
263+
<Stack spacing={1} sx={{ mt: 4 }} direction="row" justifyContent="end">
264+
<Button
265+
startIcon={<DeleteOutlineIcon />}
266+
variant="text"
267+
color="error"
268+
onClick={handleRejectDelegatedVersionDraft}
269+
>
270+
{tCommon('reject')}
271+
</Button>
272+
<Button
273+
startIcon={<PublishIcon />}
274+
variant="contained"
275+
onClick={handleApproveDelegatedVersionDraft}
276+
>
277+
{tCommon('publish')}
278+
</Button>
279+
</Stack>
280+
)}
223281

224282
{requireDelegateCorrections && sortedRejectedReasons && (
225283
<RejectReasonDrawer

0 commit comments

Comments
 (0)