refactor(contacts/contactalert): Cleaned up code

Reduced redundant code and added switch statement
This commit is contained in:
Taso authored and Taso committed 2020-12-13 00:07:38 -05:00
1 parent 96224eb239
commit 65de9d78a3
3 files changed
+44 -31

No files matched your search

@@ -3,6 +3,11 @@ import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Alert } from '../../../../ui/components/Alert';
import { useContactAlert } from '../../hooks/useAlert';
import { AlertCategory } from '../../../../common/typings/contact';
// Used as a prefix for referencing the locales
// Needed to make code cleaner
const LOCALE_PREFIX = 'APPS_';
export const ContactAlert = () => {
const { t } = useTranslation();
@@ -16,31 +21,31 @@ export const ContactAlert = () => {
setAlert(null);
};
function setMessageAlert(type: AlertCategory, severity: 'success' | 'error') {
setMessage(t(LOCALE_PREFIX + type));
setSeverity(severity);
setOpen(true);
}
useEffect(() => {
if (alert === 'CONTACT_ADD_SUCCESS') {
setMessage(t('APPS_CONTACTS_ADD_SUCCESS'));
setSeverity('success');
setOpen(true);
} else if (alert === 'CONTACT_ADD_FAILED') {
setMessage(t('APPS_CONTACTS_ADD_FAILED'));
setSeverity('error');
setOpen(true);
} else if (alert === 'CONTACT_UPDATE_SUCCESS') {
setMessage(t('APPS_CONTACTS_UPDATE_SUCCESS'));
setSeverity('success');
setOpen(true);
} else if (alert === 'CONTACT_UPDATE_FAILED') {
setMessage(t('APPS_CONTACTS_UPDATE_FAILED'));
setSeverity('error');
setOpen(true);
} else if (alert === 'CONTACT_DELETE_SUCCESS') {
setMessage(t('APPS_CONTACTS_DELETE_SUCCESS'));
setSeverity('success');
setOpen(true);
} else if (alert === 'CONTACT_DELETE_FAILED') {
setMessage(t('APPS_CONTACTS_DELETE_FAILED'));
setSeverity('error');
setOpen(true);
switch (alert) {
case 'CONTACT_ADD_FAILED':
setMessageAlert(alert, 'error');
break;
case 'CONTACT_ADD_SUCCESS':
setMessageAlert(alert, 'success');
break;
case 'CONTACT_DELETE_FAILED':
setMessageAlert(alert, 'error');
break;
case 'CONTACT_DELETE_SUCCESS':
setMessageAlert(alert, 'success');
break;
case 'CONTACT_UPDATE_FAILED':
setMessageAlert(alert, 'error');
break;
case 'CONTACT_UPDATE_SUCCESS':
setMessageAlert(alert, 'success');
}
}, [alert]);
+9 -1
View File
@@ -1,4 +1,12 @@
export type AlertCategory =
| 'CONTACT_ADD_SUCCESS'
| 'CONTACT_ADD_FAILED'
| 'CONTACT_UPDATE_SUCCESS'
| 'CONTACT_UPDATE_FAILED'
| 'CONTACT_DELETE_SUCCESS'
| 'CONTACT_DELETE_FAILED';
export interface IContactAlert {
alert: string;
alert: AlertCategory;
setAlert: (type: string) => void;
}
+6 -6
View File
@@ -64,11 +64,11 @@
"APPS_MESSAGES_MESSAGE_GROUP_CREATE_FAILED": "Failed to create message group",
"APPS_MESSAGES_MESSAGE_GROUP_CREATE_MINE": "You cannot add yourself!",
"APPS_CONTACTS_ADD_SUCCESS": "Successfully created contact!",
"APPS_CONTACTS_ADD_FAILED": "Failed to created contact!",
"APPS_CONTACTS_UPDATE_SUCCESS": "Successfully updated contact!",
"APPS_CONTACTS_UPDATE_FAILED": "Failed to update contact!",
"APPS_CONTACTS_DELETE_SUCCESS": "Successfully deleted contact!",
"APPS_CONTACTS_DELETE_FAILED": "Failed to delete contact!"
"APPS_CONTACT_ADD_SUCCESS": "Successfully created contact!",
"APPS_CONTACT_ADD_FAILED": "Failed to created contact!",
"APPS_CONTACT_UPDATE_SUCCESS": "Successfully updated contact!",
"APPS_CONTACT_UPDATE_FAILED": "Failed to update contact!",
"APPS_CONTACT_DELETE_SUCCESS": "Successfully deleted contact!",
"APPS_CONTACT_DELETE_FAILED": "Failed to delete contact!"
}
}