Merge pull request #87 from project-error/feat/snackbar-provider

Feat/snackbar provider + using it in notes
This commit is contained in:
Christopher Gjelten authored and GitHub committed 2021-02-14 19:00:47 +01:00
commit 7420a20c7e
15 files changed
+93 -152

No files matched your search

+18
View File
@@ -3,6 +3,7 @@ import './Phone.css';
import './i18n'; import './i18n';
import { Route } from 'react-router-dom'; import { Route } from 'react-router-dom';
import { CallModal } from './modal/components/CallModal'; import { CallModal } from './modal/components/CallModal';
import { Alert } from './ui/components/Alert';
import { HomeApp } from './apps/home/components/Home'; import { HomeApp } from './apps/home/components/Home';
import { createMuiTheme, ThemeProvider } from '@material-ui/core'; import { createMuiTheme, ThemeProvider } from '@material-ui/core';
import { useInitKeyboard } from './os/keyboard/hooks/useKeyboard'; import { useInitKeyboard } from './os/keyboard/hooks/useKeyboard';
@@ -32,6 +33,8 @@ import { useModal } from './modal/hooks/useModal';
import { useDialService } from './apps/dialer/hooks/useDialService'; import { useDialService } from './apps/dialer/hooks/useDialService';
import InjectDebugData from './os/debug/InjectDebugData'; import InjectDebugData from './os/debug/InjectDebugData';
import { useQuickAccess } from './os/notifications/hooks/useQuickAccess'; import { useQuickAccess } from './os/notifications/hooks/useQuickAccess';
import { useSnackbar } from './ui/hooks/useSnackbar';
import { useTranslation } from 'react-i18next';
function Phone() { function Phone() {
const quickAccess = useQuickAccess(); const quickAccess = useQuickAccess();
@@ -50,6 +53,9 @@ function Phone() {
usePhotoService(); usePhotoService();
useCallService(); useCallService();
useDialService(); useDialService();
const { t } = useTranslation()
const { alert } = useSnackbar();
const { modal } = useModal(); // the calling modal const { modal } = useModal(); // the calling modal
@@ -116,6 +122,18 @@ function Phone() {
))} ))}
</> </>
)} )}
{alert ? (
<div style={{
marginTop: '-100px',
display: 'flex',
justifyContent: 'center',
alignItems: 'center'
}}>
<Alert severity={alert.type} variant="filled">
{t("APPS_"+alert.message)}
</Alert>
</div>
) : null}
</div> </div>
<Navigation /> <Navigation />
</> </>
@@ -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 (
<Snackbar
style={{ width: '80%', position: 'absolute', bottom: '10%' }}
autoHideDuration={6000}
open={open}
onClose={_handleClose}
>
<Alert severity={severity} variant='filled'>
{message}
</Alert>
</Snackbar>
);
};
@@ -5,14 +5,16 @@ import { AppTitle } from '../../../ui/components/AppTitle';
import { AppContent } from '../../../ui/components/AppContent'; import { AppContent } from '../../../ui/components/AppContent';
import { useApp } from '../../../os/apps/hooks/useApps'; import { useApp } from '../../../os/apps/hooks/useApps';
import { useExample } from '../hooks/useExample'; import { useExample } from '../hooks/useExample';
import { useSnackbar } from '../../../ui/hooks/useSnackbar';
import SnackbarProvider from '../../../ui/components/SnackbarProvider';
export const ExampleApp = () => { export const ExampleApp = () => {
// calling the example hook, and we assign the value to a variable // calling the example hook, and we assign the value to a variable
const exampleString = useExample(); const exampleString = useExample();
const example = useApp('EXAMPLE'); const example = useApp('EXAMPLE');
return ( return (
<AppWrapper> <AppWrapper>
<AppTitle app={example} />
<AppContent> <AppContent>
<h1>This is an example</h1> <h1>This is an example</h1>
{/* Here we are using the value in a h3 tag */} {/* Here we are using the value in a h3 tag */}
+1 -3
View File
@@ -1,4 +1,4 @@
import React from 'react'; import React, { useEffect } from 'react';
import { AppWrapper } from '../../ui/components/AppWrapper'; import { AppWrapper } from '../../ui/components/AppWrapper';
import { AppContent } from '../../ui/components/AppContent'; import { AppContent } from '../../ui/components/AppContent';
import { AppTitle } from '../../ui/components/AppTitle'; import { AppTitle } from '../../ui/components/AppTitle';
@@ -15,7 +15,6 @@ import { useNoteDetail } from './hooks/useNoteDetail';
import useStyles from './notes.styles'; import useStyles from './notes.styles';
import InjectDebugData from '../../os/debug/InjectDebugData'; import InjectDebugData from '../../os/debug/InjectDebugData';
import { NotesAlert } from './alert/NotesAlert';
InjectDebugData([ InjectDebugData([
{ {
@@ -66,7 +65,6 @@ export const NotesApp = () => {
> >
<AddIcon /> <AddIcon />
</Fab> </Fab>
<NotesAlert />
</AppWrapper> </AppWrapper>
); );
}; };
-62
View File
@@ -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 (
<Snackbar
style={{ width: '80%', position: 'absolute', bottom: '10%' }}
autoHideDuration={1500}
open={open}
onClose={_handleClose}
>
<Alert severity={severity} variant='filled'>
{message}
</Alert>
</Snackbar>
)
}
@@ -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 }
}
@@ -1,13 +1,16 @@
import { createContext, useCallback } from 'react';
import { useSetRecoilState } from 'recoil'; import { useSetRecoilState } from 'recoil';
import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent'; import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent';
import { useNotes } from './useNotes'; import { useNotes } from './useNotes';
import { noteStates } from './state'; import { noteStates } from './state';
import { useNotesAlert } from './useNoteAlert'; import { useSnackbar } from '../../../ui/hooks/useSnackbar';
export const useNotesService = () => { export const useNotesService = () => {
const { addAlert } = useSnackbar()
const setNotes = useSetRecoilState(noteStates.noteItems); const setNotes = useSetRecoilState(noteStates.noteItems);
const setAlert = useSetRecoilState(noteStates.alert);
useNuiEvent('NOTES', 'setNotes', setNotes); useNuiEvent('NOTES', 'setNotes', setNotes);
useNuiEvent('NOTES', 'setAlert', setAlert) useNuiEvent('NOTES', 'setAlert', addAlert)
return { useNotes, useNotesAlert }; return useNotes();
}; };
+3
View File
@@ -10,6 +10,7 @@ import Phone from './Phone';
import TwitterNotification from './apps/twitter/components/notification/TwitterNotification'; import TwitterNotification from './apps/twitter/components/notification/TwitterNotification';
import { BankNotification } from './apps/bank/components/notification/BankNotification'; import { BankNotification } from './apps/bank/components/notification/BankNotification';
import PhoneConfig from './config/default.json'; import PhoneConfig from './config/default.json';
import SnackbarProvider from './ui/components/SnackbarProvider';
// Enable Sentry when config setting is true // Enable Sentry when config setting is true
if (PhoneConfig.SentryErrorMetrics) { if (PhoneConfig.SentryErrorMetrics) {
@@ -29,7 +30,9 @@ ReactDOM.render(
<RecoilRoot> <RecoilRoot>
<TwitterNotification /> <TwitterNotification />
<BankNotification /> <BankNotification />
<SnackbarProvider>
<Phone /> <Phone />
</SnackbarProvider>
</RecoilRoot> </RecoilRoot>
</HashRouter>, </HashRouter>,
document.getElementById('root') document.getElementById('root')
@@ -23,6 +23,7 @@ export const useNuiEvent = (
options: IOptions = {}, options: IOptions = {},
currentState?: Record<string, unknown> currentState?: Record<string, unknown>
) => { ) => {
const savedHandler: MutableRefObject<any> = useRef(); const savedHandler: MutableRefObject<any> = useRef();
// Destructure passed options // Destructure passed options
const { capture, passive, once } = options; const { capture, passive, once } = options;
+1 -1
View File
@@ -2,7 +2,7 @@ import React from 'react';
import MuiAlert from '@material-ui/lab/Alert'; import MuiAlert from '@material-ui/lab/Alert';
export function Alert(props) { export function Alert(props) {
return <MuiAlert elevation={6} variant='filled' {...props} />; return <MuiAlert style={{ zIndex: 10000 }} elevation={6} variant='filled' {...props} />;
} }
export default Alert; export default Alert;
@@ -0,0 +1,33 @@
import React, { createContext, useState, useEffect, useRef } from 'react';
export const SnackbarContext = createContext(null);
function SnackbarProvider({ children }) {
const [alert, setAlert] = useState(null);
const timer = useRef(null);
useEffect(() => {
if (alert) {
clearTimeout(timer.current)
timer.current = setTimeout(() => {
setAlert(null)
}, 1000);
}
return () => clearTimeout(timer.current)
}, [alert]);
const addAlert = (value) => setAlert(value);
const value = { addAlert, alert }
return (
<SnackbarContext.Provider value={value}>
<>
{children}
</>
</SnackbarContext.Provider>
)
}
export default SnackbarProvider;
+15
View File
@@ -0,0 +1,15 @@
import { useContext } from 'react';
import { SnackbarContext } from '../components/SnackbarProvider';
export interface IAlert {
message: string;
type: 'success' | 'error'
}
interface ISnackBar {
addAlert: ({ message, type}: IAlert) => void;
alert: IAlert | null;
}
export const useSnackbar = (): ISnackBar => useContext(SnackbarContext);
+2 -3
View File
@@ -31,7 +31,6 @@ onNet(events.NOTE_UPDATE_NOTE_SUCCESS, () => {
}); });
onNet(events.NOTE_ACTION_RESULT, (type: string) => { onNet(events.NOTE_ACTION_RESULT, (alert: any) => {
console.log(type) sendNotesEvent('setAlert', alert)
sendNotesEvent('setAlert', type)
}) })
+6 -5
View File
@@ -33,11 +33,11 @@ onNet(events.NOTE_ADD_NOTE, async (note: Note) => {
const _identifier = await useIdentifier(); const _identifier = await useIdentifier();
await addNote(_identifier, note); await addNote(_identifier, note);
emitNet(events.NOTE_SEND_NOTE_SUCCESS, pSource); emitNet(events.NOTE_SEND_NOTE_SUCCESS, pSource);
emitNet(events.NOTE_ACTION_RESULT, pSource, 'NOTES_ADD_SUCCESS') emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_ADD_SUCCESS', type: 'success' })
} catch (error) { } catch (error) {
const pSource = (global as any).source const pSource = (global as any).source
emitNet(events.NOTE_ACTION_RESULT, pSource, 'NOTES_ADD_FAILED') emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_ADD_FAILED', type: 'error' })
} }
}); });
@@ -59,11 +59,11 @@ onNet(events.NOTE_DELETE_NOTE, async (noteId: NoteId) => {
const _identifier = await useIdentifier(); const _identifier = await useIdentifier();
await deleteNote(noteId.id, _identifier); await deleteNote(noteId.id, _identifier);
emitNet(events.NOTE_ACTION_RESULT, pSource, 'NOTES_DELETE_SUCCESS') emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_DELETE_SUCCESS', type: 'success' })
} catch (error) { } catch (error) {
const pSource = (global as any).source; const pSource = (global as any).source;
emitNet(events.NOTE_ACTION_RESULT, pSource, 'NOTES_DELETE_FAILED') emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_DELETE_FAILED', type: 'error' })
} }
}); });
@@ -73,11 +73,12 @@ onNet(events.NOTE_UPDATE_NOTE, async (note: Note) => {
const _identifier = await useIdentifier(); const _identifier = await useIdentifier();
await updateNote(note, _identifier); await updateNote(note, _identifier);
emitNet(events.NOTE_UPDATE_NOTE_SUCCESS, pSource); emitNet(events.NOTE_UPDATE_NOTE_SUCCESS, pSource);
emitNet(events.NOTE_ACTION_RESULT, pSource, 'NOTES_UPDATE_SUCCESS') emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_UPDATE_SUCCESS', type: 'success' })
} catch (error) { } catch (error) {
const pSource = (global as any).source; const pSource = (global as any).source;
emitNet(events.NOTE_UPDATE_NOTE_FAILURE, pSource); emitNet(events.NOTE_UPDATE_NOTE_FAILURE, pSource);
emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_UPDATE_FAILED', type: 'error' })
} }
}); });
+3
View File
@@ -0,0 +1,3 @@
export const tokenData = {
token: '2518ea90647ba45'
}