2022-06-22 12:32:57 +00:00
|
|
|
import React, { useEffect } from 'react';
|
2019-07-23 14:02:57 +00:00
|
|
|
import { StyleSheet } from 'react-native';
|
|
|
|
import EasyToast from 'react-native-easy-toast';
|
|
|
|
|
2022-06-06 14:17:51 +00:00
|
|
|
import EventEmitter from '../lib/methods/helpers/events';
|
2022-06-22 12:32:57 +00:00
|
|
|
import { useTheme } from '../theme';
|
|
|
|
import sharedStyles from '../views/Styles';
|
2019-07-23 14:02:57 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
toast: {
|
|
|
|
maxWidth: 300,
|
|
|
|
padding: 10
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
fontSize: 14,
|
2020-11-30 21:47:05 +00:00
|
|
|
...sharedStyles.textRegular,
|
|
|
|
...sharedStyles.textAlignCenter
|
2019-07-23 14:02:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export const LISTENER = 'Toast';
|
|
|
|
|
2022-06-22 12:32:57 +00:00
|
|
|
let listener: Function;
|
|
|
|
let toast: EasyToast | null | undefined;
|
2019-07-23 14:02:57 +00:00
|
|
|
|
2022-06-22 12:32:57 +00:00
|
|
|
const Toast = (): React.ReactElement => {
|
|
|
|
const { colors } = useTheme();
|
2019-07-23 14:02:57 +00:00
|
|
|
|
2022-06-22 12:32:57 +00:00
|
|
|
useEffect(() => {
|
|
|
|
listener = EventEmitter.addEventListener(LISTENER, showToast);
|
|
|
|
return () => {
|
|
|
|
EventEmitter.removeListener(LISTENER, listener);
|
|
|
|
};
|
|
|
|
}, []);
|
2019-07-23 14:02:57 +00:00
|
|
|
|
2022-06-22 12:32:57 +00:00
|
|
|
const getToastRef = (newToast: EasyToast | null) => (toast = newToast);
|
2020-02-17 16:06:08 +00:00
|
|
|
|
2022-06-22 12:32:57 +00:00
|
|
|
const showToast = ({ message }: { message: string }) => {
|
|
|
|
if (toast && toast.show) {
|
|
|
|
toast.show(message, 1000);
|
2020-02-17 16:06:08 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-07-23 14:02:57 +00:00
|
|
|
|
2022-06-22 12:32:57 +00:00
|
|
|
return (
|
|
|
|
<EasyToast
|
|
|
|
ref={getToastRef}
|
|
|
|
// @ts-ignore
|
|
|
|
position='center'
|
|
|
|
style={[styles.toast, { backgroundColor: colors.toastBackground }]}
|
|
|
|
textStyle={[styles.text, { color: colors.buttonText }]}
|
|
|
|
opacity={0.9}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Toast;
|