mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 06:28:51 +00:00
Merge pull request #87 from project-error/feat/snackbar-provider
Feat/snackbar provider + using it in notes
This commit is contained in:
16 files changed
+106
-165
No files matched your search
+28
-10
@@ -3,6 +3,7 @@ import './Phone.css';
|
||||
import './i18n';
|
||||
import { Route } 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,6 +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 { useSnackbar } from './ui/hooks/useSnackbar';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
function Phone() {
|
||||
const quickAccess = useQuickAccess();
|
||||
@@ -50,6 +53,9 @@ function Phone() {
|
||||
usePhotoService();
|
||||
useCallService();
|
||||
useDialService();
|
||||
const { t } = useTranslation()
|
||||
|
||||
const { alert } = useSnackbar();
|
||||
|
||||
const { modal } = useModal(); // the calling modal
|
||||
|
||||
@@ -106,16 +112,28 @@ function Phone() {
|
||||
}))}
|
||||
/>
|
||||
<div className='PhoneAppContainer'>
|
||||
{modal ? (
|
||||
<CallModal />
|
||||
) : (
|
||||
<>
|
||||
<Route exact path='/' component={HomeApp} />
|
||||
{allApps.map((App) => (
|
||||
<App.Route key={App.id} />
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
{modal ? (
|
||||
<CallModal />
|
||||
) : (
|
||||
<>
|
||||
<Route exact path='/' component={HomeApp} />
|
||||
{allApps.map((App) => (
|
||||
<App.Route key={App.id} />
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
{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>
|
||||
<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 { 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 (
|
||||
<AppWrapper>
|
||||
<AppTitle app={example} />
|
||||
<AppContent>
|
||||
<h1>This is an example</h1>
|
||||
{/* Here we are using the value in a h3 tag */}
|
||||
|
||||
@@ -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,6 @@ import { useNoteDetail } from './hooks/useNoteDetail';
|
||||
|
||||
import useStyles from './notes.styles';
|
||||
import InjectDebugData from '../../os/debug/InjectDebugData';
|
||||
import { NotesAlert } from './alert/NotesAlert';
|
||||
|
||||
InjectDebugData([
|
||||
{
|
||||
@@ -66,7 +65,6 @@ export const NotesApp = () => {
|
||||
>
|
||||
<AddIcon />
|
||||
</Fab>
|
||||
<NotesAlert />
|
||||
</AppWrapper>
|
||||
);
|
||||
};
|
||||
@@ -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 { 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();
|
||||
};
|
||||
+4
-1
@@ -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(
|
||||
<RecoilRoot>
|
||||
<TwitterNotification />
|
||||
<BankNotification />
|
||||
<Phone />
|
||||
<SnackbarProvider>
|
||||
<Phone />
|
||||
</SnackbarProvider>
|
||||
</RecoilRoot>
|
||||
</HashRouter>,
|
||||
document.getElementById('root')
|
||||
|
||||
@@ -23,6 +23,7 @@ export const useNuiEvent = (
|
||||
options: IOptions = {},
|
||||
currentState?: Record<string, unknown>
|
||||
) => {
|
||||
|
||||
const savedHandler: MutableRefObject<any> = useRef();
|
||||
// Destructure passed options
|
||||
const { capture, passive, once } = options;
|
||||
|
||||
@@ -2,7 +2,7 @@ import React from 'react';
|
||||
import MuiAlert from '@material-ui/lab/Alert';
|
||||
|
||||
export function Alert(props) {
|
||||
return <MuiAlert elevation={6} variant='filled' {...props} />;
|
||||
return <MuiAlert style={{ zIndex: 10000 }} elevation={6} variant='filled' {...props} />;
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -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);
|
||||
@@ -12,4 +12,4 @@ export interface AppWrapperTypes {
|
||||
children?: JSX.Element | JSX.Element[];
|
||||
style?: CSSProperties;
|
||||
handleClickAway?: (...args: any[]) => void;
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,6 @@ onNet(events.NOTE_UPDATE_NOTE_SUCCESS, () => {
|
||||
});
|
||||
|
||||
|
||||
onNet(events.NOTE_ACTION_RESULT, (type: string) => {
|
||||
console.log(type)
|
||||
sendNotesEvent('setAlert', type)
|
||||
onNet(events.NOTE_ACTION_RESULT, (alert: any) => {
|
||||
sendNotesEvent('setAlert', alert)
|
||||
})
|
||||
@@ -33,11 +33,11 @@ onNet(events.NOTE_ADD_NOTE, async (note: Note) => {
|
||||
const _identifier = await useIdentifier();
|
||||
await addNote(_identifier, note);
|
||||
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) {
|
||||
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();
|
||||
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) {
|
||||
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();
|
||||
await updateNote(note, _identifier);
|
||||
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) {
|
||||
const pSource = (global as any).source;
|
||||
emitNet(events.NOTE_UPDATE_NOTE_FAILURE, pSource);
|
||||
emitNet(events.NOTE_ACTION_RESULT, pSource, { message: 'NOTES_UPDATE_FAILED', type: 'error' })
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export const tokenData = {
|
||||
token: '2518ea90647ba45'
|
||||
}
|
||||
Reference in new issue
Block a user