2019-07-23 14:02:57 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { StyleSheet } from 'react-native';
|
|
|
|
import EasyToast from 'react-native-easy-toast';
|
|
|
|
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../lib/constants';
|
2019-07-23 14:02:57 +00:00
|
|
|
import sharedStyles from '../views/Styles';
|
|
|
|
import EventEmitter from '../utils/events';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes, withTheme } from '../theme';
|
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';
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface IToastProps {
|
2022-04-12 16:27:05 +00:00
|
|
|
theme?: TSupportedThemes;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Toast extends React.Component<IToastProps, any> {
|
2022-03-18 14:25:03 +00:00
|
|
|
private listener?: Function;
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-03-18 14:25:03 +00:00
|
|
|
private toast: EasyToast | null | undefined;
|
2019-12-04 16:39:53 +00:00
|
|
|
|
2019-07-23 14:02:57 +00:00
|
|
|
componentDidMount() {
|
2021-02-23 19:00:39 +00:00
|
|
|
this.listener = EventEmitter.addEventListener(LISTENER, this.showToast);
|
2019-07-23 14:02:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
shouldComponentUpdate(nextProps: any) {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-23 14:02:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2022-03-18 14:25:03 +00:00
|
|
|
if (this.listener) {
|
|
|
|
EventEmitter.removeListener(LISTENER, this.listener);
|
|
|
|
}
|
2019-07-23 14:02:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 14:25:03 +00:00
|
|
|
getToastRef = (toast: EasyToast | null) => (this.toast = toast);
|
2020-02-17 16:06:08 +00:00
|
|
|
|
2022-03-18 14:25:03 +00:00
|
|
|
showToast = ({ message }: { message: string }) => {
|
2020-02-17 16:06:08 +00:00
|
|
|
if (this.toast && this.toast.show) {
|
|
|
|
this.toast.show(message, 1000);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-07-23 14:02:57 +00:00
|
|
|
|
|
|
|
render() {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2019-07-23 14:02:57 +00:00
|
|
|
return (
|
|
|
|
<EasyToast
|
2020-02-17 16:06:08 +00:00
|
|
|
ref={this.getToastRef}
|
2021-09-13 20:41:05 +00:00
|
|
|
// @ts-ignore
|
2019-07-23 14:02:57 +00:00
|
|
|
position='center'
|
2022-01-17 16:10:39 +00:00
|
|
|
style={[styles.toast, { backgroundColor: themes[theme!].toastBackground }]}
|
|
|
|
textStyle={[styles.text, { color: themes[theme!].buttonText }]}
|
2019-07-23 14:02:57 +00:00
|
|
|
opacity={0.9}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 16:39:53 +00:00
|
|
|
|
|
|
|
export default withTheme(Toast);
|