Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect } from 'react'
  2. import {View, FlatList, ScrollView, Animated, StyleSheet, Text, TouchableOpacity, Image, Platform} from 'react-native'
  3. import { connect } from 'react-redux'
  4. import { withNavigation } from 'react-navigation'
  5. import firebase from 'react-native-firebase'
  6. import ImagePicker from 'react-native-image-picker'
  7.  
  8. import { useForm } from '../../hooks/useForm'
  9.  
  10. import ActionButton from '../buttons/ActionButton'
  11. import ItemInput from './ItemInput'
  12. import AdditionalDetails, {
  13.   CommentPicture,
  14.   Comment,
  15.   SelectOtherEventType,
  16. } from './AdditionalDetails'
  17. import {colors, CONTAINER_NUMBER, EVENTS} from '../../config'
  18. import { useKeyboardListener } from '../../hooks/useKeyboardListener'
  19. import {
  20.   updateCurrentEvent,
  21.   createEvent,
  22.   addPictureEvent,
  23.   removePictureEvent,
  24.   addCommentEvent,
  25. } from '../../redux/actions'
  26.  
  27. import { getGeolocation } from '../../libs/geolocation'
  28. import { validateOtherEvent } from '../../libs/forms-validation/other-event-validation'
  29. import Icon from "react-native-vector-icons/FontAwesome";
  30.  
  31. const styles = StyleSheet.create({
  32.   viewContainer: {
  33.     flex: 1,
  34.   },
  35.   viewContentContainer: {
  36.     flexGrow: 1,
  37.     justifyContent: 'space-between',
  38.     alignItems: 'stretch',
  39.     paddingTop: 24,
  40.     paddingLeft: 24,
  41.     paddingRight: 24,
  42.   },
  43.   additionalDetailsContent: {
  44.     justifyContent: 'center',
  45.     alignItems: 'center',
  46.     alignSelf: 'stretch',
  47.   },
  48.   item: {
  49.     backgroundColor: '#f9c2ff',
  50.     padding: 10,
  51.     marginVertical: 8,
  52.     marginHorizontal: 16,
  53.     width: 100,
  54.     height:100
  55.   },
  56.   title: {
  57.     fontSize: 32,
  58.   },
  59.   addPictureButton: {
  60.     justifyContent: 'center',
  61.     alignItems: 'center',
  62.   },
  63.   addPictureText: {
  64.     ...Platform.select({
  65.       android: {
  66.         fontFamily: 'Roboto',
  67.       },
  68.       ios: {
  69.         fontFamily: 'San Francisco',
  70.       },
  71.     }),
  72.     fontSize: 14,
  73.     fontWeight: '400',
  74.     color: colors.greyDark,
  75.   },
  76.   removePictureButton: {
  77.     flexDirection: 'row',
  78.     justifyContent: 'center',
  79.     alignItems: 'center',
  80.   },
  81.   removePictureText: {
  82.     ...Platform.select({
  83.       android: {
  84.         fontFamily: 'Roboto',
  85.       },
  86.       ios: {
  87.         fontFamily: 'San Francisco',
  88.       },
  89.     }),
  90.     fontSize: 14,
  91.     fontWeight: '400',
  92.     color: colors.greyDark,
  93.     marginLeft: 6,
  94.   },
  95.  
  96.   picture: {
  97.     marginBottom: 12,
  98.   },
  99. })
  100.  
  101. const pictureOptions = {
  102.   title: 'Take a picture',
  103.   mediaType: 'photo',
  104.   cameraType: 'back',
  105.   noData: true,
  106.   storageOptions: {
  107.     skipBackup: true,
  108.     path: 'images',
  109.   },
  110. }
  111.  
  112. const mapStateToProps = state => ({
  113.   eventType: state.currentEvent.type,
  114.   containerNumber: state.currentEvent.containerNumber,
  115.   additionalDetails: state.currentEvent.details,
  116. })
  117.  
  118. const mapDispatchToProps = dispatch => ({
  119.   saveEvent: event => dispatch(updateCurrentEvent(event)),
  120.   addNewEvent: event => dispatch(createEvent(event)),
  121.   addPicture: pictureData => dispatch(addPictureEvent(pictureData)),
  122.   removePicture: () => dispatch(removePictureEvent()),
  123.   addComment: comment => dispatch(addCommentEvent(comment)),
  124. })
  125.  
  126. const OtherEventForm = ({
  127.   eventType,
  128.   containerNumber,
  129.   additionalDetails,
  130.   navigation,
  131.   addNewEvent,
  132. }) => {
  133.   const initialValues = {
  134.     pictures: [],
  135.     containerNumber,
  136.     eventType,
  137.     // otherEventType: 'Container damaged',
  138.     details: additionalDetails,
  139.   }
  140.  
  141.   const {
  142.     values,
  143.     setValues,
  144.     errors,
  145.     handleChange,
  146.     handleSubmit,
  147.     isDirty,
  148.   } = useForm(initialValues, createNewEvent, validateOtherEvent)
  149.  
  150.   const [detailsVisible, setDetailsVisible] = useState(false)
  151.   const { shift } = useKeyboardListener()
  152.  
  153.   function sendDataBack(data) {
  154.     setValues(prevState => ({ ...prevState, ...data }))
  155.   }
  156.  
  157.   function goToCapture(scanType) {
  158.     navigation.navigate('Capture', {
  159.       scanType,
  160.       sendDataBack,
  161.     })
  162.   }
  163.  
  164.   // function onAddPicture() {
  165.   //   ImagePicker.launchCamera(pictureOptions, response => {
  166.   //     if (!response.didCancel && !response.error) {
  167.   //
  168.   //       const picture = {
  169.   //         key: picturesData.length,
  170.   //         source: { uri: response.uri },
  171.   //         isPortrait: !response.isVertical,
  172.   //         ...response,
  173.   //       }
  174.   //       picturesData.push(picture);
  175.   //       setValues(prevState => ({
  176.   //         ...prevState,
  177.   //         details: {
  178.   //           ...prevState.details,
  179.   //           picture,
  180.   //         },
  181.   //       }))
  182.   //     }
  183.   //   })
  184.   // }
  185.   //
  186.   // function onRemovePicture() {
  187.   //   setValues(prevState => {
  188.   //     const {
  189.   //       details: { picture, ...detailsRest },
  190.   //       ...state
  191.   //     } = prevState
  192.   //
  193.   //     return {
  194.   //       ...state,
  195.   //       details: detailsRest,
  196.   //     }
  197.   //   })
  198.   // }
  199.  
  200.   function onAddComment(value) {
  201.     handleChange({
  202.       details: {
  203.         ...values.details,
  204.         comment: value,
  205.       },
  206.     })
  207.   }
  208.  
  209.   function onPickerSelect(otherEventType) {
  210.     setValues(prevState => ({
  211.       ...prevState,
  212.       otherEventType,
  213.     }))
  214.   }
  215.  
  216.   function onAdd() {
  217.     console.log()
  218.     ImagePicker.launchCamera(pictureOptions, response => {
  219.       if (!response.didCancel && !response.error) {
  220.  
  221.         const newPicture = {
  222.           key: values.pictures.length,
  223.           source: { uri: response.uri },
  224.           isPortrait: !response.isVertical,
  225.           ...response,
  226.         }
  227.  
  228.         setValues((prevState) => {
  229.           const {
  230.             pictures,
  231.             ...state
  232.           } = prevState
  233.  
  234.           pictures.push(newPicture)
  235.           return {
  236.             ...state,
  237.             pictures: pictures,
  238.           }
  239.         })
  240.       }
  241.     })
  242.   }
  243.  
  244.   function onRemove(key) {
  245.     setValues((prevState) => {
  246.       const {
  247.         pictures,
  248.         ...state
  249.       } = prevState
  250.       return {
  251.         ...state,
  252.         pictures: pictures.filter(item => item.key !== key),
  253.       }
  254.     })
  255.   }
  256.  
  257.   /* Handle form submission */
  258.   async function createNewEvent() {
  259.     const position = {}
  260.  
  261.     try {
  262.       const location = await getGeolocation()
  263.  
  264.       if (location) {
  265.         // Set geocoords
  266.         position.coords = new firebase.firestore.GeoPoint(
  267.           location.coords.latitude,
  268.           location.coords.longitude,
  269.         )
  270.  
  271.         // Set null error
  272.         position.error = null
  273.       }
  274.     } catch ({ code, message }) {
  275.       position.error = { code, message }
  276.     } finally {
  277.       console.log('add new event', values, position)
  278.  
  279.       // create new event
  280.       addNewEvent({
  281.         ...values,
  282.         type: eventType,
  283.         position,
  284.       })
  285.  
  286.       navigation.navigate(EVENTS[eventType].screen) // go to specific event type dashboard tab
  287.     }
  288.   }
  289.  
  290.   /* Handle additional details display */
  291.   const onToggleDetails = () => setDetailsVisible(prevState => !prevState)
  292.  
  293.   // Set default picker option value to "Container damaged"
  294.   useEffect(() => {
  295.     onPickerSelect('Container damaged')
  296.   }, [])
  297.  
  298.   return (
  299.     <View style={styles.viewContainer}>
  300.       <ScrollView
  301.         style={styles.viewContainer}
  302.         contentContainerStyle={styles.viewContentContainer}
  303.         showsVerticalScrollIndicator
  304.       >
  305.         <Animated.View style={{ transform: [{ translateY: shift }] }}>
  306.           <ItemInput
  307.             name="containerNumber"
  308.             placeholderText="Container number"
  309.             onScanPress={() => goToCapture(CONTAINER_NUMBER)}
  310.             onChangeText={value => handleChange({ containerNumber: value })}
  311.             value={values.containerNumber}
  312.             error={values.containerNumber ? !!errors.containerNumber : null}
  313.           />
  314.  
  315.           <SelectOtherEventType
  316.             selectedValue={values.otherEventType}
  317.             onValueChange={onPickerSelect}
  318.           />
  319.  
  320.           <AdditionalDetails
  321.             visible={detailsVisible}
  322.             onToggle={onToggleDetails}
  323.           >
  324.             <View style={styles.additionalDetailsContent}>
  325.               <FlatList
  326.                 horizontal
  327.                 data={[...values.pictures, { plusImage: true }]}
  328.                 renderItem={({ item }) => {
  329.                   if (item.plusImage) {
  330.                     return (
  331.                       <TouchableOpacity style={styles.addPictureButton} onPress={onAdd}>
  332.                         <Icon name="camera" size={48} color={colors.greyDark} />
  333.                         <Text style={styles.addPictureText}>Add Picture</Text>
  334.                       </TouchableOpacity>
  335.                     )
  336.                   }
  337.                   return (
  338.                     <>
  339.                       <Image
  340.                         style={[
  341.                           styles.picture,
  342.                           {
  343.                             width: item.isPortrait ? 200 / 1.33333 : 200,
  344.                             height: item.isPortrait ? 200 : 200 / 1.33333,
  345.                           },
  346.                         ]}
  347.                         source={item.source}
  348.                       />
  349.                       <TouchableOpacity
  350.                         style={styles.removePictureButton}
  351.                         onPress={onRemove(item.key)}
  352.                       >
  353.                         <Icon
  354.                           name="trash-o"
  355.                           size={14}
  356.                           color={colors.greyDark}
  357.                         />
  358.                         <Text
  359.                           style={styles.removePictureText}
  360.                         >
  361.                           Remove
  362.                         </Text>
  363.                       </TouchableOpacity>
  364.                     </>
  365.                   )
  366.                 }}
  367.                 keyExtractor={(item, index) => index.toString()}
  368.               />
  369.               <Comment
  370.                 value={values.details && values.details.comment}
  371.                 onChangeText={onAddComment}
  372.               />
  373.             </View>
  374.           </AdditionalDetails>
  375.         </Animated.View>
  376.       </ScrollView>
  377.  
  378.       <ActionButton
  379.         onPress={handleSubmit}
  380.         text="Submit Container Event"
  381.         disabled={isDirty}
  382.       />
  383.     </View>
  384.   )
  385. }
  386.  
  387. const ConnectedOtherEventForm = withNavigation(
  388.   connect(
  389.     mapStateToProps,
  390.     mapDispatchToProps,
  391.   )(OtherEventForm),
  392. )
  393.  
  394. export { ConnectedOtherEventForm as OtherEventForm }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement