Skip to content

Commit

Permalink
✨ admin: use Pix Toast on cross team pages
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiaPena committed Nov 18, 2024
1 parent 9f53611 commit 6bfa836
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 36 deletions.
20 changes: 11 additions & 9 deletions admin/app/components/administration/common/learning-content.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@ import Component from '@glimmer/component';
import AdministrationBlockLayout from '../block-layout';

export default class LearningContent extends Component {
@service notifications;
@service pixToast;
@service store;
@service intl;

@action
async refreshLearningContent() {
try {
await this.store.adapterFor('learning-content-cache').refreshCacheEntries();
this.notifications.success('La demande de rechargement du cache a bien été prise en compte.');
} catch (err) {
this.pixToast.sendSuccessNotification({
message: 'La demande de rechargement du cache a bien été prise en compte.',
});
} catch (_) {
const genericErrorMessage = this.intl.t('common.notifications.generic-error');
this.notifications.error(genericErrorMessage);
this.pixToast.sendErrorNotification({ message: genericErrorMessage });
}
}

@action
async createLearningContentReleaseAndRefreshCache() {
try {
await this.store.adapterFor('learning-content-cache').createLearningContentReleaseAndRefreshCache();
this.notifications.success(
'La création de la version du référentiel et le rechargement du cache a bien été prise en compte.',
);
} catch (err) {
this.notifications.error('Une erreur est survenue.');
this.pixToast.sendSuccessNotification({
message: 'La création de la version du référentiel et le rechargement du cache a bien été prise en compte.',
});
} catch (_) {
this.pixToast.sendErrorNotification({ message: 'Une erreur est survenue.' });
}
}
<template>
Expand Down
8 changes: 4 additions & 4 deletions admin/app/components/common/tubes-selection.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Areas from './tubes-selection/areas';
const MAX_TUBE_LEVEL = 8;

export default class TubesSelection extends Component {
@service notifications;
@service pixToast;

@tracked selectedFrameworkIds = [];

Expand Down Expand Up @@ -173,9 +173,9 @@ export default class TubesSelection extends Component {
}

this._triggerOnChange();
this.notifications.success('Fichier bien importé.');
} catch (e) {
this.notifications.error(e.message);
this.pixToast.sendSuccessNotification({ message: 'Fichier bien importé.' });
} catch (error) {
this.pixToast.sendErrorNotification({ message: error.message });
}
}

Expand Down
9 changes: 5 additions & 4 deletions admin/app/components/stages/update-stage.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import isInteger from 'lodash/isInteger';
import StageLevelSelect from './stage-level-select';

export default class UpdateStage extends Component {
@service notifications;
@service pixToast;

@tracked threshold;
@tracked level;
Expand Down Expand Up @@ -47,11 +47,12 @@ export default class UpdateStage extends Component {
model.prescriberDescription = this.prescriberDescription ? this.prescriberDescription.trim() : null;
try {
await this.args.onUpdate();
await this.notifications.success('Les modifications ont bien été enregistrées.');
await this.pixToast.sendSuccessNotification({ message: 'Les modifications ont bien été enregistrées.' });
this.args.toggleEditMode();
} catch (e) {
} catch (error) {
model.rollbackAttributes();
this.notifications.error(e.errors?.[0]?.detail ?? 'Une erreur est survenue.');
const message = error.errors?.[0]?.detail ?? 'Une erreur est survenue.';
this.pixToast.sendErrorNotification({ message });
}
}

Expand Down
24 changes: 13 additions & 11 deletions admin/app/components/target-profiles/target-profile.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Copy from './modal/copy';
import PdfParametersModal from './pdf-parameters-modal';

export default class TargetProfile extends Component {
@service notifications;
@service pixToast;
@service router;
@service store;
@service intl;
Expand Down Expand Up @@ -79,7 +79,7 @@ export default class TargetProfile extends Component {
try {
await adapter.outdate(this.args.model.id);
this.args.model.reload();
return this.notifications.success('Profil cible marqué comme obsolète.');
return this.pixToast.sendSuccessNotification({ message: 'Profil cible marqué comme obsolète.' });
} catch (responseError) {
this._handleResponseError(responseError);
}
Expand All @@ -92,23 +92,23 @@ export default class TargetProfile extends Component {
this.args.model.isSimplifiedAccess = true;
await this.args.model.save({ adapterOptions: { markTargetProfileAsSimplifiedAccess: true } });

this.notifications.success('Ce profil cible a bien été marqué comme accès simplifié.');
this.pixToast.sendSuccessNotification({ message: 'Ce profil cible a bien été marqué comme accès simplifié.' });
} catch (responseError) {
const genericErrorMessage = this.intl.t('common.notifications.generic-error');
this.notifications.error(genericErrorMessage);
this.pixToast.sendErrorNotification({ message: genericErrorMessage });
}
}

_handleResponseError({ errors }) {
if (!errors) {
return this.notifications.error('Une erreur est survenue.');
return this.pixToast.sendErrorNotification({ message: 'Une erreur est survenue.' });
}
errors.forEach((error) => {
if (['404', '412'].includes(error.status)) {
this.notifications.error(error.detail);
this.pixToast.sendErrorNotification({ message: error.detail });
}
if (error.status === '400') {
this.notifications.error('Une erreur est survenue.');
this.pixToast.sendErrorNotification({ message: 'Une erreur est survenue.' });
}
});
}
Expand All @@ -123,7 +123,7 @@ export default class TargetProfile extends Component {
const token = this.session.data.authenticated.access_token;
await this.fileSaver.save({ url, fileName, token });
} catch (error) {
this.notifications.error(error.message, { autoClear: false });
this.pixToast.sendErrorNotification({ message: error.message });
}
}

Expand All @@ -136,7 +136,7 @@ export default class TargetProfile extends Component {
const token = this.session.data.authenticated.access_token;
await this.fileSaver.save({ url, fileName, token });
} catch (error) {
this.notifications.error(error.message, { autoClear: false });
this.pixToast.sendErrorNotification({ message: error.message });
}
}

