mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
fix alert provider | remove alert from notes and contact
This commit is contained in:
1 parent
9ba8bd683f
commit
b4258718d8
13 files changed
+67
-175
No files matched your search
+18
-3
@@ -3,6 +3,7 @@ import './Phone.css';
|
|||||||
import './i18n';
|
import './i18n';
|
||||||
import { Route, useHistory } from 'react-router-dom';
|
import { Route, useHistory } 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,7 +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 SnackbarProvider from './ui/components/SnackbarProvider';
|
import { useSnackbar } from './ui/hooks/useSnackbar';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
InjectDebugData([
|
InjectDebugData([
|
||||||
{
|
{
|
||||||
@@ -96,6 +98,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
|
||||||
|
|
||||||
@@ -152,7 +157,6 @@ function Phone() {
|
|||||||
}))}
|
}))}
|
||||||
/>
|
/>
|
||||||
<div className='PhoneAppContainer'>
|
<div className='PhoneAppContainer'>
|
||||||
<SnackbarProvider>
|
|
||||||
{modal ? (
|
{modal ? (
|
||||||
<CallModal />
|
<CallModal />
|
||||||
) : (
|
) : (
|
||||||
@@ -163,7 +167,18 @@ function Phone() {
|
|||||||
))}
|
))}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</SnackbarProvider>
|
{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 />
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import PersonAddIcon from '@material-ui/icons/PersonAdd';
|
|||||||
import Fab from '@material-ui/core/Fab';
|
import Fab from '@material-ui/core/Fab';
|
||||||
import { InfoModal } from './modal/InfoModal';
|
import { InfoModal } from './modal/InfoModal';
|
||||||
import { useContactDetail } from '../hooks/useContactDetail';
|
import { useContactDetail } from '../hooks/useContactDetail';
|
||||||
import { ContactAlert } from './alert/ContactAlert';
|
|
||||||
|
|
||||||
import useStyles from './styles.contacts';
|
import useStyles from './styles.contacts';
|
||||||
import InjectDebugData from '../../../os/debug/InjectDebugData';
|
import InjectDebugData from '../../../os/debug/InjectDebugData';
|
||||||
@@ -59,7 +58,6 @@ export const ContactsApp = () => {
|
|||||||
>
|
>
|
||||||
<PersonAddIcon />
|
<PersonAddIcon />
|
||||||
</Fab>
|
</Fab>
|
||||||
<ContactAlert />
|
|
||||||
</AppContent>
|
</AppContent>
|
||||||
</AppWrapper>
|
</AppWrapper>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -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,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,8 @@ 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';
|
import { useSnackbar } from '../../ui/hooks/useSnackbar';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
InjectDebugData([
|
InjectDebugData([
|
||||||
{
|
{
|
||||||
@@ -38,9 +39,11 @@ InjectDebugData([
|
|||||||
|
|
||||||
export const NotesApp = () => {
|
export const NotesApp = () => {
|
||||||
const { setNoteModal } = useNoteModal();
|
const { setNoteModal } = useNoteModal();
|
||||||
|
const { t } = useTranslation()
|
||||||
const { detail, setDetail } = useNoteDetail();
|
const { detail, setDetail } = useNoteDetail();
|
||||||
const { notes } = useNotes();
|
const { notes } = useNotes();
|
||||||
const notesApp = useApp('NOTES');
|
const notesApp = useApp('NOTES');
|
||||||
|
const { addAlert } = useSnackbar()
|
||||||
|
|
||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
|
|
||||||
@@ -66,7 +69,6 @@ export const NotesApp = () => {
|
|||||||
>
|
>
|
||||||
<AddIcon />
|
<AddIcon />
|
||||||
</Fab>
|
</Fab>
|
||||||
<NotesAlert />
|
|
||||||
</AppWrapper>
|
</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 { 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();
|
||||||
};
|
};
|
||||||
+4
-1
@@ -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 />
|
||||||
<Phone />
|
<SnackbarProvider>
|
||||||
|
<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,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 Alert from './Alert';
|
||||||
import { makeStyles } from '@material-ui/core/styles';
|
import { makeStyles } from '@material-ui/core/styles';
|
||||||
import { Snackbar } from '@material-ui/core';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { ISnackbar } from '../interface/InterfaceUI';
|
import { IAlert } from '../hooks/useSnackbar';
|
||||||
|
|
||||||
const useStyles = makeStyles(() => ({
|
const useStyles = makeStyles(() => ({
|
||||||
alert: {
|
alert: {
|
||||||
@@ -10,39 +10,40 @@ const useStyles = makeStyles(() => ({
|
|||||||
display: 'flex',
|
display: 'flex',
|
||||||
alignItems: 'center',
|
alignItems: 'center',
|
||||||
justifyContent: 'center'
|
justifyContent: 'center'
|
||||||
|
},
|
||||||
|
hidden: {
|
||||||
|
display: 'none'
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
||||||
export const SnackbarContext = createContext(null);
|
export const SnackbarContext = createContext({ addAlert: (a: IAlert) => {} });
|
||||||
const AUTO_HIDE = 2000;
|
|
||||||
|
|
||||||
function SnackbarProvider({ children }) {
|
function SnackbarProvider({ children }) {
|
||||||
const [alerts, setAlerts] = useState([]);
|
|
||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
|
const { t } = useTranslation()
|
||||||
const alertIdx = alerts.join(',');
|
const [alert, setAlert] = useState(null);
|
||||||
|
const timer = useRef(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const timer = setTimeout(() => setAlerts((alert) => alerts.slice(0, alerts.length - 1)), AUTO_HIDE);
|
if (alert) {
|
||||||
return () => clearTimeout(timer);
|
clearTimeout(timer.current)
|
||||||
}, [alertIdx])
|
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 (
|
return (
|
||||||
<SnackbarContext.Provider value={value}>
|
<SnackbarContext.Provider value={value}>
|
||||||
<>
|
<>
|
||||||
{children}
|
{children}
|
||||||
{alerts.map((alert: ISnackbar) => (
|
|
||||||
<div className={classes.alert}>
|
|
||||||
<Alert key={alert} severity={alert.type} variant="filled">
|
|
||||||
{alert.message}
|
|
||||||
</Alert>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</>
|
</>
|
||||||
</SnackbarContext.Provider>
|
</SnackbarContext.Provider>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
import { useContext } from 'react';
|
import { useContext } from 'react';
|
||||||
import { SnackbarContext } from '../components/SnackbarProvider';
|
import { SnackbarContext } from '../components/SnackbarProvider';
|
||||||
|
|
||||||
|
|
||||||
|
export interface IAlert {
|
||||||
|
message: string;
|
||||||
|
type: 'success' | 'error'
|
||||||
|
}
|
||||||
|
|
||||||
interface ISnackBar {
|
interface ISnackBar {
|
||||||
addAlert: (message: string, type: 'success' | 'error') => void;
|
addAlert: ({ message, type}: IAlert) => void;
|
||||||
|
alert: IAlert | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useSnackbar = (): ISnackBar => useContext(SnackbarContext);
|
export const useSnackbar = (): ISnackBar => useContext(SnackbarContext);
|
||||||
@@ -12,9 +12,4 @@ export interface AppWrapperTypes {
|
|||||||
children?: JSX.Element | JSX.Element[];
|
children?: JSX.Element | JSX.Element[];
|
||||||
style?: CSSProperties;
|
style?: CSSProperties;
|
||||||
handleClickAway?: (...args: any[]) => void;
|
handleClickAway?: (...args: any[]) => void;
|
||||||
}
|
|
||||||
|
|
||||||
export interface ISnackbar {
|
|
||||||
message: string;
|
|
||||||
type: 'success' | 'error'
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user