Skip to content

Commit

Permalink
♻️ refactor(admin) create Certifications::Header component
Browse files Browse the repository at this point in the history
Co-Authored-by: Geoffroy Begouaussel <[email protected]>
Co-Authored-By: GUL <[email protected]>
  • Loading branch information
3 people committed Dec 16, 2024
1 parent 68da7c9 commit c5838a0
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 138 deletions.
80 changes: 80 additions & 0 deletions admin/app/components/certifications/header.gjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import PixButton from '@1024pix/pix-ui/components/pix-button';
import PixTooltip from '@1024pix/pix-ui/components/pix-tooltip';
import { action } from '@ember/object';
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { not } from 'ember-truth-helpers';

import ConfirmPopup from '../confirm-popup';

export default class CertificationsHeader extends Component {
@service accessControl;

@tracked isModalDisplayed = false;
@tracked confirmMessage = null;

get canPublish() {
return (
!this.args.juryCertificationSummaries.some(
(certification) => certification.status === 'error' && !certification.isCancelled,
) && this.args.session.isFinalized
);
}

@action
displayConfirmationModal() {
this.confirmMessage = this.args.session.isPublished
? 'Souhaitez-vous dépublier la session ?'
: 'Souhaitez-vous publier la session ?';
this.isModalDisplayed = true;
}

@action
onModalCancel() {
this.isModalDisplayed = false;
}

@action
async toggleSessionPublication() {
if (this.args.session.isPublished) {
await this.args.unpublishSession();
} else {
await this.args.publishSession();
}
this.isModalDisplayed = false;
}

<template>
<header class="certification-list-page__header">
<h2>Certifications</h2>
{{#if this.accessControl.hasAccessToCertificationActionsScope}}
<div class="btn-group" role="group">

{{#if @session.isPublished}}
<PixButton @triggerAction={{this.displayConfirmationModal}}>Dépublier la session</PixButton>
{{else}}
<PixTooltip @position="left" @isWide={{true}} @hide={{this.canPublish}}>
<:triggerElement>
<PixButton @triggerAction={{this.displayConfirmationModal}} @isDisabled={{not this.canPublish}}>
Publier la session
</PixButton>
</:triggerElement>
<:tooltip>
Vous ne pouvez pas publier la session tant qu'elle n'est pas finalisée ou qu'il reste des certifications
en erreur.
</:tooltip>
</PixTooltip>
{{/if}}
</div>
{{/if}}
</header>

<ConfirmPopup
@message={{this.confirmMessage}}
@confirm={{this.toggleSessionPublication}}
@cancel={{this.onModalCancel}}
@show={{this.isModalDisplayed}}
/>
</template>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,17 @@ const DEFAULT_PAGE_NUMBER = 1;
export default class ListController extends Controller {
@service pixToast;
@service store;
@service accessControl;

@tracked pageNumber = DEFAULT_PAGE_NUMBER;
@tracked pageSize = 10;

@tracked displayConfirm = false;
@tracked confirmMessage = null;

get canPublish() {
const juryCertificationSummaries = this.model.juryCertificationSummaries;
const { session } = this.model;

return (
!juryCertificationSummaries.some(
(certification) => certification.status === 'error' && !certification.isCancelled,
) && session.isFinalized
);
}

get sortedCertificationJurySummaries() {
return this.model.juryCertificationSummaries
.sortBy('numberOfCertificationIssueReportsWithRequiredAction')
.reverse();
}

@action
displayCertificationStatusUpdateConfirmationModal() {
const sessionIsPublished = this.model.session.isPublished;

if (!this.canPublish && !sessionIsPublished) return;

this.confirmMessage = sessionIsPublished
? 'Souhaitez-vous dépublier la session ?'
: 'Souhaitez-vous publier la session ?';
this.displayConfirm = true;
}

@action
async toggleSessionPublication() {
const isPublished = this.model.session.isPublished;
if (isPublished) {
await this.unpublishSession();
} else {
await this.publishSession();
}
}

async unpublishSession() {
try {
await this.model.session.save({ adapterOptions: { updatePublishedCertifications: true, toPublish: false } });
Expand All @@ -64,9 +28,9 @@ export default class ListController extends Controller {
} finally {
await this.forceRefreshModelFromBackend();
}
this.hideConfirmationModal();
}

@action
async publishSession() {
try {
await this.model.session.save({ adapterOptions: { updatePublishedCertifications: true, toPublish: true } });
Expand All @@ -80,9 +44,9 @@ export default class ListController extends Controller {
if (this.model.session.isPublished) {
this.pixToast.sendSuccessNotification({ message: 'Les certifications ont été correctement publiées.' });
}
this.hideConfirmationModal();
}

@action
notifyError(error) {
if (error.errors && error.errors[0] && error.errors[0].detail) {
this.pixToast.sendErrorNotification({ message: error.errors[0].detail });
Expand All @@ -91,16 +55,8 @@ export default class ListController extends Controller {
}
}

@action
async forceRefreshModelFromBackend() {
await this.store.findRecord('session', this.model.session.id, { reload: true });
}

hideConfirmationModal() {
this.displayConfirm = false;
}

@action
onCancelConfirm() {
this.displayConfirm = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,12 @@
<section>
<div class="certification-list-page">

<header class="certification-list-page__header">
<h2>Certifications</h2>
{{#if this.accessControl.hasAccessToCertificationActionsScope}}
<div class="btn-group" role="group">

{{#if this.model.session.isPublished}}
<PixButton @triggerAction={{this.displayCertificationStatusUpdateConfirmationModal}}>Dépublier la session</PixButton>
{{else}}

{{#if this.canPublish}}
<PixButton @triggerAction={{this.displayCertificationStatusUpdateConfirmationModal}}>Publier la session</PixButton>
{{else}}

<PixTooltip @position="left" @isWide={{true}}>
<:triggerElement>
<PixButton
@triggerAction={{this.displayCertificationStatusUpdateConfirmationModal}}
@isDisabled={{true}}
>
Publier la session
</PixButton>
</:triggerElement>
<:tooltip>
Vous ne pouvez pas publier la session tant qu'elle n'est pas finalisée ou qu'il reste des
certifications en erreur.
</:tooltip>
</PixTooltip>
{{/if}}

{{/if}}
</div>
{{/if}}
</header>
<Certifications::Header
@session={{this.model.session}}
@juryCertificationSummaries={{this.model.juryCertificationSummaries}}
@unpublishSession={{this.unpublishSession}}
@publishSession={{this.publishSession}}
/>

<div>
<Certifications::List
Expand All @@ -46,10 +19,3 @@
</div>
</div>
</section>

<ConfirmPopup
@message={{this.confirmMessage}}
@confirm={{action this.toggleSessionPublication}}
@cancel={{action this.onCancelConfirm}}
@show={{this.displayConfirm}}
/>
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,7 @@ module('Acceptance | authenticated/sessions/session/certifications', function (h
setupApplicationTest(hooks);
setupMirage(hooks);

module('When user has role metier', function () {
test('it should not show the publication button', async function (assert) {
// given
await authenticateAdminMemberWithRole({ isMetier: true })(server);
server.create('session', { id: '1' });

// when
const screen = await visit('/sessions/1/certifications');

// then
assert.dom(screen.queryByText('Publier la session')).doesNotExist();
});
});

module('When user has role certif', function () {
test('the session can be published', async function (assert) {
// given
await authenticateAdminMemberWithRole({ isCertif: true })(server);

const juryCertificationSummary = server.create('jury-certification-summary', {
status: 'ok',
isCancelled: false,
});
server.create('session', {
id: '1',
finalizedAt: '2020-01-01',
publishedAt: null,
status: 'finalized',
juryCertificationSummaries: [juryCertificationSummary],
});

const screen = await visit('/sessions/1/certifications');

this.server.get('/admin/sessions/:id', (schema, request) => {
const sessionId = request.params.id;
const session = schema.sessions.findBy({ id: sessionId });
return session.update({ publishedAt: '2020-01-01' });
});

// when
await click(screen.getByRole('button', { name: 'Publier la session' }));
await screen.findByRole('dialog');
await click(screen.getByRole('button', { name: 'Confirmer' }));

// then
assert.dom(await screen.queryByText('Publier la session')).doesNotExist();
assert.dom(await screen.getByText('Dépublier la session')).exists();
});
});

// TODO : move to Certifications::List component integration level
module('When requesting next page from pagination', function () {
test('it should display next page jury certificate summary', async function (assert) {
await authenticateAdminMemberWithRole({ isSuperAdmin: true })(server);
Expand All @@ -84,6 +35,7 @@ module('Acceptance | authenticated/sessions/session/certifications', function (h
});
});

// TODO : move to Certifications::List component integration level
module('When requesting page 2 of certification from url', function () {
test('it should display page 2 jury certificate summary', async function (assert) {
await authenticateAdminMemberWithRole({ isSuperAdmin: true })(server);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module('Acceptance | authenticated/sessions/session/informations', function (hoo
assert.strictEqual(currentURL(), '/sessions/1');
});

module('When session is not published', function () {
module('When session is finalized', function () {
module('When unfinalize button is clicked', function () {
test('it should unfinalize the session', async function (assert) {
// given
Expand Down
Loading

0 comments on commit c5838a0

Please sign in to comment.