Expand All @@ -146,11 +146,13 @@ export default class TargetProfile extends Component {
const adapter = this.store.adapterFor('target-profile');
const newTargetProfileId = await adapter.copy(this.args.model.id);
this.router.transitionTo('authenticated.target-profiles.target-profile', newTargetProfileId);
this.notifications.success(this.intl.t('pages.target-profiles.copy.notifications.success'));
this.pixToast.sendSuccessNotification({
message: this.intl.t('pages.target-profiles.copy.notifications.success'),
});
this.showCopyModal = false;
} catch (error) {
error.errors.forEach((apiError) => {
this.notifications.error(apiError.detail);
this.pixToast.sendErrorNotification({ message: apiError.detail });
});
}
}
Expand Down
8 changes: 5 additions & 3 deletions admin/app/services/error-notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import every from 'lodash/every';
import isEmpty from 'lodash/isEmpty';

export default class ErrorNotifierService extends Service {
@service notifications;
@service pixToast;

notify(anError) {
if (_isJSONAPIError(anError)) {
anError.errors.forEach((e) => this.notifications.error(new Error(`${e.title} : ${e.detail}`)));
anError.errors.forEach((e) =>
this.pixToast.sendErrorNotification({ message: new Error(`${e.title} : ${e.detail}`) }),
);
} else {
this.notifications.error(anError);
this.pixToast.sendErrorNotification({ message: anError });
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions admin/app/services/error-response-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@ const I18N_KEYS_BY_ERROR_CODE = {
};

export default class ErrorResponseHandlerService extends Service {
@service notifications;
@service pixToast;
@service intl;

notify(errorResponse, customErrorMessageByStatus) {
if (!_isJSONAPIError(errorResponse)) {
this.notifications.error(errorResponse);
this.pixToast.sendErrorNotification({ message: errorResponse });
return;
}

const { errors } = errorResponse;
if (!errors) {
this.notifications.error(ERROR_MESSAGES_BY_STATUS.DEFAULT);
this.pixToast.sendErrorNotification({ message: ERROR_MESSAGES_BY_STATUS.DEFAULT });
return;
}

errors.forEach((error) => {
const messageForCode = this._getErrorMessageForErrorCode(error.code, error.meta);
if (messageForCode) {
this.notifications.error(messageForCode);
this.pixToast.sendErrorNotification({ message: messageForCode });
return;
}

const message = _getErrorMessageForHttpStatus(error.status, customErrorMessageByStatus);
this.notifications.error(message);
this.pixToast.sendErrorNotification({ message });
});
}

Expand Down

0 comments on commit 6bfa836

Please sign in to comment.