Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix waiting for approval version #1033

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import React from 'react'
import { FormProvider, useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { RHFTextField } from '../shared/react-hook-form-inputs'
import { useNavigate } from '@/router'

type RejectDelegatedVersionDraftFormValues = {
reason: string
Expand All @@ -23,6 +24,7 @@ export const DialogRejectDelegatedVersionDraft: React.FC<
DialogRejectDelegatedVersionDraftProps
> = ({ eserviceId, descriptorId }) => {
const ariaLabelId = React.useId()
const navigate = useNavigate()

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

const onSubmit = ({ reason }: RejectDelegatedVersionDraftFormValues) => {
rejectDelegatedVersionDraft({ eserviceId, descriptorId, rejectionReason: reason })
rejectDelegatedVersionDraft(
{ eserviceId, descriptorId, rejectionReason: reason },
{
onSuccess: () => navigate('PROVIDE_ESERVICE_LIST'),
}
)
closeDialog()
}

Expand Down
26 changes: 12 additions & 14 deletions src/hooks/__tests__/useGetProviderEServiceActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ describe('useGetProviderEServiceTableActions tests', () => {
draftDescriptor: { id: 'test-2', state: 'DRAFT', version: '2' },
})
const { result } = renderUseGetProviderEServiceTableActionsHook(descriptorMock)
expect(result.current.actions).toHaveLength(0)
expect(result.current.actions).toHaveLength(1)
expect(result.current.actions[0].label).toBe('manageDraft')
})

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', () => {
Expand All @@ -241,9 +242,8 @@ describe('useGetProviderEServiceTableActions tests', () => {
draftDescriptor: { id: 'test-2', state: 'WAITING_FOR_APPROVAL', version: '2' },
})
const { result } = renderUseGetProviderEServiceTableActionsHook(descriptorMock)
expect(result.current.actions).toHaveLength(2)
expect(result.current.actions[0].label).toBe('approve')
expect(result.current.actions[1].label).toBe('reject')
expect(result.current.actions).toHaveLength(1)
expect(result.current.actions[0].label).toBe('manageDraft')
})

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

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

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', () => {
Expand All @@ -553,9 +553,8 @@ describe('useGetProviderEServiceTableActions tests', () => {
draftDescriptor: { id: 'test-2', state: 'WAITING_FOR_APPROVAL', version: '2' },
})
const { result } = renderUseGetProviderEServiceTableActionsHook(descriptorMock)
expect(result.current.actions).toHaveLength(2)
expect(result.current.actions[0].label).toBe('approve')
expect(result.current.actions[1].label).toBe('reject')
expect(result.current.actions).toHaveLength(1)
expect(result.current.actions[0].label).toBe('manageDraft')
})

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

