mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +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
@@ -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 (
|
||||
<SnackbarContext.Provider value={value}>
|
||||
<>
|
||||
{children}
|
||||
{alerts.map((alert: ISnackbar) => (
|
||||
<div className={classes.alert}>
|
||||
<Alert key={alert} severity={alert.type} variant="filled">
|
||||
{alert.message}
|
||||
</Alert>
|
||||
</div>
|
||||
))}
|
||||
</>
|
||||
</SnackbarContext.Provider>
|
||||
)
|
||||
|
||||
@@ -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);
|
||||
@@ -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'
|
||||
}
|
||||
Reference in new issue
Block a user