Skip to content

Commit

Permalink
✨ admin: Allow to access attach-target-profile without existing one
Browse files Browse the repository at this point in the history
  • Loading branch information
aceol committed May 16, 2024
1 parent 61fe9d6 commit ab053b5
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<div class="page-section attach-target-profile">
<h1 class="attach-target-profile__header">
Rattacher un nouveau profil cible à la certification
{{@model.complementaryCertification.label}}
{{@complementaryCertification.label}}
</h1>
<ComplementaryCertifications::Common::LinkToCurrentTargetProfile @model={{@model.currentTargetProfile}} />

{{#if @currentTargetProfile}}
<ComplementaryCertifications::Common::LinkToCurrentTargetProfile @model={{@currentTargetProfile}} />
{{/if}}
<form class="form" {{on "submit" this.onSubmit}}>
<Card class="attach-target-profile__card" @title="1. Renseigner le nouveau profil cible à rattacher">
<ComplementaryCertifications::AttachBadges::TargetProfileSelector
Expand All @@ -26,25 +27,28 @@
@hasExternalJury={{this.hasExternalJury}}
/>
</Card>
{{#if @currentTargetProfile}}

<div class="badge-edit-form__field attach-target-profile__notification">
<Input
class="badge-edit-form__control attach-target-profile__notification__checkbox"
@type="checkbox"
@checked="false"
{{on "change" this.onNotificationUpdated}}
id="notification-checkbox"
/>
<label for="notification-checkbox">Notifier les organisations avec une campagne basée sur l’ancien PC</label>
<PixTooltip @position="top-left" @isLight={{true}} @isWide={{true}}>
<:triggerElement>
<FaIcon @icon="circle-question" tabindex="0" />
</:triggerElement>
<:tooltip>
Un email sera envoyé à chaque membre de l'organisation
</:tooltip>
</PixTooltip>
</div>
<div class="badge-edit-form__field attach-target-profile__notification">
<Input
class="badge-edit-form__control attach-target-profile__notification__checkbox"
@type="checkbox"
@checked="false"
{{on "change" this.onNotificationUpdated}}
id="notification-checkbox"
/>

<label for="notification-checkbox">Notifier les organisations avec une campagne basée sur l’ancien PC</label>
<PixTooltip @position="top-left" @isLight={{true}} @isWide={{true}}>
<:triggerElement>
<FaIcon @icon="circle-question" tabindex="0" />
</:triggerElement>
<:tooltip>
Un email sera envoyé à chaque membre de l'organisation
</:tooltip>
</PixTooltip>
</div>
{{/if}}
{{/if}}

<div class="attach-target-profile__actions">
Expand All @@ -68,4 +72,4 @@
</PixButton>
</div>
</form>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { action } from '@ember/object';
import { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';

const DEFAULT_BADGE_LEVEL = '1';

import Component from '@glimmer/component';

export default class add extends Component {
@service notifications;
@service router;
@service store;

@tracked isSubmitDisabled = true;
@tracked isSubmitting = false;
@tracked selectedTargetProfile;

#notifyOrganizations = true;
#targetProfileBadges = new Map();

get hasExternalJury() {
return this.args.complementaryCertification.hasExternalJury;
}

@action
async onError(errorMessage) {
if (errorMessage) {
this.notifications.error(errorMessage);
}
}

@action
async onSelection(selectedAttachableTargetProfile) {
if (selectedAttachableTargetProfile) {
this.selectedTargetProfile = selectedAttachableTargetProfile;
this.#targetProfileBadges = new Map();
this.isSubmitDisabled = false;
}
}

@action
onReset() {
this.selectedTargetProfile = undefined;
this.#targetProfileBadges = new Map();
this.#notifyOrganizations = true;
this.isSubmitDisabled = true;
this.isSubmitting = false;
}

@action
onBadgeUpdated({ update: { badgeId, fieldName, fieldValue } }) {
this.#updateBadge({ badgeId, fieldName, fieldValue });
}

@action
onNotificationUpdated({ target }) {
this.#notifyOrganizations = target.checked;
}

@action
async onCancel() {
this.router.transitionTo('authenticated.complementary-certifications.complementary-certification.details');
}

@action
async onSubmit(event) {
event.preventDefault();
this.isSubmitting = true;
try {
const complementaryCertification = this.args.complementaryCertification;

const complementaryCertificationBadges = this.store.peekAll('complementary-certification-badge').toArray();

complementaryCertificationBadges.forEach((complementaryCertificationBadge) => {
complementaryCertification.complementaryCertificationBadges.removeObject(complementaryCertificationBadge);
});

this.#targetProfileBadges.forEach((badge, badgeId) => {
const aBadge = this.store.createRecord('complementary-certification-badge', {
complementaryCertification,
badgeId,
level: badge.level ?? DEFAULT_BADGE_LEVEL,
imageUrl: badge['certificate-image'],
label: badge['certificate-label'],
certificateMessage: badge['certificate-message'],
temporaryCertificateMessage: badge['certificate-temporary-message'],
stickerUrl: badge['certificate-sticker'],
minimumEarnedPix: badge['minimum-earned-pix'],
});
complementaryCertification.complementaryCertificationBadges.pushObject(aBadge);
});

await complementaryCertification.save({
adapterOptions: {
attachBadges: true,
targetProfileId: this.args.currentTargetProfile?.id,
notifyOrganizations: this.#notifyOrganizations,
},
});

this.router.transitionTo('authenticated.complementary-certifications.complementary-certification.details');

this.notifications.success(
`Profil cible rattaché à la certification ${complementaryCertification.label} mis à jour avec succès !`,
);
} catch (error) {
console.error({ error });
await this.onError("Une erreur est survenue lors de l'enregistrement du profil cible.");
} finally {
this.isSubmitting = false;
}
}

#updateBadge({ badgeId, fieldName, fieldValue }) {
const currentBadge = this.#targetProfileBadges.get(badgeId);
this.#targetProfileBadges.set(badgeId, {
...currentBadge,
[fieldName]: fieldValue,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@
{{/if}}
{{#if this.hasAccessToAttachNewTargetProfile}}
<div class="complementary-certification-details-target-profile__attach-button">
<PixButtonLink
@route="authenticated.complementary-certifications.complementary-certification.attach-target-profile"
@model={{@currentTargetProfile.id}}
>Rattacher un nouveau profil cible
</PixButtonLink>
{{#if @currentTargetProfile}}
<PixButtonLink
@route="authenticated.complementary-certifications.complementary-certification.attach-target-profile.update"
@model={{@currentTargetProfile.id}}
>Rattacher un nouveau profil cible
</PixButtonLink>
{{else}}
<PixButtonLink
@route="authenticated.complementary-certifications.complementary-certification.attach-target-profile.new"
>Rattacher un profil cible
</PixButtonLink>
{{/if}}
</div>
{{/if}}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default class AttachTargetProfileController extends Controller {
await complementaryCertification.save({
adapterOptions: {
attachBadges: true,
targetProfileId: this.model.currentTargetProfile.id,
targetProfileId: this.model.currentTargetProfile?.id,
notifyOrganizations: this.#notifyOrganizations,
},
});
Expand All @@ -103,6 +103,7 @@ export default class AttachTargetProfileController extends Controller {
`Profil cible rattaché à la certification ${complementaryCertification.label} mis à jour avec succès !`,
);
} catch (error) {
console.error({ error });
await this.onError("Une erreur est survenue lors de l'enregistrement du profil cible.");
} finally {
this.isSubmitting = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import controller from './new.js';

export default controller;
5 changes: 4 additions & 1 deletion admin/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ Router.map(function () {
this.route('list');
this.route('complementary-certification', { path: '/:complementary_certification_id' }, function () {
this.route('details');
this.route('attach-target-profile', { path: '/attach-target-profile/:target_profile_id' });
this.route('attach-target-profile', function () {
this.route('update', { path: '/:target_profile_id' });
this.route('new');
});
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Route from '@ember/routing/route';
import { service } from '@ember/service';

export default class AttachTargetProfileNewRoute extends Route {
@service accessControl;

beforeModel() {
this.accessControl.restrictAccessTo(
['isSuperAdmin'],
'authenticated.complementary-certifications.complementary-certification',
);
}

model(params) {
const complementaryCertification = this.modelFor(
'authenticated.complementary-certifications.complementary-certification',
);

return {
complementaryCertification,
currentTargetProfile: null,
};
}

resetController(controller, isExiting) {
if (isExiting) {
controller.onReset();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ export default class AttachTargetProfileRoute extends Route {
}

model(params) {
const targetProfileId = parseInt(params.target_profile_id);
const complementaryCertification = this.modelFor(
'authenticated.complementary-certifications.complementary-certification',
);

const targetProfileId = parseInt(params.target_profile_id);

return {
complementaryCertification,
currentTargetProfile: complementaryCertification.currentTargetProfiles.find(({ id }) => id === targetProfileId),
currentTargetProfile: complementaryCertification.currentTargetProfiles?.find(({ id }) => id === targetProfileId),
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<ComplementaryCertifications::AttachBadges
@complementaryCertification={{@model.complementaryCertification}}
@currentTargetProfile={{null}}
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<ComplementaryCertifications::AttachBadges
@complementaryCertification={{@model.complementaryCertification}}
@currentTargetProfile={{@model.currentTargetProfile}}
/>

0 comments on commit ab053b5

Please sign in to comment.