Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { StyleSheet } from 'react-native'
  2.  
  3. export default StyleSheet.create({
  4.   container: {
  5.     height: 75,
  6.     position: 'absolute',
  7.     left: 0,
  8.     top: 0,
  9.     right: 0,
  10.     flexDirection: 'column',
  11.     alignItems: 'center',
  12.     justifyContent: 'center',
  13.     paddingTop: 14
  14.   },
  15.   containerIos: {
  16.  
  17.   },
  18.   containerAndroid: {
  19.  
  20.   }
  21. })
  22.  
  23. class Toast extends Component {
  24.   constructor (props) {
  25.     super(props)
  26.     this.animatedValue = new Animated.Value(0)
  27.     this.closeToast = this.closeToast.bind(this)
  28.     this.clearErrorMessage = this.clearErrorMessage.bind(this)
  29.     this.state = {
  30.       message: this.props.message
  31.     }
  32.   }
  33.  
  34.   renderToast () {
  35.     if (!this.props.visible || this.props.message.length === 0) return
  36.     if (!isAndroid()) {
  37.       Animated.timing(
  38.         this.animatedValue,
  39.         {
  40.           toValue: 1,
  41.           duration: 350
  42.         }).start(this.closeToast())
  43.     } else {
  44.       Snackbar.show(this.props.message, {showAsError: true})
  45.     }
  46.   }
  47.  
  48.   clearErrorMessage () {
  49.     this.props.resetErrorMessage()
  50.   }
  51.  
  52.   closeToast () {
  53.     setTimeout(() => {
  54.       Animated.timing(
  55.         this.animatedValue,
  56.         {
  57.           toValue: 0,
  58.           duration: 350
  59.         }).start(this.clearErrorMessage)
  60.     }, displayDuration)
  61.   }
  62.  
  63.   getColorForToastType (type = 'success') {
  64.     let color
  65.     if (type === SystemMessageTypes.error) color = '#B53553'
  66.     if (type === SystemMessageTypes.primary) color = '#2487DB'
  67.     if (type === SystemMessageTypes.warning) color = '#ec971f'
  68.     if (type === SystemMessageTypes.success) color = 'green'
  69.     return color
  70.   }
  71.  
  72.   render () {
  73.     const type = isEmptyVar(this.props.type) ? SystemMessageTypes.error : this.props.type
  74.     let animation = this.animatedValue.interpolate({
  75.       inputRange: [0, 0.3, 10],
  76.       outputRange: [-90, -10, 0]
  77.     })
  78.     if (Platform.OS === 'ios') {
  79.       return (
  80.         <Animated.View
  81.           style={[style.container, style.containerIos, {
  82.             transform: [{
  83.               translateY: animation
  84.             }],
  85.             backgroundColor: this.getColorForToastType(type)
  86.           }]}>
  87.           <Text
  88.             numberOfLines={2}
  89.             ellipsizeMode={'clip'}
  90.             style={{marginLeft: 10, color: 'white', fontSize: 14, fontWeight: 'bold', textAlign: 'center'}}>
  91.             {this.renderToast()}
  92.             {this.props.message}
  93.           </Text>
  94.         </Animated.View>
  95.       )
  96.     } else {
  97.       return (
  98.         <Animated.View
  99.           style={[style.container, style.containerAndroid, {
  100.             transform: [{
  101.               translateY: animation
  102.             }],
  103.             backgroundColor: this.getColorForToastType(type)
  104.           }]}>
  105.           <Text
  106.             numberOfLines={2}
  107.             ellipsizeMode={'tail'}
  108.             style={{marginLeft: 10, color: 'white', fontSize: 14, fontWeight: 'bold', textAlign: 'center'}}>
  109.             {this.renderToast()}
  110.             {this.props.message}
  111.           </Text>
  112.         </Animated.View>
  113.       )
  114.     }
  115.   }
  116. }
  117.  
  118. Toast.defaultProps = {
  119.   type: 'error',
  120.   visible: false
  121. }
  122.  
  123. Toast.propTypes = {
  124.   message: PropTypes.string,
  125.   type: PropTypes.string,
  126.   visible: PropTypes.bool
  127. }
  128.  
  129. const mapStateToProps = (state, props) => {
  130.   return {
  131.     message: state.error ? state.error.errorMessage : null,
  132.     visible: !isEmptyVar(state.error.errorMessage)
  133.   }
  134. }
  135.  
  136. const mapDispatchToProps = (dispatch, props) => {
  137.   return {
  138.     resetErrorMessage: () => dispatch(receiveErrorMessage(null))
  139.   }
  140. }
  141.  
  142. const reduxContainer = connect(
  143.   mapStateToProps,
  144.   mapDispatchToProps
  145. )(Toast)
  146.  
  147. export default reduxContainer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement