Skip to content

Commit

Permalink
feat: update notifier to enable or not the notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
ReidyT committed May 31, 2024
1 parent c6a0523 commit 23dd8a9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 19 deletions.
97 changes: 81 additions & 16 deletions src/config/notifier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { toast } from 'react-toastify';

import { Notifier, routines } from '@graasp/query-client';
import {
EnableNotifications,
NotificationStatus,
Notifier,
NotifierOptions,
isNotificationEnabled,
routines,
} from '@graasp/query-client';
import buildI18n, {
FAILURE_MESSAGES,
REQUEST_MESSAGES,
Expand Down Expand Up @@ -74,13 +81,73 @@ const {
deleteItemThumbnailRoutine,
} = routines;

const notifier: Notifier = ({
type,
payload,
const notify = ({
enableNotifications,
notificationStatus,
onEnabled,
onDisabled,
}: {
type: string;
payload?: Payload;
enableNotifications: EnableNotifications | undefined;
notificationStatus: NotificationStatus;
onEnabled: () => void;
onDisabled: () => void;
}) => {
if (isNotificationEnabled(enableNotifications, notificationStatus)) {
onEnabled();
} else {
onDisabled();
}
};

const notifyError = (
message: string,
enableNotifications: EnableNotifications | undefined,
) => {
notify({
enableNotifications,
notificationStatus: NotificationStatus.ERROR,
onEnabled: () => toast.error(message),
onDisabled: () => console.error(message),
});
};

const notifySuccess = (
message: string,
enableNotifications: EnableNotifications | undefined,
) => {
notify({
enableNotifications,
notificationStatus: NotificationStatus.SUCCESS,
onEnabled: () => toast.success(message),
// eslint-disable-next-line no-console
onDisabled: () => console.log(message),
});
};

const notifyInfo = (
message: string,
enableNotifications: EnableNotifications | undefined,
) => {
notify({
enableNotifications,
notificationStatus: NotificationStatus.INFO,
onEnabled: () => toast.info(message),
// eslint-disable-next-line no-console
onDisabled: () => console.log(message),
});
};

const notifier: Notifier = (
{
type,
payload,
}: {
type: string;
payload?: Payload;
},
options?: NotifierOptions,
) => {
const enableNotifications = options?.enableNotifications;
let message = null;
switch (type) {
// error messages
Expand Down Expand Up @@ -162,37 +229,35 @@ const notifier: Notifier = ({

// progress messages
// todo: this might be handled differently
case uploadFileRoutine.REQUEST: {
toast.info(i18n.t(REQUEST_MESSAGES.UPLOAD_FILES));
break;
}
case uploadFileRoutine.REQUEST:
case uploadItemThumbnailRoutine.REQUEST: {
toast.info(i18n.t(REQUEST_MESSAGES.UPLOAD_FILES));
notifyInfo(i18n.t(REQUEST_MESSAGES.UPLOAD_FILES), enableNotifications);
break;
}
case importZipRoutine.REQUEST: {
toast.info(i18n.t(REQUEST_MESSAGES.IMPORT_ZIP));
notifyInfo(i18n.t(REQUEST_MESSAGES.IMPORT_ZIP), enableNotifications);
break;
}
case importH5PRoutine.REQUEST: {
toast.info(i18n.t(REQUEST_MESSAGES.IMPORT_H5P));
notifyInfo(i18n.t(REQUEST_MESSAGES.IMPORT_H5P), enableNotifications);
break;
}
default:
}
// error notification
if (payload?.error) {
if (message) {
toast.error(i18n.t(message));
notifyError(i18n.t(message), enableNotifications);
} else {
toast.error(payload.error.toString());
notifyError(payload.error.toString(), enableNotifications);
}
}
// success notification
else if (message) {
// TODO: enable if not websockets
// allow resend invitation
toast.success(i18n.t(message));
notifySuccess(i18n.t(message), enableNotifications);
}
};

export default notifier;
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1752,8 +1752,8 @@ __metadata:
linkType: hard

"@graasp/query-client@github:graasp/graasp-query-client#allow-disable-notifications":
version: 3.9.0
resolution: "@graasp/query-client@https://github.com/graasp/graasp-query-client.git#commit=885ad1fb904649c94e7bfd57b24e19f894d5c043"
version: 3.10.0
resolution: "@graasp/query-client@https://github.com/graasp/graasp-query-client.git#commit=8773660c4a216751dbbcf7ef8c543651880bd8e4"
dependencies:
"@tanstack/react-query": "npm:4.36.1"
"@tanstack/react-query-devtools": "npm:4.36.1"
Expand All @@ -1764,7 +1764,7 @@ __metadata:
"@graasp/sdk": ^4.0.0
"@graasp/translations": ^1.23.0
react: ^18.0.0
checksum: 10/38dde21ae0582481f055ef6bde1277967f7b749c10e7b3b141b84fa139cdd5bd530ef013a6781bb36d5cbee3099c4a79b237a02ad9d04baa79c492515b27fd25
checksum: 10/62a5e02524efbbdaa0b06c2ed3000555e02b58baeb861c6573859be16613b9236c4274353f108f26e819104dc2df8bb6c121cfad6c23e51f22326e3bf2c318df
languageName: node
linkType: hard

Expand Down

0 comments on commit 23dd8a9

Please sign in to comment.