From 9846a4e1ae8202c17aa739e8fb305c8ae6af8301 Mon Sep 17 00:00:00 2001 From: Christopher Date: Thu, 18 Feb 2021 23:20:24 +0100 Subject: [PATCH] feat(Alerts): alerts for contacts --- phone/src/apps/contacts/hooks/useAlert.ts | 8 ----- .../apps/contacts/hooks/useContactsService.ts | 10 +++---- resources/client/cl_contacts.ts | 4 +-- resources/server/sv_contacts.ts | 30 +++++++++++++++---- 4 files changed, 31 insertions(+), 21 deletions(-) delete mode 100644 phone/src/apps/contacts/hooks/useAlert.ts diff --git a/phone/src/apps/contacts/hooks/useAlert.ts b/phone/src/apps/contacts/hooks/useAlert.ts deleted file mode 100644 index b7d7ee8..0000000 --- a/phone/src/apps/contacts/hooks/useAlert.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { useRecoilState } from 'recoil'; -import { IContactAlert } from '../../../common/typings/contact'; -import { contactsState } from './state'; - -export const useContactAlert = (): IContactAlert => { - const [alert, setAlert] = useRecoilState(contactsState.contactAlert); - return { alert, setAlert }; -}; diff --git a/phone/src/apps/contacts/hooks/useContactsService.ts b/phone/src/apps/contacts/hooks/useContactsService.ts index 82a985d..c8a3a04 100644 --- a/phone/src/apps/contacts/hooks/useContactsService.ts +++ b/phone/src/apps/contacts/hooks/useContactsService.ts @@ -2,12 +2,12 @@ import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent'; import { useSetRecoilState } from 'recoil'; import { contactsState } from './state'; import { useContacts } from './useContacts'; -import { useContactAlert } from './useAlert'; +import { useSnackbar } from '../../../ui/hooks/useSnackbar'; export const useContactsService = () => { const setContacts = useSetRecoilState(contactsState.contacts); - const setAlert = useSetRecoilState(contactsState.contactAlert); + const { addAlert } = useSnackbar() useNuiEvent('CONTACTS', 'setContacts', setContacts); - useNuiEvent('CONTACTS', 'setAlert', setAlert); - return { useContacts, useContactAlert }; -}; + useNuiEvent('CONTACTS', 'setAlert', addAlert); + return useContacts(); +}; \ No newline at end of file diff --git a/resources/client/cl_contacts.ts b/resources/client/cl_contacts.ts index 0fc8c42..c3cadc9 100644 --- a/resources/client/cl_contacts.ts +++ b/resources/client/cl_contacts.ts @@ -40,12 +40,12 @@ onNet(events.CONTACTS_DELETE_CONTACT_SUCCESS, () => { emitNet(events.CONTACTS_GET_CONTACTS); }); -onNet(events.CONTACTS_ACTION_RESULT, (result: string) => { +onNet(events.CONTACTS_ACTION_RESULT, (alert: any) => { SendNuiMessage( JSON.stringify({ app: 'CONTACTS', method: 'setAlert', - data: result, + data: alert }) ); }); diff --git a/resources/server/sv_contacts.ts b/resources/server/sv_contacts.ts index df03f81..4b26060 100644 --- a/resources/server/sv_contacts.ts +++ b/resources/server/sv_contacts.ts @@ -85,9 +85,15 @@ onNet( const _identifier = getIdentifier(_source); await addContact(_identifier, number, display, avatar); emitNet(events.CONTACTS_ADD_CONTACT_SUCCESS, _source); - emitNet(events.CONTACTS_ACTION_RESULT, _source, 'CONTACT_ADD_SUCCESS'); + emitNet(events.CONTACTS_ACTION_RESULT, _source, { + message: 'CONTACT_ADD_SUCCESS', + type: 'success' + }); } catch (e) { - emitNet(events.CONTACTS_ACTION_RESULT, _source, 'CONTACT_ADD_FAILED'); + emitNet(events.CONTACTS_ACTION_RESULT, _source, { + message: 'CONTACT_ADD_FAILED', + type: 'error' + }); contactsLogger.error(`Failed to add contact, ${e.message}`, { source: _source, }); @@ -106,11 +112,17 @@ onNet(events.CONTACTS_UPDATE_CONTACT, async (contact: Contacts) => { emitNet(events.CONTACTS_UPDATE_CONTACT_SUCCESS, _source); // console.log('ffs, didd it return successful'); - emitNet(events.CONTACTS_ACTION_RESULT, _source, 'CONTACT_UPDATE_SUCCESS'); + emitNet(events.CONTACTS_ACTION_RESULT, _source, { + message: 'CONTACT_UPDATE_SUCCESS', + type: 'success' + }); // console.log('UPDATED CONTACT: ', contact); } catch (e) { const _source = (global as any).source; - emitNet(events.CONTACTS_ACTION_RESULT, _source, 'CONTACT_UPDATE_FAILED'); + emitNet(events.CONTACTS_ACTION_RESULT, _source, { + message: 'CONTACT_UPDATE_FAILED', + type: 'error' + }); contactsLogger.error(`Failed to update contact, ${e.message}`, { source: _source, }); @@ -123,11 +135,17 @@ onNet(events.CONTACTS_DELETE_CONTACT, async (contact: ContactId) => { const _identifier = await getIdentifier(_source); await deleteContact(contact, _identifier); emitNet(events.CONTACTS_DELETE_CONTACT_SUCCESS, _source); - emitNet(events.CONTACTS_ACTION_RESULT, _source, 'CONTACT_DELETE_SUCCESS'); + emitNet(events.CONTACTS_ACTION_RESULT, _source, { + message: 'CONTACT_DELETE_SUCCESS', + type: 'success' + }); } catch (e) { contactsLogger.error(`Failed to delete contact, ${e.message}`, { source: _source, }); - emitNet(events.CONTACTS_ACTION_RESULT, _source, 'CONTACT_DELETE_FAILED'); + emitNet(events.CONTACTS_ACTION_RESULT, _source, { + message: 'CONTACT_DELETE_FAILED', + type: 'error' + }); } });