From b4258718d8d737bca17b5057766b8f96a1692db6 Mon Sep 17 00:00:00 2001 From: Christopher Date: Sat, 13 Feb 2021 13:48:05 +0100 Subject: [PATCH] fix alert provider | remove alert from notes and contact --- phone/src/Phone.tsx | 21 +++++- .../apps/contacts/components/ContactsApp.tsx | 2 - .../components/alert/ContactAlert.tsx | 65 ------------------- .../apps/example/components/ExampleApp.tsx | 4 +- phone/src/apps/notes/NotesApp.tsx | 8 ++- phone/src/apps/notes/alert/NotesAlert.tsx | 62 ------------------ phone/src/apps/notes/hooks/useNoteAlert.ts | 8 --- phone/src/apps/notes/hooks/useNotesService.ts | 11 ++-- phone/src/index.tsx | 5 +- phone/src/os/nui-events/hooks/useNuiEvent.ts | 1 + phone/src/ui/components/SnackbarProvider.tsx | 41 ++++++------ phone/src/ui/hooks/useSnackbar.ts | 9 ++- phone/src/ui/interface/InterfaceUI.ts | 5 -- 13 files changed, 67 insertions(+), 175 deletions(-) delete mode 100644 phone/src/apps/contacts/components/alert/ContactAlert.tsx delete mode 100644 phone/src/apps/notes/alert/NotesAlert.tsx delete mode 100644 phone/src/apps/notes/hooks/useNoteAlert.ts diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index bbca10f..dcd93b1 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -3,6 +3,7 @@ import './Phone.css'; import './i18n'; import { Route, useHistory } from 'react-router-dom'; import { CallModal } from './modal/components/CallModal'; +import { Alert } from './ui/components/Alert'; import { HomeApp } from './apps/home/components/Home'; import { createMuiTheme, ThemeProvider } from '@material-ui/core'; import { useInitKeyboard } from './os/keyboard/hooks/useKeyboard'; @@ -32,7 +33,8 @@ import { useModal } from './modal/hooks/useModal'; import { useDialService } from './apps/dialer/hooks/useDialService'; import InjectDebugData from './os/debug/InjectDebugData'; import { useQuickAccess } from './os/notifications/hooks/useQuickAccess'; -import SnackbarProvider from './ui/components/SnackbarProvider'; +import { useSnackbar } from './ui/hooks/useSnackbar'; +import { useTranslation } from 'react-i18next'; InjectDebugData([ { @@ -96,6 +98,9 @@ function Phone() { usePhotoService(); useCallService(); useDialService(); + const { t } = useTranslation() + + const { alert } = useSnackbar(); const { modal } = useModal(); // the calling modal @@ -152,7 +157,6 @@ function Phone() { }))} />
- {modal ? ( ) : ( @@ -163,7 +167,18 @@ function Phone() { ))} )} - + {alert ? ( +
+ + {t("APPS_"+alert.message)} + +
+ ) : null}
diff --git a/phone/src/apps/contacts/components/ContactsApp.tsx b/phone/src/apps/contacts/components/ContactsApp.tsx index 506be6a..9080140 100644 --- a/phone/src/apps/contacts/components/ContactsApp.tsx +++ b/phone/src/apps/contacts/components/ContactsApp.tsx @@ -10,7 +10,6 @@ import PersonAddIcon from '@material-ui/icons/PersonAdd'; import Fab from '@material-ui/core/Fab'; import { InfoModal } from './modal/InfoModal'; import { useContactDetail } from '../hooks/useContactDetail'; -import { ContactAlert } from './alert/ContactAlert'; import useStyles from './styles.contacts'; import InjectDebugData from '../../../os/debug/InjectDebugData'; @@ -59,7 +58,6 @@ export const ContactsApp = () => { > - ); diff --git a/phone/src/apps/contacts/components/alert/ContactAlert.tsx b/phone/src/apps/contacts/components/alert/ContactAlert.tsx deleted file mode 100644 index a0b467c..0000000 --- a/phone/src/apps/contacts/components/alert/ContactAlert.tsx +++ /dev/null @@ -1,65 +0,0 @@ -import { Snackbar } from '@material-ui/core'; -import React, { useState, useEffect, useCallback } 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(); - const { alert, setAlert } = useContactAlert(); - const [open, setOpen] = useState(false); - const [message, setMessage] = useState(null); - const [severity, setSeverity] = useState(''); - - const _handleClose = () => { - setOpen(false); - setAlert(null); - }; - - const setMessageAlert = useCallback((type: AlertCategory, severity: 'success' | 'error') => { - setMessage(t(LOCALE_PREFIX + type)); - setSeverity(severity); - setOpen(true); - }, [t]); - - useEffect(() => { - 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'); - break; - } - }, [alert, setMessageAlert]); - - return ( - - - {message} - - - ); -}; diff --git a/phone/src/apps/example/components/ExampleApp.tsx b/phone/src/apps/example/components/ExampleApp.tsx index 2889c9e..a5c3dc1 100644 --- a/phone/src/apps/example/components/ExampleApp.tsx +++ b/phone/src/apps/example/components/ExampleApp.tsx @@ -5,14 +5,16 @@ import { AppTitle } from '../../../ui/components/AppTitle'; import { AppContent } from '../../../ui/components/AppContent'; import { useApp } from '../../../os/apps/hooks/useApps'; import { useExample } from '../hooks/useExample'; +import { useSnackbar } from '../../../ui/hooks/useSnackbar'; +import SnackbarProvider from '../../../ui/components/SnackbarProvider'; export const ExampleApp = () => { // calling the example hook, and we assign the value to a variable + const exampleString = useExample(); const example = useApp('EXAMPLE'); return ( -

This is an example

{/* Here we are using the value in a h3 tag */} diff --git a/phone/src/apps/notes/NotesApp.tsx b/phone/src/apps/notes/NotesApp.tsx index 8875d82..36b43a5 100644 --- a/phone/src/apps/notes/NotesApp.tsx +++ b/phone/src/apps/notes/NotesApp.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { AppWrapper } from '../../ui/components/AppWrapper'; import { AppContent } from '../../ui/components/AppContent'; import { AppTitle } from '../../ui/components/AppTitle'; @@ -15,7 +15,8 @@ import { useNoteDetail } from './hooks/useNoteDetail'; import useStyles from './notes.styles'; import InjectDebugData from '../../os/debug/InjectDebugData'; -import { NotesAlert } from './alert/NotesAlert'; +import { useSnackbar } from '../../ui/hooks/useSnackbar'; +import { useTranslation } from 'react-i18next'; InjectDebugData([ { @@ -38,9 +39,11 @@ InjectDebugData([ export const NotesApp = () => { const { setNoteModal } = useNoteModal(); + const { t } = useTranslation() const { detail, setDetail } = useNoteDetail(); const { notes } = useNotes(); const notesApp = useApp('NOTES'); + const { addAlert } = useSnackbar() const classes = useStyles(); @@ -66,7 +69,6 @@ export const NotesApp = () => { > -
); }; diff --git a/phone/src/apps/notes/alert/NotesAlert.tsx b/phone/src/apps/notes/alert/NotesAlert.tsx deleted file mode 100644 index c760082..0000000 --- a/phone/src/apps/notes/alert/NotesAlert.tsx +++ /dev/null @@ -1,62 +0,0 @@ -import React, { useState, useEffect, useCallback } from 'react' -import { Snackbar } from '@material-ui/core'; -import { Alert } from '../../../ui/components/Alert'; -import { AlertCategory } from '../../../common/typings/notes'; -import { useTranslation } from 'react-i18next'; -import { useNotesAlert } from '../hooks/useNoteAlert'; - -const LOCALE_PREFIX = 'APPS_'; - -export const NotesAlert = () => { - const { t } = useTranslation(); - const [open, setOpen] = useState(false); - const { alert, setAlert } = useNotesAlert(); - const [message, setMessage] = useState(null); - const [severity, setSeverity] = useState(''); - - const _handleClose = () => { - setOpen(false) - setAlert(null) - } - - const setMessageAlert = useCallback((type: AlertCategory, severity: 'success' | 'error') => { - setMessage(t(LOCALE_PREFIX + type)); - setSeverity(severity) - setOpen(true) - }, [t]) - - useEffect(() => { - switch (alert) { - case 'NOTES_ADD_SUCCESS': - setMessageAlert(alert, 'success'); - break; - case 'NOTES_ADD_FAILED': - setMessageAlert(alert, 'error'); - break; - case 'NOTES_UPDATE_SUCCESS': - setMessageAlert(alert, 'success'); - break; - case 'NOTES_UPDATE_FAILED': - setMessageAlert(alert, 'error'); - break; - case 'NOTES_DELETE_SUCCESS': - setMessageAlert(alert, 'success'); - break; - case 'NOTES_DELETE_FAILED': - setMessageAlert(alert, 'error') - } - }, [alert, setMessageAlert]) - - return ( - - - {message} - - - ) -} diff --git a/phone/src/apps/notes/hooks/useNoteAlert.ts b/phone/src/apps/notes/hooks/useNoteAlert.ts deleted file mode 100644 index ba081b2..0000000 --- a/phone/src/apps/notes/hooks/useNoteAlert.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { useRecoilState } from 'recoil'; -import { INotesAlert } from '../../../common/typings/notes'; -import { noteStates } from './state'; - -export const useNotesAlert = (): INotesAlert => { - const [alert, setAlert] = useRecoilState(noteStates.alert) - return { alert, setAlert } -} \ No newline at end of file diff --git a/phone/src/apps/notes/hooks/useNotesService.ts b/phone/src/apps/notes/hooks/useNotesService.ts index c4356c4..dfe627b 100644 --- a/phone/src/apps/notes/hooks/useNotesService.ts +++ b/phone/src/apps/notes/hooks/useNotesService.ts @@ -1,13 +1,16 @@ + +import { createContext, useCallback } from 'react'; import { useSetRecoilState } from 'recoil'; import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent'; import { useNotes } from './useNotes'; import { noteStates } from './state'; -import { useNotesAlert } from './useNoteAlert'; +import { useSnackbar } from '../../../ui/hooks/useSnackbar'; export const useNotesService = () => { + const { addAlert } = useSnackbar() const setNotes = useSetRecoilState(noteStates.noteItems); - const setAlert = useSetRecoilState(noteStates.alert); + useNuiEvent('NOTES', 'setNotes', setNotes); - useNuiEvent('NOTES', 'setAlert', setAlert) - return { useNotes, useNotesAlert }; + useNuiEvent('NOTES', 'setAlert', addAlert) + return useNotes(); }; diff --git a/phone/src/index.tsx b/phone/src/index.tsx index 8d5a152..8d1fcef 100644 --- a/phone/src/index.tsx +++ b/phone/src/index.tsx @@ -10,6 +10,7 @@ import Phone from './Phone'; import TwitterNotification from './apps/twitter/components/notification/TwitterNotification'; import { BankNotification } from './apps/bank/components/notification/BankNotification'; import PhoneConfig from './config/default.json'; +import SnackbarProvider from './ui/components/SnackbarProvider'; // Enable Sentry when config setting is true if (PhoneConfig.SentryErrorMetrics) { @@ -29,7 +30,9 @@ ReactDOM.render( - + + + , document.getElementById('root') diff --git a/phone/src/os/nui-events/hooks/useNuiEvent.ts b/phone/src/os/nui-events/hooks/useNuiEvent.ts index 5897bf4..38521f4 100644 --- a/phone/src/os/nui-events/hooks/useNuiEvent.ts +++ b/phone/src/os/nui-events/hooks/useNuiEvent.ts @@ -23,6 +23,7 @@ export const useNuiEvent = ( options: IOptions = {}, currentState?: Record ) => { + const savedHandler: MutableRefObject = useRef(); // Destructure passed options const { capture, passive, once } = options; diff --git a/phone/src/ui/components/SnackbarProvider.tsx b/phone/src/ui/components/SnackbarProvider.tsx index d25e301..20d6704 100644 --- a/phone/src/ui/components/SnackbarProvider.tsx +++ b/phone/src/ui/components/SnackbarProvider.tsx @@ -1,8 +1,8 @@ -import React, { createContext, useState, useEffect, useMemo, useCallback } from 'react'; +import React, { createContext, useState, useEffect, useRef, useMemo, useCallback } from 'react'; import Alert from './Alert'; import { makeStyles } from '@material-ui/core/styles'; -import { Snackbar } from '@material-ui/core'; -import { ISnackbar } from '../interface/InterfaceUI'; +import { useTranslation } from 'react-i18next'; +import { IAlert } from '../hooks/useSnackbar'; const useStyles = makeStyles(() => ({ alert: { @@ -10,39 +10,40 @@ const useStyles = makeStyles(() => ({ display: 'flex', alignItems: 'center', justifyContent: 'center' + }, + hidden: { + display: 'none' } })) -export const SnackbarContext = createContext(null); -const AUTO_HIDE = 2000; +export const SnackbarContext = createContext({ addAlert: (a: IAlert) => {} }); function SnackbarProvider({ children }) { - const [alerts, setAlerts] = useState([]); const classes = useStyles(); - - const alertIdx = alerts.join(','); + const { t } = useTranslation() + const [alert, setAlert] = useState(null); + const timer = useRef(null); useEffect(() => { - const timer = setTimeout(() => setAlerts((alert) => alerts.slice(0, alerts.length - 1)), AUTO_HIDE); - return () => clearTimeout(timer); - }, [alertIdx]) + if (alert) { + clearTimeout(timer.current) + timer.current = setTimeout(() => { + setAlert(null) + }, 2500); + } + return () => clearTimeout(timer.current) + }, [alert]); - const addAlert = useCallback((message, type) => setAlerts((alerts) => [{message, type}, ...alerts]), []); - const value = useMemo(() => ({ addAlert }), [addAlert]); + const addAlert = (value) => setAlert(value); + + const value = { addAlert, alert } return ( <> {children} - {alerts.map((alert: ISnackbar) => ( -
- - {alert.message} - -
- ))}
) diff --git a/phone/src/ui/hooks/useSnackbar.ts b/phone/src/ui/hooks/useSnackbar.ts index d072f59..7433cc6 100644 --- a/phone/src/ui/hooks/useSnackbar.ts +++ b/phone/src/ui/hooks/useSnackbar.ts @@ -1,8 +1,15 @@ import { useContext } from 'react'; import { SnackbarContext } from '../components/SnackbarProvider'; + +export interface IAlert { + message: string; + type: 'success' | 'error' +} + interface ISnackBar { - addAlert: (message: string, type: 'success' | 'error') => void; + addAlert: ({ message, type}: IAlert) => void; + alert: IAlert | null; } export const useSnackbar = (): ISnackBar => useContext(SnackbarContext); \ No newline at end of file diff --git a/phone/src/ui/interface/InterfaceUI.ts b/phone/src/ui/interface/InterfaceUI.ts index c6dbc42..5f655bd 100644 --- a/phone/src/ui/interface/InterfaceUI.ts +++ b/phone/src/ui/interface/InterfaceUI.ts @@ -12,9 +12,4 @@ export interface AppWrapperTypes { children?: JSX.Element | JSX.Element[]; style?: CSSProperties; handleClickAway?: (...args: any[]) => void; -} - -export interface ISnackbar { - message: string; - type: 'success' | 'error' } \ No newline at end of file