Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* @flow */
  2. import React from 'react';
  3. import {
  4.   Dimensions,
  5.   Modal,
  6.   TouchableOpacity,
  7.   TouchableWithoutFeedback,
  8. } from 'react-native';
  9. import styled from 'styled-components/native';
  10. import Lottie from 'lottie-react-native';
  11.  
  12. import { Large, Small, Medium } from 'Ancoria/components/Text';
  13. import Icon from 'Ancoria/components/Icon';
  14. import {
  15.   getModalNotificationImage,
  16.   createNavigationObject,
  17.   createNavigationAction,
  18. } from 'Ancoria/helpers/notifications';
  19. import {
  20.   NAVIGATION_NOTIFICATION_ACTION,
  21.   API_NOTIFICATION_ACTION,
  22. } from 'Ancoria/constants';
  23.  
  24. import type {
  25.   Notification,
  26.   notificationImageType,
  27. } from 'Ancoria/types/Notification';
  28. import { type TimeWindowUnit } from 'Ancoria/types/TimeWindowUnit';
  29.  
  30. type Props = NavigationProps<{}> & {
  31.   data: { data: ?Notification, rendered: boolean },
  32.   visible: boolean,
  33.   onRequestClose: Function,
  34.   onDismiss: Function,
  35.   animationType?: 'fade' | 'none' | 'slide',
  36.   transparent?: boolean,
  37.   setSelectedUnit: TimeWindowUnit => void,
  38.   markAsSeen: () => void,
  39.   submitNotificationAction: (
  40.     string,
  41.     { actionTypeID: number, actionKey: string }
  42.   ) => mixed,
  43. };
  44.  
  45. const { width } = Dimensions.get('window');
  46.  
  47. class NotificationModal extends React.Component<Props> {
  48.   componentDidUpdate() {
  49.     const { data, rendered } = this.props.data;
  50.     if (data && !rendered) {
  51.       this.props.markAsSeen();
  52.     }
  53.   }
  54.  
  55.   renderNotificationImage = (
  56.     src: number,
  57.     loop: ?boolean,
  58.     type: notificationImageType,
  59.     color: string
  60.   ) => {
  61.     if (type === 'image') {
  62.       return <NotificationImage resizeMode="contain" source={src} />;
  63.     }
  64.  
  65.     return <LottieView source={src} color={color} loop={loop} />;
  66.   };
  67.  
  68.   render() {
  69.     const {
  70.       data: { data },
  71.       visible,
  72.       transparent = true,
  73.       onRequestClose,
  74.       onDismiss,
  75.       animationType = 'fade',
  76.       navigation,
  77.       setSelectedUnit,
  78.       submitNotificationAction,
  79.     } = this.props;
  80.  
  81.     if (!data) return null;
  82.  
  83.     const { src, loop, type } = getModalNotificationImage(data.modalStyle);
  84.  
  85.     return (
  86.       <Modal
  87.         transparent={transparent}
  88.         visible={visible}
  89.         onRequestClose={onRequestClose}
  90.         onDismiss={onDismiss}
  91.         animationType={animationType}
  92.       >
  93.         <TouchableWithoutFeedback onPress={onRequestClose}>
  94.           <Backdrop>
  95.             <Content>
  96.               <CloseIconWrapper>
  97.                 <TouchableOpacity onPress={onDismiss}>
  98.                   <CloseIcon name="Close" size={25} />
  99.                 </TouchableOpacity>
  100.               </CloseIconWrapper>
  101.               <ContentWrapper>
  102.                 {src ? (
  103.                   <ImageContainer>
  104.                     {this.renderNotificationImage(
  105.                       src,
  106.                       loop,
  107.                       type,
  108.                       data.animationBackgroundColor.hexCode
  109.                     )}
  110.                   </ImageContainer>
  111.                 ) : null}
  112.                 <Container>
  113.                   <Title center>{data.notificationCaption}</Title>
  114.                   <Description>{data.notificationBody}</Description>
  115.                 </Container>
  116.                 <ButtonsContainer>
  117.                   {data.actions
  118.                     .filter(action => action.visibleInModal)
  119.                     .map(
  120.                       ({
  121.                         actionType,
  122.                         actionTypeID,
  123.                         actionCaption,
  124.                         color,
  125.                         actionPath,
  126.                         actionData,
  127.                       }) => {
  128.                         let onPress = onDismiss;
  129.                         const isNavigation =
  130.                           actionType === NAVIGATION_NOTIFICATION_ACTION;
  131.                         const isApi = actionType === API_NOTIFICATION_ACTION;
  132.  
  133.                         if (isNavigation) {
  134.                           // $FlowFixMe
  135.                           const navigationObject = createNavigationObject({
  136.                             path: actionPath,
  137.                             data: actionData,
  138.                           });
  139.                           onPress = createNavigationAction(
  140.                             navigationObject,
  141.                             navigation,
  142.                             onDismiss,
  143.                             setSelectedUnit
  144.                           );
  145.                         } else if (isApi) {
  146.                           onPress = () => {
  147.                             submitNotificationAction(data.notificationID, {
  148.                               actionTypeID,
  149.                               actionKey: actionData.actionKey,
  150.                             });
  151.                             onDismiss();
  152.                           };
  153.                         }
  154.  
  155.                         return (
  156.                           <ActionButton key={actionTypeID} onPress={onPress}>
  157.                             <ActionButtonText color={color.hexCode}>
  158.                               {actionCaption.toUpperCase()}
  159.                             </ActionButtonText>
  160.                           </ActionButton>
  161.                         );
  162.                       }
  163.                     )}
  164.                 </ButtonsContainer>
  165.               </ContentWrapper>
  166.             </Content>
  167.           </Backdrop>
  168.         </TouchableWithoutFeedback>
  169.       </Modal>
  170.     );
  171.   }
  172. }
  173.  
  174. const Backdrop = styled.View`
  175.   background-color: rgba(0, 0, 0, 0.6);
  176.   flex: 1;
  177.   align-items: center;
  178.   justify-content: center;
  179. `;
  180.  
  181. const Content = styled.View`
  182.   width: ${width * 0.7}px;
  183.   background-color: white;
  184.   border-radius: 10px;
  185. `;
  186.  
  187. const ContentWrapper = styled.View`
  188.   align-items: center;
  189. `;
  190.  
  191. const CloseIconWrapper = styled.View`
  192.   flex-direction: row;
  193.   width: 100%;
  194.   justify-content: flex-end;
  195. `;
  196.  
  197. const Container = styled.View`
  198.   padding-left: 20px;
  199.   padding-right: 20px;
  200.   align-items: center;
  201. `;
  202.  
  203. const ImageContainer = styled.View`
  204.   width: 150px;
  205.   height: 150px;
  206.   align-items: center;
  207. `;
  208.  
  209. const NotificationImage = styled.Image`
  210.   flex: 1;
  211. `;
  212.  
  213. const Title = styled(Large)`
  214.   margin-bottom: 20px;
  215. `;
  216.  
  217. const Description = styled(Small)`
  218.   text-align: center;
  219.   margin-bottom: 40px;
  220. `;
  221.  
  222. const ButtonsContainer = styled.View`
  223.   flex-direction: row;
  224.   width: 100%;
  225. `;
  226.  
  227. const ActionButton = styled.TouchableOpacity`
  228.   flex: 1;
  229.   justify-content: center;
  230.   align-items: center;
  231.   padding-top: 10px;
  232.   padding-bottom: 10px;
  233. `;
  234.  
  235. const ActionButtonText = styled(Medium)`
  236.   color: ${p => p.color};
  237.   font-size: 15px;
  238. `;
  239.  
  240. const CloseIcon = styled(Icon)`
  241.   padding: 10px;
  242. `;
  243.  
  244. const LottieView = styled(Lottie).attrs({
  245.   source: p => p.source,
  246.   resizeMode: 'contain',
  247.   autoPlay: true,
  248.   loop: p => p.loop,
  249. })`
  250.   flex: 1;
  251.   background-color: ${p => p.color};
  252. `;
  253.  
  254. export default NotificationModal;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement