Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. import React, { useState, useEffect } from "react";
  2. import {
  3. View,
  4. Text,
  5. StyleSheet,
  6. TextInput,
  7. TouchableHighlight,
  8. Alert,
  9. Button,
  10. Image
  11. } from "react-native";
  12. import DatePicker from "react-native-datepicker";
  13. import ImagePicker from "react-native-image-picker";
  14. import firebase from "react-native-firebase";
  15. import uuid from "uuid/v4";
  16.  
  17. import { withNavigation } from "react-navigation";
  18. import Colors from "../constants/colors";
  19. import Dimensions from "../constants/dimensions";
  20. import withFireBase from "./withFirebase";
  21.  
  22. const EditItem = props => {
  23. const { updateItem } = props;
  24.  
  25. const state = {
  26. name: props.navigation.getParam("name"),
  27. date: props.navigation.getParam("expire"),
  28. type: props.navigation.getParam("type"),
  29. imageURI: props.navigation.getParam("imageURI"),
  30. ID: props.navigation.getParam("ID"),
  31. filename: ""
  32. };
  33.  
  34. const [autoData, setValues] = useState(state);
  35. const handleNameChange = name => {
  36. setValues(prevState => ({ ...prevState, name: name }));
  37. };
  38.  
  39. const handleSubmit = () => {
  40. updateItem({
  41. name: autoData.name,
  42. imageURI: autoData.imageURI,
  43. expire: autoData.date,
  44. type: autoData.type,
  45. date: autoData.date,
  46. ID: autoData.ID,
  47. filename: autoData.filename
  48. });
  49. console.log("TEST =>", autoData);
  50.  
  51. Alert.alert("Eveniment actualizat cu succes");
  52. };
  53.  
  54. const handleDateChange = date => {
  55. setValues(prevState => ({ ...prevState, date }));
  56. };
  57.  
  58. const uploadImage = async () => {
  59. const ext = autoData.imageUri && autoData.imageUri.split(".").pop(); // Extract image extension
  60. const filename = `${uuid()}.${ext}`; // Generate unique name
  61.  
  62. return firebase
  63. .storage()
  64. .ref(`Asigurari/images/${filename}`)
  65. .putFile(autoData.imageUri)
  66. .on(
  67. firebase.storage.TaskEvent.STATE_CHANGED,
  68. snapshot => {
  69. let localState = { filename };
  70. if (snapshot.state === firebase.storage.TaskState.SUCCESS) {
  71. setValues(prevState => ({ ...prevState, ...localState }));
  72. }
  73. },
  74. error => {
  75. unsubscribe();
  76. alert("Sorry, Try again.");
  77. }
  78. );
  79. };
  80.  
  81. const chooseFile = () => {
  82. var options = {
  83. title: "Select Image",
  84. customButtons: [
  85. { name: "customOptionKey", title: "Choose Photo from Custom Option" }
  86. ],
  87. storageOptions: {
  88. skipBackup: true,
  89. path: "images"
  90. }
  91. };
  92. ImagePicker.showImagePicker(options, response => {
  93. if (response.didCancel) {
  94. console.log("User cancelled image picker");
  95. } else if (response.error) {
  96. console.log("ImagePicker Error: ", response.error);
  97. } else if (response.customButton) {
  98. console.log("User tapped custom button: ", response.customButton);
  99. alert(response.customButton);
  100. } else {
  101. const source = { uri: response.uri };
  102. const newData = {
  103. imgSource: source,
  104. imageUri: response.uri
  105. };
  106. // You can also display the image using data:
  107. // let source = { uri: 'data:image/jpeg;base64,' + response.data };
  108. setValues(prevState => ({ ...prevState, ...newData }));
  109. //console.log("AutoData=>", {vfc:true}, autoData);
  110. }
  111. });
  112. };
  113.  
  114. return (
  115. <View style={styles.main}>
  116. <Text style={styles.title}>{autoData.type}</Text>
  117. <DatePicker
  118. style={styles.datePicker}
  119. date={autoData.date} //initial date from autoData
  120. mode="date" //The enum of date, datetime and time
  121. placeholder="select expire date"
  122. format="DD-MM-YYYY"
  123. minDate=""
  124. maxDate=""
  125. confirmBtnText="Confirm"
  126. cancelBtnText="Cancel"
  127. onDateChange={date => handleDateChange(date)}
  128. />
  129. <TextInput
  130. style={styles.itemInput}
  131. value={autoData.name}
  132. onChangeText={name => handleNameChange(name)}
  133. />
  134. <TouchableHighlight
  135. style={styles.button}
  136. onPress={() => {
  137. handleSubmit();
  138. uploadImage();
  139. }}
  140. >
  141. <Text style={styles.buttonText}>Update</Text>
  142. </TouchableHighlight>
  143. <Button title="Select Image" onPress={chooseFile} />
  144. <Image source={autoData.imgSource} style={styles.image} />
  145. </View>
  146. );
  147. };
  148.  
  149. const styles = StyleSheet.create({
  150. main: {
  151. flex: 1,
  152. padding: Dimensions.primarySpacing,
  153. flexDirection: "column",
  154. justifyContent: "center"
  155. },
  156. datePicker: {
  157. width: Dimensions.datePickerWidth,
  158. marginBottom: 20,
  159. marginLeft: 20
  160. },
  161. title: {
  162. marginBottom: Dimensions.primarySpacing,
  163. fontSize: Dimensions.titleFontSize,
  164. textAlign: "center"
  165. },
  166. itemInput: {
  167. height: Dimensions.primaryHeight,
  168. padding: 4,
  169. marginRight: 5,
  170. fontSize: Dimensions.titleFontSize,
  171. borderWidth: 1,
  172. borderColor: Colors.addItemInputBackgroundColor,
  173. borderRadius: 8,
  174. color: Colors.addItemInputBackgroundColor
  175. },
  176. buttonText: {
  177. fontSize: 18,
  178. color: Colors.loginButtonColor,
  179. alignSelf: "center"
  180. },
  181. button: {
  182. height: Dimensions.primaryHeight,
  183. flexDirection: "row",
  184. backgroundColor: Colors.white,
  185. borderColor: Colors.white,
  186. borderWidth: 1,
  187. borderRadius: 8,
  188. marginBottom: Dimensions.padding,
  189. marginTop: Dimensions.padding,
  190. alignSelf: "stretch",
  191. justifyContent: "center"
  192. },
  193. image: {
  194. marginTop: 20,
  195. minWidth: 200,
  196. height: 200
  197. }
  198. });
  199.  
  200. export default withFireBase(withNavigation(EditItem));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement