Skip to content

Commit

Permalink
refactor: archive beneficiaires depuis pilotage
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurlbrjc committed Dec 17, 2024
1 parent 25be68e commit 8c85a67
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 64 deletions.
160 changes: 98 additions & 62 deletions components/pilotage/TableauBeneficiairesAArchiver.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { DateTime } from 'luxon'
import React, { useState } from 'react'

import DeleteBeneficiaireModal from 'components/jeune/DeleteBeneficiaireModal'
import Button, { ButtonStyle } from 'components/ui/Button/Button'
import { TagDate } from 'components/ui/Indicateurs/Tag'
import FailureAlert from 'components/ui/Notifications/FailureAlert'
import Table from 'components/ui/Table/Table'
import TD from 'components/ui/Table/TD'
import { TH } from 'components/ui/Table/TH'
Expand All @@ -21,77 +25,109 @@ const derniereActiviteColumn = 'Dernière activité'
export default function TableauBeneficiairesAArchiver({
beneficiaires,
}: TableauBeneficiairesAArchiverProps) {
const [beneficiaireAArchiver, setBeneficiaireAArchiver] = useState<
BeneficiaireWithActivity | undefined
>()
const [
showSuppressionCompteBeneficiaireError,
setShowSuppressionCompteBeneficiaireError,
] = useState<boolean>(false)

return (
<>
{beneficiaires.length > 0 && (
<Table caption={{ text: 'Liste des bénéficiaires à archiver' }}>
<thead>
<TR isHeader={true}>
<TH>Bénéficiaire</TH>
<TH>{dateFinCEJColumn}</TH>
<TH>{derniereActiviteColumn}</TH>
{/* <TH>Archiver</TH> */}
</TR>
</thead>
{showSuppressionCompteBeneficiaireError && (
<FailureAlert
label='Suite à un problème inconnu l’archivage a échoué. Vous pouvez réessayer.'
onAcknowledge={() => setShowSuppressionCompteBeneficiaireError(false)}
/>
)}

<tbody>
{beneficiaires.map((beneficiaire: BeneficiaireWithActivity) => (
<TR key={beneficiaire.id}>
<TD isBold>
<div>{getNomBeneficiaireComplet(beneficiaire)}</div>
</TD>
<Table caption={{ text: 'Liste des bénéficiaires à archiver' }}>
<thead className='sr-only'>
<TR isHeader={true}>
<TH>Bénéficiaire</TH>
<TH>{dateFinCEJColumn}</TH>
<TH>{derniereActiviteColumn}</TH>
<TH>Archiver</TH>
</TR>
</thead>

<TD>
<div
className='text-s-regular text-grey_800'
aria-hidden={true}
>
{dateFinCEJColumn}
</div>
<tbody>
{beneficiaires.map((beneficiaire: BeneficiaireWithActivity) => (
<TR key={beneficiaire.id}>
<TD isBold>
<div>{getNomBeneficiaireComplet(beneficiaire)}</div>
</TD>

{beneficiaire.dateFinCEJ && (
<TagDate
label={toLongMonthDate(
DateTime.fromISO(beneficiaire.dateFinCEJ)
)}
/>
)}
<TD>
<div
className='text-s-regular text-grey_800'
aria-hidden={true}
>
{dateFinCEJColumn}
</div>

{!beneficiaire.dateFinCEJ && (
<>
--
<span className='sr-only'>
information non disponible
</span>
</>
)}
</TD>
{beneficiaire.dateFinCEJ && (
<TagDate
label={toLongMonthDate(
DateTime.fromISO(beneficiaire.dateFinCEJ)
)}
/>
)}

<TD>
{beneficiaire.isActivated && (
<>
<span
className='text-xs-regular text-grey_800'
aria-hidden={true}
>
{derniereActiviteColumn}
</span>
<span className='text-s-regular'>
{toRelativeDateTime(beneficiaire.lastActivity!)}
</span>
</>
)}
{!beneficiaire.dateFinCEJ && (
<>
--
<span className='sr-only'>information non disponible</span>
</>
)}
</TD>

{!beneficiaire.isActivated && (
<span className='text-s-regular text-warning'>
Compte non activé
<TD>
{beneficiaire.isActivated && (
<>
<span
className='text-xs-regular text-grey_800'
aria-hidden={true}
>
{derniereActiviteColumn}
</span>
<span className='text-s-regular'>
{toRelativeDateTime(beneficiaire.lastActivity!)}
</span>
)}
</TD>
</TR>
))}
</tbody>
</Table>
</>
)}

{!beneficiaire.isActivated && (
<span className='text-s-regular text-warning'>
Compte non activé
</span>
)}
</TD>

<TD>
<Button
type='button'
style={ButtonStyle.PRIMARY}
onClick={() => setBeneficiaireAArchiver(beneficiaire)}
>
Archiver
<span className='sr-only'>
{getNomBeneficiaireComplet(beneficiaire)}
</span>
</Button>
</TD>
</TR>
))}
</tbody>
</Table>

{beneficiaireAArchiver && (
<DeleteBeneficiaireModal
beneficiaire={beneficiaireAArchiver}
onClose={() => setBeneficiaireAArchiver(undefined)}
onError={() => setShowSuppressionCompteBeneficiaireError(true)}
/>
)}
</>
)
Expand Down
33 changes: 31 additions & 2 deletions tests/pages/pilotage/PilotagePage.archivage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { act, screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { AxeResults } from 'axe-core'
import { axe } from 'jest-axe'
import { useRouter } from 'next/navigation'

import Pilotage from 'app/(connected)/(with-sidebar)/(with-chat)/pilotage/PilotagePage'
import { desCategories } from 'fixtures/action'
import { unBeneficiaireWithActivity } from 'fixtures/beneficiaire'
import { desMotifsDeSuppression } from 'fixtures/referentiel'
import { getMotifsSuppression } from 'services/beneficiaires.service'
import getByDescriptionTerm from 'tests/querySelector'
import renderWithContexts from 'tests/renderWithContexts'

jest.mock('services/evenements.service')
jest.mock('services/referentiel.service')
jest.mock('services/conseiller.service')
jest.mock('services/beneficiaires.service')
jest.mock('components/ModalContainer')

describe('PilotagePage client side - Archivage des bénéficiaires', () => {
describe('contenu', () => {
let container: HTMLElement

const motifsSuppression = desMotifsDeSuppression()
beforeEach(async () => {
;(getMotifsSuppression as jest.Mock).mockResolvedValue(motifsSuppression)
;(useRouter as jest.Mock).mockReturnValue({ replace: jest.fn() })

await act(async () => {
Expand All @@ -40,10 +45,14 @@ describe('PilotagePage client side - Archivage des bénéficiaires', () => {
value: [
unBeneficiaireWithActivity({
id: 'a-archiver-1',
prenom: 'Ada',
nom: 'Lovelace',
estAArchiver: true,
}),
unBeneficiaireWithActivity({
id: 'a-archiver-2',
prenom: 'Grace',
nom: 'Hopper',
estAArchiver: true,
}),
unBeneficiaireWithActivity(),
Expand Down Expand Up @@ -97,10 +106,30 @@ describe('PilotagePage client side - Archivage des bénéficiaires', () => {
name: 'Dernière activité',
})
).toBeInTheDocument()
expect(
within(tableau).getByRole('button', { name: 'Archiver Lovelace Ada' })
).toBeInTheDocument()
expect(
within(tableau).getByRole('button', { name: 'Archiver Hopper Grace' })
).toBeInTheDocument()
})

it('permet d’archiver les bénéficiaires', async () => {
// When
await userEvent.click(
screen.getByRole('button', { name: 'Archiver Lovelace Ada' })
)

// Then
expect(
screen.getByText(
'Souhaitez-vous supprimer le compte bénéficiaire : Ada Lovelace ?'
)
).toBeInTheDocument()
})
})

describe("quand le conseiller n'a pas d'animation collective à clore", () => {
describe("quand le conseiller n'a pas de bénéficiaire à archiver", () => {
it('affiche un message qui le précise', async () => {
// Given
renderWithContexts(
Expand Down

0 comments on commit 8c85a67

Please sign in to comment.