daily pastebin goal
34%
SHARE
TWEET

Untitled

a guest Jul 12th, 2018 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import React from 'react';
  3. import { __ } from '../../Transforms';
  4. import { Observer } from 'mobx-react';
  5. import { MenuContainer, MenuItem, MenuModal } from '../../Components/Menu/Menu';
  6. import PropTypes from 'prop-types';
  7. import colors from '../../Themes/Colors';
  8. import TaggunAPI, { getExpenseDataFromTaggunResult, mergeExpense } from '../../Services/TaggunAPI';
  9. import { DocumentPicker } from 'expo';
  10. import { logEvent } from '../../Analytics/AmplitudeAnalytics';
  11. import {
  12.   USER_CLICKED_MANUALLY_INPUT_EXPENSE_FROM_GALLERY,
  13.   USER_CLICKED_SCAN_EXPENSE_FROM_EXPENSE_SCREEN,
  14.   USER_CLICKED_UPLOAD_PDF_FROM_EXPENSE_SCREEN
  15. } from '../../Analytics/AmplitudeConstants';
  16. import { Platform } from "react-native";
  17.  
  18. export default class AttachToExpenseMenu extends React.Component {
  19.   static propTypes = {
  20.     navigation: PropTypes.shape({
  21.       navigate: PropTypes.func.isRequired,
  22.     }).isRequired,
  23.     goBack: PropTypes.func,
  24.     scanCustomCallback: PropTypes.func, // This will be called instead of opening the expense to scan if provided
  25.     onPDFProcessStarted: PropTypes.func, // An optional callback for when the PDF processing started
  26.     onPDFProcessStopped: PropTypes.func, // An optional callback for when the PDF processing is finished
  27.     color: PropTypes.string, // Custom color theme (default is invoices)
  28.     scanCustomPdf: PropTypes.func,
  29.  
  30.   };
  31.  
  32.   static defaultProps = {
  33.     onPDFProcessStarted: () => {
  34.     },
  35.     onPDFProcessStopped: () => {
  36.     },
  37.     goBack: () => {
  38.  
  39.     },
  40.     color: colors.expenses,
  41.     scanCustomCallback: null,
  42.   };
  43.  
  44.   state = {
  45.     expense: null,
  46.   };
  47.  
  48.   open = expense => {
  49.     this.setState({ expense });
  50.     this.menuModal.open();
  51.   };
  52.  
  53.   close = () => {
  54.     this.menuModal.close();
  55.   };
  56.  
  57.   addPhotoScan = () => {
  58.     logEvent(USER_CLICKED_SCAN_EXPENSE_FROM_EXPENSE_SCREEN);
  59.     this.props.navigation.navigate('expense', {
  60.       onClose: this.props.goBack,
  61.       expense: this.state.expense,
  62.       scan: true,
  63.     });
  64.   };
  65.  
  66.   addPhotoGalery = () => {
  67.     logEvent(USER_CLICKED_MANUALLY_INPUT_EXPENSE_FROM_GALLERY);
  68.     this.props.navigation.navigate('expense', {
  69.       onClose: this.props.goBack,
  70.       expense: this.state.expense,
  71.       uploadToExpense: true,
  72.     });
  73.   };
  74.  
  75.   uploadPDF = userStore => {
  76.     logEvent(USER_CLICKED_UPLOAD_PDF_FROM_EXPENSE_SCREEN);
  77.     let expense = { ...this.state.expense };
  78.     this.close();
  79.     // Show a document picker to choose a PDF file
  80.     setTimeout(() => DocumentPicker.getDocumentAsync()
  81.       .then(
  82.         doc => {
  83.           // Use cancel the picker, stop everything
  84.           if (doc.type === 'cancel') return;
  85.  
  86.           // Render that we are processing the PDF
  87.           this.props.onPDFProcessStarted();
  88.  
  89.           // Upload the PDF to the server
  90.           userStore.uploadPDF(doc.uri)
  91.             .then(pdfUrl => {
  92.               // Create a new empty expense
  93.               expense.photoSrc = pdfUrl;
  94.               TaggunAPI.getOCRFromFile(doc.uri)
  95.                 .then(result => {
  96.                   // Indicate that we have stopped processing the PDF
  97.                   this.props.onPDFProcessStopped();
  98.                   expense = {
  99.                     ...expense,
  100.                     ...getExpenseDataFromTaggunResult(result, userStore, expense)
  101.                   };
  102.                   expense.isImportedFromPDF = true;
  103.  
  104.                   this.props.scanCustomPdf(expense);
  105.  
  106.                   // Open the expense form view, passing the expense data
  107.                   this.props.navigation.navigate('expense', {
  108.                     expense,
  109.                     isImportedFromPDF: true, // Indicate that the expense has been imported from PDF, used for title and menus
  110.                     onClose: this.props.goBack,
  111.                   });
  112.                 });
  113.             })
  114.             .then(response => {
  115.                 //    if (response === []) {
  116.                 this.props.onPDFProcessStarted();
  117.                 // }
  118.               }
  119.             );
  120.         }), 500);
  121.         // This is mistake function, maybe modal make problem on IOS, this way is workaround resolve
  122.   };
  123.  
  124.   render() {
  125.     const { color } = this.props;
  126.     return (
  127.       <MenuModal ref={m => this.menuModal = m}>
  128.         <MenuContainer>
  129.           <MenuItem onPress={this.addPhotoGalery} bold color={color}>
  130.             {__('upload_from_gallery')}
  131.           </MenuItem>
  132.           <MenuItem onPress={this.addPhotoScan} bold color={color}>
  133.             {__('scan_an_expense')}
  134.           </MenuItem>
  135.           {/* Inject the user store for the upload PDF button */}
  136.           <Observer
  137.             inject={(stores) => ({ userStore: stores.userStore })}
  138.             render={props => (
  139.               <MenuItem
  140.                 onPress={() => this.uploadPDF(props.userStore)} bold color={color}
  141.                         closeMenu={this.close} last>
  142.                 {__('upload_pdf')}
  143.               </MenuItem>
  144.             )}
  145.           />
  146.         </MenuContainer>
  147.         <MenuContainer>
  148.           <MenuItem last>
  149.             {__('cancel')}
  150.           </MenuItem>
  151.         </MenuContainer>
  152.       </MenuModal>
  153.     );
  154.   }
  155. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top