2019-07-23 14:02:57 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { StyleSheet } from 'react-native';
|
|
|
|
import EasyToast from 'react-native-easy-toast';
|
2019-12-04 16:39:53 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2019-07-23 14:02:57 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../constants/colors';
|
2019-07-23 14:02:57 +00:00
|
|
|
import sharedStyles from '../views/Styles';
|
|
|
|
import EventEmitter from '../utils/events';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { 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';
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
class Toast extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
theme: PropTypes.string
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
shouldComponentUpdate(nextProps) {
|
|
|
|
const { theme } = this.props;
|
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-23 14:02:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2021-02-23 19:00:39 +00:00
|
|
|
EventEmitter.removeListener(LISTENER, this.listener);
|
2019-07-23 14:02:57 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 16:06:08 +00:00
|
|
|
getToastRef = toast => this.toast = toast;
|
|
|
|
|
2019-07-23 14:02:57 +00:00
|
|
|
showToast = ({ message }) => {
|
2020-02-17 16:06:08 +00:00
|
|
|
if (this.toast && this.toast.show) {
|
|
|
|
this.toast.show(message, 1000);
|
|
|
|
}
|
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}
|
2019-07-23 14:02:57 +00:00
|
|
|
position='center'
|
2019-12-04 16:39:53 +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);
|