it('should return the correct actions if user is an api operator and delegate, e-service is SUSPENDED with no draft descriptors', () => {
Expand Down
12 changes: 6 additions & 6 deletions src/hooks/useGetProviderEServiceActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export function useGetProviderEServiceActions(
hasVersionDraft: true,
isDraftWaitingForApproval: false,
},
() => []
() => [editDraftAction]
)
.with(
{
Expand All @@ -255,7 +255,7 @@ export function useGetProviderEServiceActions(
hasVersionDraft: true,
isDraftWaitingForApproval: true,
},
() => [approveDelegatedVersionDraftAction, rejectDelegatedVersionDraftAction]
() => [editDraftAction]
)
.with({ isAdmin: true, isDelegator: false, isDelegate: true, hasVersionDraft: false }, () => [
createNewDraftAction,
Expand Down Expand Up @@ -302,7 +302,7 @@ export function useGetProviderEServiceActions(
hasVersionDraft: true,
isDraftWaitingForApproval: false,
},
() => []
() => [editDraftAction]
)
.with(
{
Expand All @@ -312,7 +312,7 @@ export function useGetProviderEServiceActions(
hasVersionDraft: true,
isDraftWaitingForApproval: true,
},
() => [approveDelegatedVersionDraftAction, rejectDelegatedVersionDraftAction]
() => [editDraftAction]
)
.with({ isAdmin: false, isDelegator: false, isDelegate: true, hasVersionDraft: false }, () => [
createNewDraftAction,
Expand Down Expand Up @@ -382,7 +382,7 @@ export function useGetProviderEServiceActions(
hasVersionDraft: true,
isDraftWaitingForApproval: true,
},
() => [approveDelegatedVersionDraftAction, rejectDelegatedVersionDraftAction]
() => [editDraftAction]
)
.with({ isAdmin: true, isDelegator: false, isDelegate: true, hasVersionDraft: false }, () => [
reactivateAction,
Expand Down Expand Up @@ -439,7 +439,7 @@ export function useGetProviderEServiceActions(
hasVersionDraft: true,
isDraftWaitingForApproval: true,
},
() => [approveDelegatedVersionDraftAction, rejectDelegatedVersionDraftAction]
() => [editDraftAction]
)
.with({ isAdmin: false, isDelegator: false, isDelegate: true, hasVersionDraft: false }, () => [
createNewDraftAction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { EServiceQueries } from '@/api/eservice'
import { ButtonSkeleton } from '@/components/shared/MUI-skeletons'
import { useGetProviderEServiceActions } from '@/hooks/useGetProviderEServiceActions'
import { TableRow } from '@pagopa/interop-fe-commons'
import type { ProducerEService } from '@/api/api.generatedTypes'
import type { EServiceDescriptorState, ProducerEService } from '@/api/api.generatedTypes'
import { AuthHooks } from '@/api/auth'
import { useQueryClient } from '@tanstack/react-query'
import { ByDelegationChip } from '@/components/shared/ByDelegationChip'
Expand Down Expand Up @@ -38,8 +38,8 @@ export const EServiceTableRow: React.FC<EServiceTableRow> = ({ eservice }) => {
eservice.mode
)

const isEServiceInDraft = !eservice.activeDescriptor
const isEServiceEditable = (isAdmin || isOperatorAPI) && isEServiceInDraft
const hasActiveDescriptor = eservice.activeDescriptor
const isEServiceEditable = (isAdmin || isOperatorAPI) && !hasActiveDescriptor

const isEServiceByDelegation = isDelegate || isDelegator

Expand Down Expand Up @@ -70,10 +70,10 @@ export const EServiceTableRow: React.FC<EServiceTableRow> = ({ eservice }) => {
{eservice?.activeDescriptor && (
<StatusChip for="eservice" state={eservice.activeDescriptor.state} />
)}
{(isEServiceInDraft || eservice?.draftDescriptor) && (
{(!hasActiveDescriptor || eservice?.draftDescriptor) && (
<StatusChip
for="eservice"
state={'DRAFT'}
state={eservice.draftDescriptor?.state as EServiceDescriptorState}
isDraftToCorrect={eservice.draftDescriptor?.requireCorrections}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ import { RejectReasonDrawer } from '@/components/shared/RejectReasonDrawer'
import { useDrawerState } from '@/hooks/useDrawerState'
import { AuthHooks } from '@/api/auth'
import { useGetDelegationUserRole } from '@/hooks/useGetDelegationUserRole'
import { useDialog } from '@/stores'

const ProviderEServiceSummaryPage: React.FC = () => {
const { t } = useTranslation('eservice')
const { t: tCommon } = useTranslation('common', { keyPrefix: 'actions' })
const { t: tDialogApproveDelegatedVersionDraft } = useTranslation('shared-components', {
keyPrefix: 'dialogApproveDelegatedVersionDraft',
})
const { jwt } = AuthHooks.useJwt()

const { eserviceId, descriptorId } = useParams<'PROVIDE_ESERVICE_SUMMARY'>()
const navigate = useNavigate()
const { openDialog, closeDialog } = useDialog()

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

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

const delegation = producerDelegations?.find(
(delegation) => delegation.eservice?.id === eserviceId
)

const { mutate: deleteVersion } = EServiceMutations.useDeleteVersionDraft()
const { mutate: deleteDraft } = EServiceMutations.useDeleteDraft()
const { mutate: publishVersion } = EServiceMutations.usePublishVersionDraft({
isByDelegation: isDelegate,
})
const { mutate: approveDelegatedVersionDraft } =
EServiceMutations.useApproveDelegatedVersionDraft()

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

const delegation = producerDelegations?.find(
(delegation) => delegation.eservice?.id === eserviceId
)

publishVersion(
{
eserviceId: descriptor.eservice.id,
Expand All @@ -101,6 +108,38 @@ const ProviderEServiceSummaryPage: React.FC = () => {
)
}

const handleRejectDelegatedVersionDraft = () => {
openDialog({
type: 'rejectDelegatedVersionDraft',
eserviceId,
descriptorId,
})
}

const handleApproveDelegatedVersionDraft = () => {
const handleProceed = () => {
approveDelegatedVersionDraft(
{ eserviceId, descriptorId },
{
onSuccess: () =>
navigate('PROVIDE_ESERVICE_MANAGE', { params: { eserviceId, descriptorId } }),
}
)
closeDialog()
}

openDialog({
type: 'basic',
title: tDialogApproveDelegatedVersionDraft('title'),
description: tDialogApproveDelegatedVersionDraft('description', {
eserviceName: delegation?.eservice?.name,
delegateName: delegation?.delegate.name,
}),
proceedLabel: tDialogApproveDelegatedVersionDraft('actions.approveAndPublish'),
onProceed: handleProceed,
})
}

const canBePublished = () => {
return !!(
descriptor &&
Expand Down Expand Up @@ -220,6 +259,25 @@ const ProviderEServiceSummaryPage: React.FC = () => {
<PublishButton onClick={handlePublishDraft} disabled={!canBePublished()} />
</Stack>
)}
{isDelegator && descriptor?.state === 'WAITING_FOR_APPROVAL' && (
<Stack spacing={1} sx={{ mt: 4 }} direction="row" justifyContent="end">
<Button
startIcon={<DeleteOutlineIcon />}
variant="text"
color="error"
onClick={handleRejectDelegatedVersionDraft}
>
{tCommon('reject')}
</Button>
<Button
startIcon={<PublishIcon />}
variant="contained"
onClick={handleApproveDelegatedVersionDraft}
>
{tCommon('publish')}
</Button>
</Stack>
)}

{requireDelegateCorrections && sortedRejectedReasons && (
<RejectReasonDrawer
Expand Down
Loading