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 { useTranslation } from 'react-i18next';
import { Alert } from '../../../../ui/components/Alert'; import { Alert } from '../../../../ui/components/Alert';
import { useContactAlert } from '../../hooks/useAlert'; 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 = () => { export const ContactAlert = () => {
const { t } = useTranslation(); const { t } = useTranslation();
@@ -16,31 +21,31 @@ export const ContactAlert = () => {
setAlert(null); setAlert(null);
}; };
function setMessageAlert(type: AlertCategory, severity: 'success' | 'error') {
setMessage(t(LOCALE_PREFIX + type));
setSeverity(severity);
setOpen(true);
}
useEffect(() => { useEffect(() => {
if (alert === 'CONTACT_ADD_SUCCESS') { switch (alert) {
setMessage(t('APPS_CONTACTS_ADD_SUCCESS')); case 'CONTACT_ADD_FAILED':
setSeverity('success'); setMessageAlert(alert, 'error');
setOpen(true); break;
} else if (alert === 'CONTACT_ADD_FAILED') { case 'CONTACT_ADD_SUCCESS':
setMessage(t('APPS_CONTACTS_ADD_FAILED')); setMessageAlert(alert, 'success');
setSeverity('error'); break;
setOpen(true); case 'CONTACT_DELETE_FAILED':
} else if (alert === 'CONTACT_UPDATE_SUCCESS') { setMessageAlert(alert, 'error');
setMessage(t('APPS_CONTACTS_UPDATE_SUCCESS')); break;
setSeverity('success'); case 'CONTACT_DELETE_SUCCESS':
setOpen(true); setMessageAlert(alert, 'success');
} else if (alert === 'CONTACT_UPDATE_FAILED') { break;
setMessage(t('APPS_CONTACTS_UPDATE_FAILED')); case 'CONTACT_UPDATE_FAILED':
setSeverity('error'); setMessageAlert(alert, 'error');
setOpen(true); break;
} else if (alert === 'CONTACT_DELETE_SUCCESS') { case 'CONTACT_UPDATE_SUCCESS':
setMessage(t('APPS_CONTACTS_DELETE_SUCCESS')); setMessageAlert(alert, 'success');
setSeverity('success');
setOpen(true);
} else if (alert === 'CONTACT_DELETE_FAILED') {
setMessage(t('APPS_CONTACTS_DELETE_FAILED'));
setSeverity('error');
setOpen(true);
} }
}, [alert]); }, [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 { export interface IContactAlert {
alert: string; alert: AlertCategory;
setAlert: (type: string) => void; 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_FAILED": "Failed to create message group",
"APPS_MESSAGES_MESSAGE_GROUP_CREATE_MINE": "You cannot add yourself!", "APPS_MESSAGES_MESSAGE_GROUP_CREATE_MINE": "You cannot add yourself!",
"APPS_CONTACTS_ADD_SUCCESS": "Successfully created contact!", "APPS_CONTACT_ADD_SUCCESS": "Successfully created contact!",
"APPS_CONTACTS_ADD_FAILED": "Failed to created contact!", "APPS_CONTACT_ADD_FAILED": "Failed to created contact!",
"APPS_CONTACTS_UPDATE_SUCCESS": "Successfully updated contact!", "APPS_CONTACT_UPDATE_SUCCESS": "Successfully updated contact!",
"APPS_CONTACTS_UPDATE_FAILED": "Failed to update contact!", "APPS_CONTACT_UPDATE_FAILED": "Failed to update contact!",
"APPS_CONTACTS_DELETE_SUCCESS": "Successfully deleted contact!", "APPS_CONTACT_DELETE_SUCCESS": "Successfully deleted contact!",
"APPS_CONTACTS_DELETE_FAILED": "Failed to delete contact!" "APPS_CONTACT_DELETE_FAILED": "Failed to delete contact!"
} }
} }